Awk 使用标记将行分组

Awk 使用标记将行分组,awk,set,Awk,Set,写入的每一行都属于向上的第一个标记。 例如:“卡车司机喝醉了”属于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 a

写入的每一行都属于向上的第一个标记。 例如:“卡车司机喝醉了”属于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

这里有一个
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
开头,则将行中的信息用作数组
a
ID


然后将所有行存储在数组
a
中。最后打印数组
a

中的所有数据听起来很有趣,请添加您迄今为止尝试过的内容,我们很乐意提供帮助!非标记行能否以“AN”开头,例如,
是否有可能发生这种情况?
。那么一个数字怎么样?例如,
AN627是曲马多50mg。
。如果上面所有的-我们如何识别标记行?它工作得很好,但是如果我们有,比如说,ANx,BQx,ZOx?要我列举所有的标签吗?你可以使用
/^…$/
。如果行是三个字符,则将其设置为标记。或者
/^[:alnum:][[:alnum:][[:digit:][]+/
。你只需要定义一些只在ID上点击的唯一的