Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract multiple captured groups from sed to variables

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.

like image 682
Jawap Avatar asked Nov 05 '12 16:11

Jawap


2 Answers

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.

like image 111
ruakh Avatar answered Oct 21 '22 07:10

ruakh


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"
like image 25
potong Avatar answered Oct 21 '22 08:10

potong