仅当记录二与模式匹配时如何连接记录

时间:2014-05-14 08:50:03

标签: regex join awk matching

我有一个由时间戳和丢弃的数据包信息组成的文件:

Tue May 13 09:50:00 HST 2014
Tue May 13 09:55:00 HST 2014
60 packets transmitted, 50 packets received, 16.7% packet loss
Tue May 13 10:00:00 HST 2014
60 packets transmitted, 54 packets received, 10.0% packet loss
Tue May 13 10:05:00 HST 2014
Tue May 13 10:10:00 HST 2014
Tue May 13 10:15:00 HST 2014

使用awk,我想输出只有数据包信息行,加上它前面的日期行:

Tue May 13 09:55:00 HST 2014 60 packets transmitted, 50 packets received, 16.7% packet loss
Tue May 13 10:00:00 HST 2014 60 packets transmitted, 54 packets received, 10.0% packet loss

我无法根据模式匹配找到有关加入记录的任何信息。任何帮助将不胜感激

2 个答案:

答案 0 :(得分:2)

您可以使用此sed

sed -n 'N; /packet/s/\n/ /p; D;' yourfile

答案 1 :(得分:1)

使用awk

awk '/loss$/ {print p,$0} {p=$0}' file
Tue May 13 09:55:00 HST 2014 60 packets transmitted, 50 packets received, 16.7% packet loss
Tue May 13 10:00:00 HST 2014 60 packets transmitted, 54 packets received, 10.0% packet loss