每条写入行都属于第一个标记向上。 例如:'卡车司机喝醉了#39;属于AN8。我想将属于相应标签的所有书写行分组。行应保持相同的顺序。
输入:
AN9
the cow is eating way too much
AN8
The trucker is drunk
AN9
The field are running out of herbs.
AN8
the truck is not going that staight
well of course the road is in curve
AN9
and
another line
AN8
The cop needs to check this out
AN9
now the cow is soooo big dude !
输出:
AN9
the cow is eating way too much
The field are running out of herbs.
and
another line
now the cow is soooo big dude !
AN8
The trucker is drunk
the truck is not going that staight
well of course the road is in curve
The cop needs to check this out
答案 0 :(得分:0)
这是一个awk
awk '/^AN/ {id=$0;next} {a[id]=a[id]"\n"$0} END {for (i in a) print i,a[i]}' file
AN8
The trucker is drunk
the truck is not going that staight
well of course the road is in curve
The cop needs to check this out
AN9
the cow is eating way too much
The field are running out of herbs.
and
another line
now the cow is soooo big dude !
如果行以AN
开头,请将行ID
中的信息用于数组a
。
然后将所有行存储在数组a
中。最后打印数组a