I have a text file called junk.txt
:
hello
foo
0
-1
0
1
0
2
0
foo
Cindy
00000
Lou
2 000
0
Who
0000
0
wat?
0000 00000
0
0
0000 00000
filler
00
0
00
000
0000
0
0
bye
When I run the following I get this:
cat junk.txt | awk '{if (/foo/ ~ $1) print $1,"<-- found match"; else print $1}'
awk: cmd. line:1: warning: regular expression on left of `~' or `!~' operator
hello
foo
0 <-- found match
-1
0 <-- found match
1
0 <-- found match
2
0 <-- found match
foo
<-- found match
Cindy
00000
<-- found match
Lou
2
0 <-- found match
Who
0000
0
wat?
0000
0
0
0000
<-- found match
filler
<-- found match
00
<-- found match
0 <-- found match
00 <-- found match
000 <-- found match
0000 <-- found match
0 <-- found match
<-- found match
0 <-- found match
bye
I understand what is happening when the regex is to the left of the ~
.
I understand that a 0
or an empty string (null?) will count as a match.
What I do not understand is why sometimes a 0
will match, and sometimes it won't. It seems like it has something to do with whatever record was processed before it, but I thought awk
treated each input record independently so they shouldn't affect one another (at least not without some variable assignments or other manipulation).
EDIT: In case it matters I'm using GNU Awk 4.1.3