awk字段分隔符\ [\]

时间:2012-12-26 01:29:29

标签: awk

给出以下命令:

cat 11 | awk -F"[\[\]]" '{print $1,$2}'        
cat 11 | awk -F"[\]\[]" '{print $1,$2}'

为什么awk输出不同:

zxscgdb@linux:~/tmp> cat 11    
12-05 09:07:53:33 F[zxinit_lib.c] L[485] E[10106] process:zxsc_gdb is ok.    
12-05 09:08:03:35 F[zxinit_lib.c] L[485] E[10106] process:zxsc_gdb is ok.    
12-05 09:08:13:37 F[zxinit_lib.c] L[485] E[10106] process:zxsc_gdb is ok.

zxscgdb@linux:~/tmp> cat 11 | awk -F"[\]\[]" '{print $1,$2}'    
awk: warning: escape sequence `\]' treated as plain `]'    
awk: warning: escape sequence `\[' treated as plain `['    
12-05 09:07:53:33 F zxinit_lib.c    
12-05 09:08:03:35 F zxinit_lib.c    
12-05 09:08:13:37 F zxinit_lib.c    

zxscgdb@linux:~/tmp> cat 11 | awk -F"[\[\]]" '{print $1,$2}'    
awk: warning: escape sequence `\[' treated as plain `['    
awk: warning: escape sequence `\]' treated as plain `]'    
12-05 09:07:53:33 F[zxinit_lib.c] L[485] E[10106] process:zxsc_gdb is ok.     
12-05 09:08:03:35 F[zxinit_lib.c] L[485] E[10106] process:zxsc_gdb is ok.     
12-05 09:08:13:37 F[zxinit_lib.c] L[485] E[10106] process:zxsc_gdb is ok. 

2 个答案:

答案 0 :(得分:2)

我的理解是您希望使用[]作为分隔符。在这种情况下:

awk -F '[][]' '{print $1,$2}' 11

]后的字符[(如[]]中所示)将被视为自身。

答案 1 :(得分:0)

您需要使用单引号绕过shell解释:

awk -F'[\\[\\]]' '{print $2,$4}' 11

或逃避第一个退格:

 awk -F"[\\\[\\\]]" '{print $2,$4}' 11