a very basic sed one-liner...that isn't working :-(

Greetings all. :slight_smile:

I would like to use sed to join all non-blank lines together in a particular file. I was thinking I could do this by simply replacing the terminating, new-line character on every line which is not blank, but I must be missing something in my sed line:

$ sed '/^.*$/s/\n$/\t/' InputFile

The solution here needs to leave blank lines untouched as the file is setup so blank lines mark the beginning of a new block of related lines, like so:

 
ROUTER-1-1
Virtual-Access1 is up, line protocol is up
Hardware is Virtual Access interface
MTU 1500 bytes, BW 256 Kbit, DLY 20000 usec,
reliability 255/255, txload 5/255, rxload 4/255
Encapsulation PPP, LCP Open
 
Dialer0 is up, line protocol is up (spoofing)
Hardware is Unknown
Internet address is 192.168.1.1/32
MTU 1500 bytes, BW 56 Kbit, DLY 20000 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation PPP, loopback not set
 
Virtual-Access1 is up, line protocol is up
Hardware is Virtual Access interface
MTU 1500 bytes, BW 256 Kbit, DLY 20000 usec,
reliability 255/255, txload 5/255, rxload 4/255
Encapsulation PPP, LCP Open
 
Loopback0 is up, line protocol is up
Hardware is Loopback
Description: Version 1.2
Internet address is 10.0.1.1/32
MTU 1514 bytes, BW 8000000 Kbit, DLY 5000 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation LOOPBACK, loopback not set
 

Essentially, each original line in a block will end-up as a tab-delimited field on a single line of output; Something like this:

 
ROUTER-1-1 Virtual-Access1 is up, line protocol is up Hardware is Virtual Access interface MTU 1500 bytes, BW 256 Kbit, DLY 20000 usec, reliability \ ...
 
Dialer0 is up, line protocol is up (spoofing) Hardware is Unknown Internet address is 192.168.1.1/32 MTU 1500 bytes, BW 56 Kbit, DLY 20000 usec, \ ...
 
Virtual-Access1 is up, line protocol is up Hardware is Virtual Access interface MTU 1500 bytes, BW 256 Kbit, DLY 20000 usec, reliability \ ...
 
Loopback0 is up, line protocol is up Hardware is Loopback Description: Version 1.2 Internet address is 10.0.1.1/32 MTU 1514 bytes, BW 8000000  \ ...
 

Please correct my misguided ways and also let me know if you have a more eligant solution to this problem.

Thanks!
--Steve
(Linux newbie...in case you couldn't tell)

p.s.
For anyone out there reading this on a small monitor, I'm sorry if my lines of example output are excessively long. :frowning:

I thought about using shorter (...and more boring) lines of input, but in the end, thought it might be more interesting and instructive if I included the real-world data with which I'm working.

If I made the wrong call here, let me know and I'll refrain from including long lines in my future posts.

You have to use N command in sed to append the next line to the pattern space.

$ sed '
/^$/b
:loop
$! {
N
/\n *$/!b loop
}
s/\n\([^ ]\)/\t\1/g' a

ROUTER-1-1      Virtual-Access1 is up, line protocol is up      Hardware is Virtual Access interface    MTU 1500 bytes, BW 256 Kbit, DLY 20000 usec,    reliability 255/255, txload 5/255, rxload 4/255 Encapsulation PPP, LCP Open

Dialer0 is up, line protocol is up (spoofing)   Hardware is Unknown     Internet address is 192.168.1.1/32      MTU 1500 bytes, BW 56 Kbit, DLY 20000 usec,    reliability 255/255, txload 1/255, rxload 1/255  Encapsulation PPP, loopback not set

Virtual-Access1 is up, line protocol is up      Hardware is Virtual Access interface    MTU 1500 bytes, BW 256 Kbit, DLY 20000 usec,    reliability 255/255, txload 5/255, rxload 4/255 Encapsulation PPP, LCP Open

Loopback0 is up, line protocol is up    Hardware is Loopback    Description: Version 1.2        Internet address is 10.0.1.1/32 MTU 1514 bytes, BW 8000000 Kbit, DLY 5000 usec, reliability 255/255, txload 1/255, rxload 1/255 Encapsulation LOOPBACK, loopback not set

Explanation

1. Keeps adding the non-empty line. and replace new line with tab.
2. When empty line/(lines with only spaces) comes, just print the pattern space and start next cycle. i.e s/\n\([^ ]\)/\t\1/g' a this substitution will not happen for empty lines.

Don't know for sed but if awk is ok for you:

awk '{ORS=(/^ $/)?"\n\n":" "}1' file

with GNU sed

:y
s/\n/ /
N
/\n *$/!by