0

I have a file with SNMP traps info just like below:

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.

Grzegorz
  • 33
  • 1
  • 4
  • 1
    I think [this post](https://stackoverflow.com/a/1252191/4741225) can help you – Alin Aug 10 '18 at 12:04
  • Why does the IP address change? And the text says to replace "colon and newline with a space", but the example leaves the colon in place...? – Benjamin W. Aug 10 '18 at 13:43

6 Answers6

1

Using awk and ternary if-then-else operator:

awk '{printf "%s%s",$0,/:$/?" ":"\n"}' file
oliv
  • 12,690
  • 25
  • 45
0

If just need to remove the line break and colon :\n with a space try this:

$ 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 -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.
nbari
  • 25,603
  • 10
  • 76
  • 131
0
awk '/:$/{printf $0" "} !/:$/{print $0}' inputfile

You can use printf and print functions based on the filter.

P....
  • 17,421
  • 2
  • 32
  • 52
0
(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).

keithpjolley
  • 2,089
  • 1
  • 17
  • 20
0

You can try this regex:

:\s*\n
Ayush
  • 1
  • 2
0

Use N option:

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.
GAD3R
  • 4,317
  • 1
  • 23
  • 34