views:

257

answers:

2

Hi,

With a simple bash script i generate a text file with many entrys like this:

192.168.1.1
hostname1
192.168.1.2
hostname2
192.168.1.3
hostname3

Now i want to reformat this file, that it looks like this:

192.168.1.1 hostname1
192.168.1.2 hostname2
192.168.1.3 hostname3

Some ideas to solve this? Sed maybe?

Thanks for help and best regards. :)

+8  A: 
$ sed '$!N;s/\n/ /' infile
192.168.1.1 hostname1
192.168.1.2 hostname2
192.168.1.3 hostname3
Tim
Would you please explain how it come to be?
NawaMan
thanks. exactly what i search. :)
fwa
@NawaMan: If you mean "how does it work?" then: If not (!) the last line ($) then append the next line (N) and replace the newline between them with a space (s/\n/ /) and repeat starting with the next line (which will be the 3rd, 5th, etc.).
Dennis Williamson
:-D Thanks. The just too crypt-ed.
NawaMan
+6  A: 

Here's a shell-only alternative:

while read first; do read second; echo "$first $second"; done
Jukka Matilainen