Showing posts with label sed and awk. Show all posts
Showing posts with label sed and awk. Show all posts

Wednesday, 16 February 2011

Sed script for changing different lines into one line

I had a file with different server names on each line but I needed them in a single line so that I can use multiterm as:

multixterm -xc "ssh root@%n" server1 server2 server3 server4 ....

my server file has the names as follows:


server1
server2
server3
server4
.....

......


To achieve this we have many options, some of them are:


Use sed as follows:

sed -n '/:/{x;1!s/\n/ /g;1!p;d;};H;${g;s/\n/ /g;p;}' serverlist


Use awk as:

awk '/: /{if(NR>1)print p;p=$0;next}{p=p FS $0}END{print p}' infile



References:

http://www.unix.com/shell-programming-scripting/147171-combine-multiple-lines-single-line.html