I have the following text
abc <THIS> abc <THAT> abc <WHAT> abc
where abc is a placeholder for a well defined expression. I'd like to extract the 3 terms in the brackets and save them in 3 separate variables. Is is possible to do that without parsing the text 3 times? Basically I'd like to capture and somehow "export" multiple groups.
It's clear that I can extract one of them like this:
VARIABLE=`echo $TEXT | sed "s_abc <\(.*\)> abc <.*> abc <.*> abc_\1_g"`
But is it possible to get all 3 of them without running sed 3 times?
Other (portable) solutions without sed are also welcome.
If there are any characters that you know will not appear in THIS, THAT, or WHAT, then you can write something like this:
IFS=$'\t' read -r VAR1 VAR2 VAR3 \
< <(sed 's/^abc <\(.*\)> abc <\(.*\)> abc <\(.*\)> abc$/\1\t\2\t\3/' \
<<< "$TEXT"
)
telling sed to use that separator in its output, and read to use that separator in its input.
This might work for you (GNU sed & bash):
line='abc <THIS> abc <THAT> abc <WHAT> abc'
var=($(sed 's/[^<]*<\([^>]*\)>[^<]*/"\1" /g' <<<"$line"))
echo "first ${var[0]} second ${var[1]} third ${var[2]}"
first "THIS" second "THAT" third "WHAT"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With