合并相同的模式之间的多条线路到使用一行AWK / SED [英] Merge multiple lines between same pattern into a single line using awk/sed
本文介绍了合并相同的模式之间的多条线路到使用一行AWK / SED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些信息,一些日志文件如下
I have some log files with some information as below
2016-03-18 00:07:43,482 SOME ERROR
OCCURED
API
LINE INFO
2016-03-18 00:07:44,482 OCCURED
authentication failure
2016-03-18 00:07:45,482 ERROR OCCURED
2016-03-18 00:07:46,482 NOT IMP OCCURED
所需的输出
2016-03-18 00:07:43,482 SOME ERROR OCCURED API LINE INFO
2016-03-18 00:07:44,482 OCCURED authentication failure
2016-03-18 00:07:45,482 ERROR OCCURED
2016-03-18 00:07:46,482 NOT IMP OCCURED
这意味着我想所有的线合并2016年3月18日之间。
That means I want to merge all the lines between 2016-03-18
我想每一行开始与2016年3月18日为图案。
I want every line to start with the 2016-03-18 as the pattern.
有人能帮助我做到这一点使用shell脚本preferably用awk /桑达?
Could someone help me do this using shell scripting preferably using awk/Sed?
推荐答案
您可以使用以下 AWK
命令:
awk '
/^[0-9]{4}(-[0-9]{2}){2}/ && line {print line; line=""}
{line = line ? line" "$0 : $0}
END {print line}
' file
更新
原来,有些 AWK
的实施不支持 {N}
量词。例如 mawk
。
It turned out that some awk
implementations doesn't support {n}
quantifiers. For example mawk
.
在这种情况下,你必须写命令是这样的:
In that case you'll have to write the command like this:
mawk '
/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/ && line {print line; line=""}
{line = line ? line" "$0 : $0}
END {print line}
' file
这篇关于合并相同的模式之间的多条线路到使用一行AWK / SED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文