Search code examples
regexwindowspattern-matchinggawk

awk regex pattern does not match beginning of the line


I'm using GNU awk version 3.1.7 on Windows 10, MinGW installation.

File to test this has this contents but same behaviour is with other files as well.

test.txt

line one
second line
another line
end this one should match
double test
yet another

I want to print only first words beginning with e.

awk command I'm using is:

awk '{ if ($1 ~ /^e/) {print $1} }' test.txt

But this prints every first word which has character e anywhere.

output

line
second
another
end
double 
yet

When I want to match end of the word works fine.
Match every first word ending with d.

awk '{ if ($1 ~ /d$/) {print $1} }' test.txt

output

second
end

Any idea why first example matching beginning of the word does not work?
What I'm I doing wrong there?


Solution

  • That's got nothing to do with gawk it's Windows quoting rules. gawk doesn't even see the quotes - it just runs on whatever script Windows passes to it (i.e. the part between the quotes) and it's entirely Windows that interprets the quotes to isolate the script that it then passes to gawk. The standard advice is to avoid the problem is by putting the awk script in a file and running as awk -f script instead of trying to deal with the Windows quoting nightmare. The best advice though is to run cygwin on top of Windows.