0

I have a string as below

Delete :xna.del|mode:Full|Execution: loaded

I need my output to be stored in 3 parameters. for an example

a=xna.del
b=Full
c=loaded
New contributor
user3416176 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 Answers 2

1

With zsh instead of bash, storing the result in an associative array:

$ string='Delete :xna.del|mode:Full|Execution: loaded'
$ IFS=':|'
$ set -o extendedglob
$ typeset -A field=( ${=string//[[:space:]]#:[[:space:]]#/:} )
$ typeset -p field
typeset -A field=( [Delete]=xna.del [Execution]=loaded [mode]=Full )
$ print -r -- $field[mode]
Full
0

so i guess the Word in front of ":" has no concern for you?

Try this solution with an Array for all Values

#!/usr/bin/env bash
TEXT="Delete :xna.del|mode:Full|Execution: loaded"
MYVAR=( $(tr '|' '\n' <<< "${TEXT}" |cut -d: -f2) )

#Checks on the result
echo MYVAR has "${#MYVAR[@]}" entries
#single-Access
echo "${MYVAR[0]}"
echo "${MYVAR[1]}"
echo "${MYVAR[2]}"
#formated output
printf "\t%s\n" "${MYVAR[@]}"
#iterate again to work with it?
for value in "${MYVAR[@]}"; do echo "$value"; done
1
  • 1
    That looks more like zsh syntax than bash syntax. In bash, you'd need "${MYVAR[0]}", quote all the expansions except $(tr...) and disable globbing. Jun 23 at 5:32

Your Answer

user3416176 is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.