简体   繁体   English

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

[英]Replace end of line with ':' at the end to one liner (SED or AWK or other)

I have a file with SNMP traps info just like below: 我有一个包含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

I need to do it a one-liner like below 我需要像下面这样的单线

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

The main problem is, that I need replace 'colon on end of line and brak line to a space' 主要问题是,我需要替换“行末尾的冒号,并将布拉克行替换为空格”

I try to use regexp, like sed 's/:$\\n/ /g' , but it doesn't work. 我尝试使用regexp,例如sed's /:$ \\ n / / g' ,但是它不起作用。

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

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

If just need to remove the line break and colon :\\n with a space try this: 如果只需要用空格删除换行符和冒号:\\n ,请尝试以下操作:

$ tr ":\n" " " < file

This works only if you have a single entry, otherwise, it will remove all the ending line brakes and concatenate all your strings something you may not want, in that case, paste could do a better job, for example: 仅当您只有一个条目时,此方法才有效;否则,它将删除所有结尾行制动器并将所有字符串连接在一起,这可能是您不想要的,在这种情况下, paste可以做得更好,例如:

$ paste -sd " \n" file

It will pair every two lines using an space, giving an output like this: 它将使用空格将每两行配对,给出如下输出:

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

From the paste man: 粘贴人员:

-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.
awk '/:$/{printf $0" "} !/:$/{print $0}' inputfile

您可以使用基于过滤器的printfprint功能。

(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

If you are using a mac use gsed (via brew install gnu-sed ). 如果您使用的是Mac,请使用gsed (通过brew install gnu-sed )。

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

:\s*\n

Use N option: 使用N选项:

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

sed command summary sed命令摘要

N

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM