1

给定以下命令:

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. 
4

2 回答 2

4

我的理解是你想用[and]作为分隔符。在这种情况下:

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

]紧跟在 a [(如) 之后的 char[]]被视为自身。

于 2012-12-26T10:39:41.007 回答
0

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

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

或转义第一个退格键:

 awk -F"[\\\[\\\]]" '{print $2,$4}' 11
于 2012-12-26T01:58:11.810 回答