[gawk] Extract text matched by a regular expression capture group

Created on November 28, 2026

You can use the match function in gawk to extract text matched by a capture group.

echo __dummy__ | gawk 'match($0, /__(.*?)__/, m) { print m[1] }'

Explanation

  • The first argument to match is the string to search. Here, $0 contains the current input record.
  • The second argument is the regular expression.
  • The third argument is the array that stores the match results.
  • m[1] contains the text matched by the first capture group.

Output

dummy
gawk