AWK: Access captured group from line pattern

rampion picture rampion · Jun 2, 2010 · Viewed 177.1k times · Source

If I have an awk command

pattern { ... }

and pattern uses a capturing group, how can I access the string so captured in the block?

Answer

glenn jackman picture glenn jackman · Jan 12, 2011

With gawk, you can use the match function to capture parenthesized groups.

gawk 'match($0, pattern, ary) {print ary[1]}' 

example:

echo "abcdef" | gawk 'match($0, /b(.*)e/, a) {print a[1]}' 

outputs cd.

Note the specific use of gawk which implements the feature in question.

For a portable alternative you can achieve similar results with match() and substr.

example:

echo "abcdef" | awk 'match($0, /b[^e]*/) {print substr($0, RSTART+1, RLENGTH-1)}'

outputs cd.