bash的合并线在一个文件 [英] bash to merge lines in a file

查看:91
本文介绍了bash的合并线在一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换这样的文字:

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

这一个:

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

我用

cat FILE |sed 'N;s/.com\n//'

反正有没有做到这一点,或者我应该只写如果...那么...

Is there anyway to achieve this, or should I just write the If... Then...

谢谢大家的答案:D(你总是告诉我新的东西:D)

Thanks everybody for the answers :D (you always show me new things :D)

推荐答案

=HTTP ://stackoverflow.com/users/967492/potong>波东几乎是正确的;它只能处理一个服务器,而不是多个服务器,但所需要的变化是小的。

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
$

该脚本是两部分,由两个 -e 选项标识。第一部分标识的服务器名称;这些线路不能包含空格(因此 / ^ [^] * $ / 查找无空格线),并复制该行到保留空间( ^ h ),然后删除它( D ),并在下一行继续。脚本的第二部分只行使包含空格的线条。追加保持空间模式空间的内容换行符后();然后它将该行成一切都交给了换行和一切换行后,并切换它们,这样的后( \\ 2 )是第一位的,然后一个空格,然后在之前( \\ 1 )。

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).

本采用了经典的 SED 常规EX pressions;它是在Mac OS X(10.7.5),同时与BSD SED 并与GNU SED 测试无变化。 GNU SED 的选项,例如 -r 来改变正则表达式的相互pretation这将节省你几个反斜杠。

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.

这篇关于bash的合并线在一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆