Skip to content

/errors/awk/warning-escape-sequence-treated-as-plain

awk: warning: escape sequence `\.' treated as plain `.'

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · GNU Awk 5.4.1updated
tool
awk
exit code
0
versions
GNU Awk 4.x – 5.x
os
linux, macos, windows

awk: warning: escape sequence `\.' treated as plain `.' means gawk found \. inside a string (including the value of -F), and \. is not a string escape, so gawk dropped the backslash. The regex then receives a bare ., which matches any character. The program keeps running, which makes the warning easy to ignore and the result quietly wrong. Write the separator as -F'[.]', or use a regex constant such as /\./.

Reproduce the warning

bash
echo "app.example.com" | awk -F'\.' '{ print $2 }'

Output

awk: warning: escape sequence `\.' treated as plain `.'
example

The field split still worked here because gawk treats a single-character FS other than space literally. A longer string shows the real damage, because the dot now matches any character:

bash
echo "a-b.c" | awk '{ sub("\.c", "_"); print }'

Output

awk: cmd. line:1: warning: escape sequence `\.' treated as plain `.'
a-b_

sub replaced the first "any character followed by c", which here is .c, by luck. With the regex constant the match is exact:

bash
echo "abc.c" | awk '{ sub("\.c", "_"); print }'
echo "abc.c" | awk '{ sub(/\.c/, "_"); print }'

Output

awk: cmd. line:1: warning: escape sequence `\.' treated as plain `.'
a_.c
abc_

The first line turned abc.c into a_.c (the regex .c matched bc); the second replaced the literal .c.

Fix: bracket expressions, regex constants or double backslashes

bash
echo "app.example.com" | awk -F'[.]' '{ print $2 }'
echo "a|b|c" | awk -F'[|]' '{ print $3 }'
echo "abc.c" | awk '{ n = split($0, p, /\./); print n, p[2] }'
echo "abc.c" | awk '{ sub("\\.c", "_"); print }'

Output

example
c
2 c
abc_
Written asRegex gawk usesWarning
-F'\.'. (single char, literal)yes
"\." in sub/split/gsub. (any character)yes
"\\."\. (literal dot)no
/\./\. (literal dot)no
-F'[.]' or "[.]"[.] (literal dot)no

The regexp variant

Inside a regex constant, gawk warns about unknown regex escapes with a different message, for example regexp escape sequence `\"' is not a known regexp operator. The cause is the same: a backslash that does nothing. Remove it, or use a bracket expression.

Sources