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

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, 2022 at 5:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .