I am trying to get into bash a little bit. I was going through this guide but the second example in this subsection on gawk doesn't seem to work.

The problem is this command:

ls -l | awk '/\<(a|x).*\.conf$/ { print $9 }'

It works only if I replace \< with a space. I also tried using \y, but no luck there either.

Does anyone have any idea what the problem might be here?

Thanks :)

  • I just tried it and it worked fine for me, what output do you get?
    – ryekayo
    Aug 5 '14 at 20:37
  • When I leave it as it is I get no output. If I use space instead of the \< it seems to work OK and I get the list of the .conf files that match. Aug 5 '14 at 20:40
  • 2
    Are you sure you're using gawk - not, for example, mawk? Aug 5 '14 at 20:42
  • @steeldriver You're right :) I am using mawk. Could you tell me what the difference is here? Aug 5 '14 at 20:46
  • mawk is "is smaller and much faster than gawk", but lacks some of its features (including some regex capability) - as you have discovered. Aug 5 '14 at 21:28

The GNU awk manual (sec. 3.5) documents that the regex \< is gawk-specific and thus one should not expect it to work in other implementations.

According to man mawk, if you place a backslash in front of a nonspecial character, then the backslash is removed. Thus, under mawk, \< is interpreted simply as an angle bracket character.


I simplified the regex to provide examples of the different behavior:

$ echo -e " a\n ab.conf\n <ac.conf" | gawk '/\<(a|x)/ { print}'
$ echo -e " a\n ab.conf\n <ac.conf" | mawk '/\<(a|x)/ { print}'

Again, gawk interprets \< as the beginning of a word while mawk interprets it simply as an angle bracket.

What does POSIX say about this issue

The GNU awk manual explains:

If you place a backslash in a string constant before something that is not one of the characters previously listed, POSIX awk purposely leaves what happens as undefined.

In other words, in this case, the different awk interpreters are free to make their own decisions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.