awkの一貫性のない動作

awkの一貫性のない動作

次のテキストファイルがあります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

次のコマンドを実行すると、次の結果が表示されます。

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

正規表現がの左側にある場合、何が起こるかを理解してください~

01つまたは空の文字列(null?)が一致すると見なされることがわかります。

私が理解していないのは、遺言章が時々0一致し、時には一致しない理由です。以前に処理されたすべてのレコードに関連しているように見えますが、awk各入力レコードは独立して処理されるため、互いに影響を与えてはいけません(少なくともいくつかの変数の割り当てや他の操作なし)。

編集:重要な場合はGNU Awk 4.1.3を使用しています。

ベストアンサー1

(g)awksのマニュアルページから:

~ !~        Regular expression match, negated match.  NOTE: Do not use a constant regular  expression  (/foo/)
            on  the left-hand side of a ~ or !~.  Only use one on the right-hand side.  The expression /foo/ ~
            exp has the same meaning as (($0 ~ /foo/) ~ exp).  This is usually not what you want.

明示的に禁止された方法で使用すると何が起こると予想されますか?

おすすめ記事