如何在bash中连接同一段的行?

时间:2012-01-23 20:32:48

标签: bash

我有一个包含以下行的日志文件:

vi test.log


    23 Jan 01:29:33.498/GLOBAL/ser: RECEIVED message from 91.x.x.x:33583:
    INVITE sip:39329172xxxx@sip.x SIP/2.0^M
    Supported: ^M
    Allow: INVITE, ACK, OPTIONS, CANCEL, BYE^M
    Contact: sip:131400xxxx@91.x.x.x:33583^M
    Via: SIP/2.0/UDP 91.x.x.x:33583;branch=z9hG4bKe65d47e555749b753faaf095c3256ec569bde77d37de66f62ff18bc40d492496^M
    Call-id: ac755ea7e10821aa8174b2e5cd51d9e6^M
    Cseq: 1 INVITE^M
    From: sip:131400xxxx@sip.x;tag=5a541f1b2fd279cd0b8af3be3f67c7cf^M
    ax-forwards: 70^M
    To: sip:39329172xxxx@sip.x^M
    Content-type: application/sdp^M
    Content-length: 127^M
    ^M
    v=0^M
    o=anonymous 1327282173 1327282173 IN IP4 91.x.x.x^M
    s=session^M
    c=IN IP4 91.x.x.x^M
    t=0 0^M
    m=audio 5856 RTP/AVP 0^M

    23 Jan 01:29:33.499/GLOBAL/ser: SENDING message to 91.x.x.x:33583:
    SIP/2.0 100 trying -- your call is important to us^M
    Via: SIP/2.0/UDP 91.x.x.x:33583;branch=z9hG4bKe65d47e555749b753faaf095c3256ec569bde77d37de66f62ff18bc40d492496^M
    Call-id: ac755ea7e10821aa8174b2e5cd51d9e6^M
    Cseq: 1 INVITE^M
    From: sip:131400xxxx@sip.x;tag=5a541f1b2fd279cd0b8af3be3f67c7cf^M
    To: sip:39329172xxxx@sip.x^M
    Server: SSP v2.0.84^M
    Content-Length: 0^M
    ^M


我想要达到的目标是:


    23 Jan 01:29:33.498/GLOBAL/ser: RECEIVED message from 91.x.x.x:33583:|INVITE sip:39329172xxxx@sip.x SIP/2.0|Supported:|Allow: INVITE, ACK, OPTIONS, CANCEL, BYE|Contact: sip:1314007008@91.x.x.x:33583|Via: SIP/2.0/UDP 91.x.x.x:33583;branch=z9hG4bKe65d47e555749b753faaf095c3256ec569bde77d37de66f62ff18bc40d492496|Call-id: ac755ea7e10821aa8174b2e5cd51d9e6|Cseq: 1 INVITE|From: sip:131400xxxx@sip.x;tag=5a541f1b2fd279cd0b8af3be3f67c7cf|Max-forwards: 70|To: sip:39329172xxxx@sip.x|Content-type: application/sdp|Content-length: 127|v=0|o=anonymous 1327282173 1327282173 IN IP4 91.x.x.x|s=session|c=IN IP4 91.x.x.x|t=0 0|m=audio 5856 RTP/AVP 0
    23 Jan 01:29:33.499/GLOBAL/ser: SENDING message to 91.x.x.x:33583:|SIP/2.0 100 trying -- your call is important to us|Via: SIP/2.0/UDP 91.x.x.x:33583;branch=z9hG4bKe65d47e555749b753faaf095c3256ec569bde77d37de66f62ff18bc40d492496|Call-id: ac755ea7e10821aa8174b2e5cd51d9e6|Cseq: 1 INVITE|From: sip:131400xxxx@sip.x;tag=5a541f1b2fd279cd0b8af3be3f67c7cf|To: sip:39329172xxxx@sip.x|Server: SSP v2.0.84|Content-Length: 0

基本上,同一段(会话)中的所有行都应与“|”连接。然后应该添加回车符并连接下一段,依此类推。请注意,每个新行都以日期和时间开头。时间。

到目前为止,我只能连接所有行但无法添加回车..任何帮助将非常感激。谢谢。

2 个答案:

答案 0 :(得分:2)

您可以使用以下awk脚本来执行此操作:

awk '{if ($0 ~ /^\s*$/) {print line; line="";} else line=line $0 "|"}' file.txt

这假设在para结束后总是出现一个与你的例子相同的空行。

<强>解释

$0 ~ /^\s*$/ - to check if line is completely blank or only has white spaces
if block executes when blank line appears. It prints line var and resets line to ""
else block is concatenating line variable with the current line of file and a pipe

答案 1 :(得分:0)

这可能对你有用(虽然数据不清楚):

sed '1{h;d};/^[123]\?[0-9] [JFMASOND].. ..:..:..\..../{:a;x;s/\s*\n\+/|/g;s/.$//p;d};H;$ba;d' file
23 Jan 01:29:33.498/GLOBAL/ser: RECEIVED message from 91.x.x.x:33583:|INVITE sip:39329172xxxx@sip.x SIP/2.0|Supported:|Allow: INVITE, ACK, OPTIONS, CANCEL, BYE|Contact: sip:131400xxxx@91.x.x.x:33583|Via: SIP/2.0/UDP 91.x.x.x:33583;branch=z9hG4bKe65d47e555749b753faaf095c3256ec569bde77d37de66f62ff18bc40d492496|Call-id: ac755ea7e10821aa8174b2e5cd51d9e6|Cseq: 1 INVITE|From: sip:131400xxxx@sip.x;tag=5a541f1b2fd279cd0b8af3be3f67c7cf|ax-forwards: 70|To: sip:39329172xxxx@sip.x|Content-type: application/sdp|Content-length: 127|v=0|o=anonymous 1327282173 1327282173 IN IP4 91.x.x.x|s=session|c=IN IP4 91.x.x.x|t=0 0|m=audio 5856 RTP/AVP 0
23 Jan 01:29:33.499/GLOBAL/ser: SENDING message to 91.x.x.x:33583:|SIP/2.0 100 trying -- your call is important to us|Via: SIP/2.0/UDP 91.x.x.x:33583;branch=z9hG4bKe65d47e555749b753faaf095c3256ec569bde77d37de66f62ff18bc40d492496|Call-id: ac755ea7e10821aa8174b2e5cd51d9e6|Cseq: 1 INVITE|From: sip:131400xxxx@sip.x;tag=5a541f1b2fd279cd0b8af3be3f67c7cf|To: sip:39329172xxxx@sip.x|Server: SSP v2.0.84|Content-Length: 0