在一个衬管(SED或AWK或其他)的末端用“:”替换行尾

时间:2018-08-10 11:53:04

标签: bash awk sed

我有一个包含SNMP陷阱信息的文件,如下所示:

2018-08-10 13:38:10 gateway [UDP: [192.168.20.254]:53555->[192.168.20.57]:162]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (4294861396) 497 days, 2:10:13.96      SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-MIB::snmpTraps.6        SNMPv2-SMI::enterprises.1824.1.0.0.1 = STRING: "\"This is a string\""   SNMPv2-SMI::enterprises.1824.1.0.0.1 = Counter32: 3345556       SNMPv2-SMI::enterprises.1824.1.0.0.1 = Gauge32: 12343212        SNMPv2-SMI::enterprises.1824.1.0.0.1 = INTEGER: 99      SNMPv2-SMI::enterprises.1824.1.0.0.1 = IpAddress: 100.200.123.65        SNMPv2-SMI::enterprises.1824.1.0.0.1 = OID: iso.2.3.4.5.6.7.8.9 SNMPv2-SMI::enterprises.1824.1.0.0.1 = Timeticks: (2233121) 6:12:11.21

我需要像下面这样单线做

2018-08-10 13:38:10 gateway [UDP: [172.17.2.254]:53555->[172.17.2.57]:162]: DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (4294861396) 497 days, 2:10:13.96      SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-MIB::snmpTraps.6        SNMPv2-SMI::enterprises.1824.1.0.0.1 = STRING: "\"This is a string\""   SNMPv2-SMI::enterprises.1824.1.0.0.1 = Counter32: 3345556       SNMPv2-SMI::enterprises.1824.1.0.0.1 = Gauge32: 12343212        SNMPv2-SMI::enterprises.1824.1.0.0.1 = INTEGER: 99      SNMPv2-SMI::enterprises.1824.1.0.0.1 = IpAddress: 100.200.123.65        SNMPv2-SMI::enterprises.1824.1.0.0.1 = OID: iso.2.3.4.5.6.7.8.9 SNMPv2-SMI::enterprises.1824.1.0.0.1 = Timeticks: (2233121) 6:12:11.21

主要问题是,我需要替换“行尾的冒号并将brak行替换为空格”

我尝试使用正则表达式,例如 sed's /:$ \ n / / g',但是它不起作用。

6 个答案:

答案 0 :(得分:1)

使用awk和三元if-then-else运算符:

awk '{printf "%s%s",$0,/:$/?" ":"\n"}' file

答案 1 :(得分:0)

如果只需要删除换行符和冒号:\n,请尝试以下操作:

$ tr ":\n" " " < file

仅当您只有一个条目时,此方法才有效;否则,它将删除所有结束线制动器并将所有字符串连接起来,这可能是您不想要的,在这种情况下,paste可以做得更好例如:

$ paste -sd " \n" file

它将每两行使用一个空格配对,给出如下输出:

2018-08-10 13:38:10 gateway [UDP: [192.168.20.254]:53555->[192.168.20.57]:162]: DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (4294861396) 497 days, 2:10:13.96
2018-08-10 13:38:10 gateway [UDP: [192.168.20.254]:53555->[192.168.20.57]:162]: DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (4294861396) 497 days, 2:10:13.96

来自paste个人:

-s    Concatenate all of the lines of each separate input file in command line order. 
      The newline character of every line except the last line in each input file is
      replaced with the tab character, unless otherwise specified by the -d option.

答案 2 :(得分:0)

awk '/:$/{printf $0" "} !/:$/{print $0}' inputfile

您可以基于过滤器使用printfprint函数。

答案 3 :(得分:0)

    $datetime1 = new DateTime("2010-06-20");

    $datetime2 = new DateTime("2011-06-22");

    $difference = $datetime1->diff($datetime2);

    echo 'Difference: '.$difference->y.' years, '
        .$difference->m.' months, '
        .$difference->d.' days<br>';

    print_r($difference);

如果您使用的是Mac,请使用(echo alpha;echo "1: foo:";echo "bar1";echo "2: foo:";echo "bar2";echo beta) \ | sed -n '/:$/{N;s/\n/ /};/[^:]/p' alpha 1: foo: bar1 2: foo: bar2 beta (通过gsed)。

答案 4 :(得分:0)

您可以尝试以下正则表达式:

:\s*\n

答案 5 :(得分:0)

使用N选项:

sed 'N; s/\n/ /' file

sed command summary

N

Add a newline to the pattern space, then append the next line of input to the
pattern space.