简体   繁体   English

带有字段和变量的简单模式匹配在 GAWK/AWK 中似乎不起作用

[英]Simple Pattern match with a field and a variable does not seem to work in GAWK/AWK

I am trying to extract all lines where a field matches a pattern which is defined as a variable.我正在尝试提取字段与定义为变量的模式匹配的所有行。 I tried the following我尝试了以下

head input.dat |
awk -F '|' -v CODE="39905|19043" '{print $13; if($13~CODE){print "Matched"} else {print "Nomatch"} }'

I am printing the value of the field before attempting a pattern match.(This way I don't have to show the entire line that contains many fields) This is the output I got.我在尝试模式匹配之前打印字段的值。(这样我就不必显示包含许多字段的整行)这是我得到的 output。

PLAN_ID
Nomatch
39905
Nomatch
39905
Nomatch
39883
Nomatch
19043
Nomatch
2215
Nomatch
19043
Nomatch
9149
Nomatch
42718
Nomatch
24
Nomatch

I expected to see at least 3 instances of Matched in the output. What am I doing wrong?我希望在 output 中看到至少 3 个 Matched 实例。我做错了什么?


edit by @Fravadona由@Fravadona 编辑

xxd input.dat | head -n 6
00000000: fffe 4d00 4f00 4e00 5400 4800 5f00 4900 ..M.O.N.T.H._.I.
00000010: 4400 7c00 5300 5600 4300 5f00 4400 5400 D.|.S.V.C._.D.T.
00000020: 7c00 5000 4100 5400 4900 4500 4e00 5400 |.P.A.T.I.E.N.T.
00000030: 5f00 4900 4400 7c00 5000 4100 5400 5f00 .I.D.|.P.A.T..
00000040: 5a00 4900 5000 3300 7c00 4300 4c00 4100 Z.I.P.3.|.C.L.A.
00000050: 4900 4d00 5f00 4900 4400 7c00 5300 5600 I.M._.I.D.|.S.V.

Turns out that the input file uses the UTF-16 LE Encoding (as shown by the hexdump of the content).事实证明,输入文件使用 UTF-16 LE 编码(如内容的 hexdump 所示)。 Thus, the solution seems to be to convert the input file from UTF-16LE to UTF-8 before running AWK. Thanks因此,解决方案似乎是在运行 AWK 之前将输入文件从 UTF-16LE 转换为 UTF-8。谢谢

I found out (thanks to all who suggested looking at the hexdump of the input file) that the file used UTF-16LE encoding.我发现(感谢所有建议查看输入文件的 hexdump 的人)该文件使用 UTF-16LE 编码。 Once I converted the input file using iconv , the AWK script worked as expected使用iconv转换输入文件后,AWK 脚本按预期工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM