Consider the string hello(world)
.
I wanted to use an awk
invocation to extract world
.
This was my first attempt, and the output was an empty string, with no errors.
echo 'hello(world)' | awk -F'(|)' '{print $2}'
My second attempt was to use character classes, which produced the expected behavior:
echo 'hello(world)' | awk -F'[()]' '{print $2}'
However, given that awk’s documentation claims that the field separator can be a regular expression, I expected the first attempt to work.
Here is my awk version:
$ awk --version
GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.2.0)
Copyright (C) 1989, 1991-2019 Free Software Foundation.
Why does the expression (|)
not achieve the desired result?
After fiddling around with putting in front of various characters, I finally realized that the problem was with the
()
and not with the |
character. Moreover, the ()
needed to be double-escaped.
The following expression achieves the desired result:
echo 'hello(world)' | awk -F'\(|\)' '{print $2}'
world
Curiously, it omitting the escape on the closing parenthesis also appears to work correctly:
echo 'hello(world)' | awk -F'\(|)' '{print $2}'
world