Parse String from a Variable

Hello,

Is there a quick way to parse the values from a variable?

The variable has the following sample input:
TA=[IV_Test PF_SAPP_FWK]

The values of the TA variable is not fixed/hardcoded

Basically I need to get the IV_Test and PF_SAPP_FWK values.

I created a script that first use sed to remove [ ] , then redirect it to a file
Then use awk to parse the contents of the file, and redirect it to another file.

echo $TA | sed 's/\[//g' | sed 's/\]//g' > $TEMP_FILE
awk -F" " '{
        for (i = 1; i <= NF; i++) {
           n = split($i, q, " ")
           print q[n] >> "$TEMP_FILE1"
        }
        }'$TEMP_FILE

while read TA_LEVEL
        do
        echo "VALUE: $TA_LEVEL"
done < $TEMP_FILE1

Im just a newbie in scripting and I appreciate your help.

Thanks,
racbern

You don't really need the temp file.

Take care to properly quote any user input.

echo "$TA" | sed -e 's/TA=\[//' -e 's/]$//' -e 's/ /\
/g' |
while read TA_LEVEL; do
  echo TA_LEVEL="$TA_LEVEL"
done

Some seds are picky when it comes to newlines inside a quoted string so you might have to experiment with that, or maybe fall back to your awk solution (or tr ' ' '\012' or some such).

Do I infer correctly that the number of labels between the [brackets] can be variable, and you want to break them up to one per line? That wasn't entirely clear from your problem description, but looks like that's what your code does. If it's always two fields then maybe this could be simplified further.

Try this one:

echo "$TA" | awk 'BEGIN{FS="\[|\]"}{split($2,s," ");print s[1],s[2]}'

Regards

If you're looking to do it in a single line, try this:

echo $TA | perl -pe 's/^\[(.) (.)\]$/$1\n$2/' > file.txt

Hope this helps.