今天一同事问一个日志排序问题 , 结果和过程可能比较简单,如果对函数不熟悉的话,只想提一下可以另解的思路
test.data内容如下
a "t=15" 333 bbba1 xy jfdf 298
a2 zz jfdf 298
b "t=13" uv mnnn
b1 0z asdf aaa
b2 wif wfinwlk sls
c "t=14" sl sldfjdslf
c1 sfdjfdj slffj sdlkfjsdlfj
c2 slsls ssss ssss
需求是对"t=xxx"这个xxx值进行排序 ,实际上a,a1,a2可以看成同一行,只是日志中打到了不同的行,
希望输出是如下之类的:
b "t=13" uv mnnn
b1 0z asdf aaa
b2 wif wfinwlk sls
c "t=14" sl sldfjdslf
c1 sfdjfdj slffj sdlkfjsdlfj
c2 slsls ssss ssss
a "t=15" 333 bbba1 xy jfdf 298
a2 zz jfdf 298
或
b "t=13" uv mnnn b1 0z asdf aaa b2 wif wfinwlk sls
c "t=14" sl sldfjdslf c1 sfdjfdj slffj sdlkfjsdlfj c2 slsls ssss ssss
a "t=15" 333 bbb a1 xy jfdf 298 a2 zz jfdf 298
其实如果不用asort 或asorti之类的函数可以这样(合并行,然后提取数值成独立一列,用sort排序):
awk 'BEGIN{row=0;} {row++; gsub(/"\n"/,"", $0); a[NR]=$0; if($2~/"/) gsub(/["|t=]/,"",$2); b[NR]=$2;} END{for(i=1;i<=row;i+=3 )print b[i]"\t"a[i]"\t"a[i+1]"\t"a[i+2]"\t"}' test.data | sort -n
输出如下:
13 b "t=13" uv mnnn b1 0z asdf aaa b2 wif wfinwlk sls
14 c "t=14" sl sldfjdslf c1 sfdjfdj slffj sdlkfjsdlfj c2 slsls ssss ssss
15 a "t=15" 333 bbb a1 xy jfdf 298 a2 zz jfdf 298