Search code examples
awkescaping

How to escape the period (dot) character in awk?


I am using gawk on Windows 8.1 (from GnuWin32)

How to escape the period (".") character?

I tried with "\." and "\x2e" and it doesn't work

my input file (z.txt) is this:

fox.doc
bear.jpg
abcjpg

and I'm trying to find .jpg files with this script (z.awk):

  { printf $0; if (match($0, /\x2ejpe?g$/)) printf " - Jpeg image"; print "" }

I run this script from a batch file (z.bat):

cat z.txt | gawk -f z.awk

It doesn't matter if I'm using "\." or "\x2e" or ".", it will still match any character. The output I got is

fox.doc
bear.jpg - Jpeg image
abcjpg - Jpeg image

In the output, the last line should be:

abcjpg

I've uploaded the scripts here - https://github.com/FrostShock/WinScripts/tree/main/ZZZ


Solution

  • With GNU awk you must use the compatibility mode (-c) if you want the escape sequences to be interpreted literally:

    $ man awk
    ...
    In  compatibility  mode,  the  characters represented by octal and
    hexadecimal escape sequences are treated literally when used in regular
    expression constants.  Thus, /a\52b/ is equivalent to /a\*b/.
    

    But I am very surprized that \. did not work as expected. There must be something else that happens in what you do not show. Try, maybe:

    awk '/\.jpe?g$/ {$(NF+1) = " - Jpeg image"} {print}'
    

    Demo:

    $ printf '%s\n' "foo.doc" "bear.jpg" "abcjpg.txt" | \
      awk '/\.jpe?g$/ {$(NF+1) = " - Jpeg image"} {print}'
    abcjpg.txt
    bear.jpg  - Jpeg image
    foo.doc