首页 > 其他 > 详细

sed高级使用

时间:2015-09-17 21:41:48      阅读:238      评论:0      收藏:0      [点我收藏+]

1、名reverse脚本用sed编辑器脚本反转数据流的文本行

                h 将模式空间复制到保持空间
                H 将模式空间附加到保持空间
                g 将保持空间复制到模式空间
                G 将保持空间附加到模式空间
                x  交换模式空间和保持空间的内容

                p  打印模式空间

 

                n  提取数据流的下一行

    

[root@logicserver tmp]# vim reverse.sh
#!/bin/bash
#
sed -n ‘{
1!G
h
$p
}‘ $1
~       
[root@logicserver tmp]# cat data1
the quick brown fox jumps over the lazy dog1
a quick brown fox jumps over the lazy dog2
the quick brown fox jumps over the lazy dog3
the quick brown fox jumps over the lazy dog4
the quick brown fox jumps over the lazy dog5
the quick brown fox jumps over the lazy dog6


[root@logicserver tmp]# sh reverse.sh data1
the quick brown fox jumps over the lazy dog6
the quick brown fox jumps over the lazy dog5
the quick brown fox jumps over the lazy dog4
the quick brown fox jumps over the lazy dog3
a quick brown fox jumps over the lazy dog2
the quick brown fox jumps over the lazy dog1

2、加位行间距

[root@logicserver tmp]# sed ‘G‘ data1
the quick brown fox jumps over the lazy dog1
a quick brown fox jumps over the lazy dog2
 
the quick brown fox jumps over the lazy dog3
the quick brown fox jumps over the lazy dog4
the quick brown fox jumps over the lazy dog5
the quick brown fox jumps over the lazy dog6

如果末尾不想这个空白行

[root@logicserver tmp]# sed ‘$!G‘ data1
the quick brown fox jumps over the lazy dog1
a quick brown fox jumps over the lazy dog2
 
the quick brown fox jumps over the lazy dog3
the quick brown fox jumps over the lazy dog4
the quick brown fox jumps over the lazy dog5
the quick brown fox jumps over the lazy dog6

3、删除已有空白行

[root@logicserver tmp]# sed ‘/^$/d‘ data1
the quick brown fox jumps over the lazy dog1
a quick brown fox jumps over the lazy dog2
the quick brown fox jumps over the lazy dog3
the quick brown fox jumps over the lazy dog4
the quick brown fox jumps over the lazy dog5
the quick brown fox jumps over the lazy dog6

4、给文件中的行编号

 [root@logicserver tmp]# sed ‘=‘ data1
1
the quick brown fox jumps over the lazy dog1
2
a quick brown fox jumps over the lazy dog2
3
4
the quick brown fox jumps over the lazy dog3
5
the quick brown fox jumps over the lazy dog4
6
the quick brown fox jumps over the lazy dog5
7
the quick brown fox jumps over the lazy dog6

这样可观性不是很好

[root@logicserver tmp]# sed ‘=‘ data1 | sed ‘N;s/\n/ /‘
1 the quick brown fox jumps over the lazy dog1
2 a quick brown fox jumps over the lazy dog2
3 the quick brown fox jumps over the lazy dog3
4 the quick brown fox jumps over the lazy dog4
5 the quick brown fox jumps over the lazy dog5
6 the quick brown fox jumps over the lazy dog6

5、删除空白行

 [root@logicserver tmp]# sed ‘/^$/d‘ data1
the quick brown fox jumps over the lazy dog1
a quick brown fox jumps over the lazy dog2
the quick brown fox jumps over the lazy dog3
the quick brown fox jumps over the lazy dog4
the quick brown fox jumps over the lazy dog5
the quick brown fox jumps over the lazy dog6
[root@logicserver tmp]#

本文出自 “散人” 博客,请务必保留此出处http://zouqingyun.blog.51cto.com/782246/1695822

sed高级使用

原文:http://zouqingyun.blog.51cto.com/782246/1695822

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!