2017-07-15 43 views
-1

的文本文件:如何從複雜文件中的字段

$ cat filename.txt 
2017-07-15 00:00:27,000 NAME: THT TYPE: S {"cp":"R3"} 
2017-07-15 00:00:27,301 NAME: THT TYPE: S {"cp":"R3"} 
2017-07-15 00:00:26,993 NAME: THT TYPE: M {"bl":"t","cp":"R1","scp":"T5"}. 

,我嘗試的命令行:

$ cat filename.txt |awk '{print $1,$2,$4,$6,$7}' 
2017-07-15 00:00:27,000 THT S {"cp":"R3"} 
2017-07-15 00:00:27,301 THT S {"cp":"R3"} 
2017-07-15 00:00:26,993 THT M {"bl":"t","cp":"R1","scp":"T5"} 

所需的輸出:

017-07-15 00,THT,S,R3 
017-07-15 00,THT,S,R3 
017-07-15 00,THT,M,R1 

我想我們可以使用「IF」,但我沒有任何想法用AWK使用「IF」。

回答

1

考慮到您的Input_file與此處顯示的示例相同。如果是的話,請嘗試遵循awk,並讓我知道這是否有助於你。

awk -F'[ :{"]' 'NF>18{print substr($1,2),$2 s1 $7 s1 $10 s1 $21;next} {print substr($1,2),$2 s1 $7 s1 $10 s1 $16}' s1="," Input_file 

這裏也增加了非線性形式的解決方案。

awk -F'[ :{"]' 'NF>18{ 
        print substr($1,2),$2 s1 $7 s1 $10 s1 $21; 
        next 
       } 
       { 
        print substr($1,2),$2 s1 $7 s1 $10 s1 $16 
       } 
      ' s1="," Input_file 
0

呆子溶液:

awk '{ $7=gensub(/.*"cp":"([^"]+)".*/,"\\1","g",$7); 
     print substr($1,2)" "substr($2,1,2),$4,$6,$7 }' OFS=',' filename.txt 

輸出:

017-07-15 00,THT,S,R3 
017-07-15 00,THT,S,R3 
017-07-15 00,THT,M,R1 

  • $7=gensub(/.*"cp":"([^"]+)".*/,"\\1","g",$7) - 捕獲"cp"屬性值
1
$ awk -v OFS=',' '{match($NF,/"cp":"[^"]+/); print substr($0,2,12), $4, $6, substr($NF,RSTART+6,RLENGTH-6)}' file 
017-07-15 00,THT,S,R3 
017-07-15 00,THT,S,R3 
017-07-15 00,THT,M,R1