Make awk character classes

How do I make awk recognize character classes?

For example, this:

echo "a\n1\nb\n2\nc" | awk '/1/'

      

outputs 1

as expected, but this:

echo "a\n1\nb\n2\nc" | awk '/\d/'

      

no output, where I expected to like 1

, and 2

to survive in the filter.

I thought it might be related to shell escaping (zsh) but awk '/\\d/'

doesn't work either.

+3


source to share


1 answer


You can try using conditional character classes:

[ghoti@pc ~]$ printf "a\n1\nb\n2\nc\n" | awk '/[[:digit:]]/'
1
2
[ghoti@pc ~]$ 

      

As far as I know, type notation \d

is actually not part of the ERE, which is the regex dialog understood by most awk variants (as well as The One True Awk ).




UPDATE

As noted in the comments, some Linux distributions may have mawk

masquerading as awk

. mawk is not the same as awk. It is a minimally available awk clone, designed for speed of execution, not functionality. And despite claims to the page of his leadership , he supports extended regular expressions, mawk can not implement POSIX classes, such as [:digit:]

, [:upper:]

, [:lower:]

, etc.

If you run systems that provide non-standard tools, such as mawk

instead of the standard ones, then you should expect to live in interesting moments. The Awk scripter expects any binary /usr/bin/awk

to behave like awk. If not, the system will break down.

+7


source







All Articles