sed两行并一行

之前看到“国产0与1”的多行合并一行
[url]http://qq164587043.blog.51cto.com/261469/58754[/url]
awk来实现,耶
system_finger.txt内容为:
fc7:2.6.23.1
Fedora release 7 (Moonshine)
查找下资料知道用awk可以完成这项艰巨的任务。
awk '{if(NR%2==0){printf $0 "\n"}else{printf "%s:",$0}}' > system_fingerprint.txt
cat  system_fingerprint.txt 看看
fc7:2.6.23.1:Fedora release 7 (Moonshine)
 
用sed来实现也是一个办法
 
[root@rhelas5 ~]# sed 'N;s/\n/ :/' system_finger.txt
fc7:2.6.23.1 :Fedora release 7 (Moonshine)
 
N这个参数我的理解是,第一行是fc7:2.6.23.1,加上N这个参数就把第二行Fedora release 7 (Moonshine)也合并到第一行,中间是用\n分隔的
 
假如system_finger.txt文件不止两行,输出又该如何
system_finger.txt内容为:
fc7:2.6.23.1
Fedora release 7 (Moonshine)
rhel
as5
[root@rhelas5 ~]# sed 'N;s/\n/ :/' system_finger.txt
fc7:2.6.23.1 :Fedora release 7 (Moonshine)
rhel :as5
 
因为sed处理是单行处理,结果就是这样


 

 
 

你可能感兴趣的:(shell,职场,休闲)