将多行堆栈加入1行

时间:2015-01-13 19:19:15

标签: shell

我想将以下日志堆栈转换为单行,同时保持两个堆栈之间的分离。

我的问题是根据示例日志获得下面所需输出的Linux命令是什么?

示例日志:

At 2014-12-24 10:45:40.010: 250 OK^M
At 2014-12-24 10:45:40.010: 550 Unknown user^M
At 2014-12-24 10:45:40.010: 503-All RCPT commands were rejected with this error:^M
503-Unknown user^M
503 Valid RCPT command must precede DATA^M
Log-Entry-Offset/Size: 1267870/0000633^M
Content-ID: 223994/50022_2142272156   HU^M
MailHost: btinternet.com(0.0.0.0)^M
Addressee: denniscruze@btinternet.com^M
Status-Attempt-Session-Error: 55:1:39d9:451^M
Date: Wed Dec 24 10:45:40 EST 2014^M
At 2014-12-24 10:45:39.846: Sent: RSET^M

At 2014-12-24 10:46:09.658: 250 OK^M
At 2014-12-24 10:46:09.658: 550 Unknown user^M
At 2014-12-24 10:46:09.658: 503-All RCPT commands were rejected with this error:^M
503-Unknown user^M
503 Valid RCPT command must precede DATA^M
Log-Entry-Offset/Size: 2705117/0000659^M
Content-ID: 16680/50022_1962750881   HU^M
MailHost: aol.com(0.0.0.0)^M
Addressee: mvrow@aol.com^M
Status-Attempt-Session-Error: 1:1:39a2:250^M
Date: Wed Dec 24 10:46:09 EST 2014^M
At 2014-12-24 10:46:09.498: Sent: MAIL FROM:<news@go.intervalworld.com>^M

期望的输出:

At 2014-12-24 10:45:40.010: 250 OK^M At 2014-12-24 10:45:40.010: 550 Unknown user^M At 2014-12-24 10:45:40.010: 503-All RCPT commands were rejected with this error:^M 503-Unknown user^M 503 Valid RCPT command must precede DATA^M Log-Entry-Offset/Size: 1267870/0000633^M Content-ID: 223994/50022_2142272156   HU^M MailHost: btinternet.com(0.0.0.0)^M Addressee: denniscruze@btinternet.com^M Status-Attempt-Session-Error: 55:1:39d9:451^M Date: Wed Dec 24 10:45:40 EST 2014^M At 2014-12-24 10:45:39.846: Sent: RSET^M

At 2014-12-24 10:46:09.658: 250 OK^M At 2014-12-24 10:46:09.658: 550 Unknown user^M At 2014-12-24 10:46:09.658: 503-All RCPT commands were rejected with this error:^M 503-Unknown user^M 503 Valid RCPT command must precede DATA^M Log-Entry-Offset/Size: 2705117/0000659^M Content-ID: 16680/50022_1962750881   HU^M MailHost: aol.com(0.0.0.0)^M Addressee: mvrow@aol.com^M Status-Attempt-Session-Error: 1:1:39a2:250^M Date: Wed Dec 24 10:46:09 EST 2014^M At 2014-12-24 10:46:09.498: Sent: MAIL FROM:<news@go.intervalworld.com>^M

1 个答案:

答案 0 :(得分:1)

使用此代码:

awk 'BEGIN {
    FS='\n'
    RS='\n\n'
}
{
    gsub(/\n/, " ", $0)
    print $0
    print ""
}' file_goes_here

将每个记录的文件记录分开,即你想要彼此相邻的所有东西,它们是分开的bij双新行。然后为空格和输出提供所有换行符。