I want convert this text:
qa-ops01.mysite.com
/dev/mapper/sys-home 58G 26G 30G 47% /home
/dev/mapper/sys-tmp 3.9G 2.3G 1.5G 61% /tmp
qa-ops02.mysite.com
/dev/mapper/sys-home 58G 26G 30G 47% /usr
/dev/mapper/sys-tmp 3.9G 2.3G 1.5G 61% /var
qa-ops03.mysite.com
/dev/mapper/sys-home 58G 26G 30G 47% /lib
/dev/mapper/sys-tmp 3.9G 2.3G 1.5G 61% /etc
to this one:
qa-ops01.mysite.com /dev/mapper/sys-home 58G 26G 30G 47% /home
qa-ops01.mysite.com /dev/mapper/sys-tmp 3.9G 2.3G 1.5G 61% /tmp
qa-ops02.mysite.com /dev/mapper/sys-home 58G 26G 30G 47% /usr
qa-ops02.mysite.com /dev/mapper/sys-tmp 3.9G 2.3G 1.5G 61% /var
qa-ops03.mysite.com /dev/mapper/sys-home 58G 26G 30G 47% /lib
qa-ops03.mysite.com /dev/mapper/sys-tmp 3.9G 2.3G 1.5G 61% /etc
I have used
cat FILE |sed 'N;s/.com\n//'
Is there anyway to achieve this, or should I just write the If… Then…
Thanks everybody for the answers 😀 (you always show me new things :D)
The answer by potong is almost correct; it only handles one server, rather than multiple servers, but the change required is small.
The script is in two parts, identified by the two
-eoptions. The first part identifies server names; those lines contain no spaces (hence/^[^ ]*$/looks for a line with no spaces), and copies the line into the hold space (h) and then deletes it (d) and continues with the next line. The second part of the script is only exercised on lines that contain spaces. It appends the content of the hold space to the pattern space after a newline (G); then it splits the line into ‘everything up to the newline’ and ‘everything after the newline’, and switches them so that the ‘after’ (\2) comes first, then a space, then the ‘before’ (\1).This uses the classic
sedregular expressions; it was tested on Mac OS X (10.7.5) with both the BSDsedand also with GNUsedwithout change. GNUsedhas options such as-rto change the interpretation of regexes which would save you a few backslashes.