bash to merge lines in a file

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 :D (you always show me new things :D)

Answers


The answer by potong is almost correct; it only handles one server, rather than multiple servers, but the change required is small.

$ sed -e '/^[^ ]*$/{h;d;}' -e 'G; s/\(.*\)\n\(.*\)/\2 \1/' data
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
$

The script is in two parts, identified by the two -e options. 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 sed regular expressions; it was tested on Mac OS X (10.7.5) with both the BSD sed and also with GNU sed without change. GNU sed has options such as -r to change the interpretation of regexes which would save you a few backslashes.


I don't know much about sed, but in AWK:

awk 'NR==1{prefix=$0;next} {print prefix, $0}' file
Update

If that is the case, look for lines with only one field (column), use it for prefix. That means, the only change to the above script is NF (number of fields) in place of NR (number of records, or lines).

awk 'NF==1{prefix=$0;next} {print prefix, $0}' file

This might work for you (GNU sed):

sed -r '1{h;d};G;s/(.*)\n(.*)/\2 \1/' file
  • 1{h;d} save the first line in the hold space (HS) and then delete the pattern space (PS).
  • G on all subsequent lines append a newline and the HS to PS.
  • s/(.*)\n(.*)/\2 \1/ re-arrange the PS and remove the introduced newline.

Since posting the answer the original question was changed, to take this into account:

sed -r '/%/!{h;d};G;s/(.*)\n(.*)/\2 \1/' file

This handles multiple servers by saving lines in the HS which do not contain %.


try this one:

awk '!/^\/dev/{header=$0;next} {print header, $0}' input.txt

alternatively (functionally identical, but a bit easier to read and understand, IMO):

awk '{if($0 !~ /^\/dev/){header=$0}else{print header, $0}}' input.txt

awk '{if($0~/mysite.com/){x=$0}else{print x,$0}}' your_file

tested here


Another way using awk is as follows:

awk '{if ($1 ~ /mysite.com/){a= $0 } if ($1 ~ /dev/){ print a" "$0}}' temp.txt

  1. a = $0 copies the line to a if that contains mysite.com
  2. If second lines contains /dev then it joins both lines

or other variation of potong sed

sed -re '/mysite.com/{h;d};G;s/(.*)\n(.*)/\2 \1/' temp.txt


Need Your Help

Where to put automapper configuration for webservice?

web-services configuration automapper

I read that a web service does not have a global.asax to configure global settings (like the automapper configuration). So my question is:

how to get urls from google query?

python html url google-search

hi guys i've try to urls from google but it's return 0 urls !

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.