为什么我的awk脚本
BEGIN {
FS = "][ \t\v]+"
}
# Note space after + in the end of the regex.
NF == 2 && $1 ~ /[:alpha:][:digit:]+ / {
print $1, "<<<";
}
Run Code Online (Sandbox Code Playgroud)
不匹配文件中的任何字符串,如下所示:
I1130 15:18:42.526808 17329 thrift_bridge.cpp:126] AAA
E1130 15:18:42.527042 16076 thrift_bridge.hpp:288] BBB
Run Code Online (Sandbox Code Playgroud)
但是如果我删除空格,则两行都在输出中.
这是因为你的字符类语法是错误的:
/[[:alpha:]][[:digit:]]+ /
Run Code Online (Sandbox Code Playgroud)
没有方括号[:alpha:],[:digit:]看起来不像预定义的POSIX字符类,而是像基本类.
/[:alpha:][:digit:]+//[ahlp:][dgit:]+/与p:每行相同,并匹配.
正如@ John1024注意到的那样,mawk不支持POSIX字符类,所以你必须写:
/[a-zA-Z][0-9]+ /
Run Code Online (Sandbox Code Playgroud)
或者使用gawk,因为它在linux下可用.