sharingli
sharingli

Reputation: 138

awk field separator \[ \]

Given following command:

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

why does awk output diffrent:

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. 

Upvotes: 1

Views: 3902

Answers (2)

jfg956
jfg956

Reputation: 16748

My understanding is that you want to use [ and ] as delimiter. In this case:

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

The char ] immediately after a [ (as in []]) is taken as itself.

Upvotes: 4

perreal
perreal

Reputation: 98098

You need to bypass shell interpretation using single quotes:

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

or escape the first backspace:

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

Upvotes: 0

Related Questions