提取文件中指定模式匹配的字段内容:
awk 'BEGIN{FS=OFS=","}{for(i=1;i<=NF;i++){if($i ~ /daytime=|sectime=|s1=|cid=/)lines=lines","$i;};print lines;lines=""}' $FILE
进阶版,使用awk的match函数:
awk '{match($0,/(daytime=[^,]*?,)/,a);match($0,/(sectime=[^,]*?,)/,b);match($0,/(s1=[^,]*?,)/,c);match($0,/(cid[^}]*)?}/,d);print a[1],b[1],d[1],",",c[1]}' clickid_807c98_2017-09-17_23_conver.txt
超强版,直接生成csv格式可以使用Excel打开:
awk '{match($0,/daytime=([^,]*?,)/,a);match($0,/sectime=([^,]*?,)/,b);match($0,/s1=([^,]*?,)/,c);match($0,/cid=([^}]*)?}/,d);print a[1],b[1],d[1],",",c[1]}' clickid_807c98_2017-09-17_23_conver.txt