标签:sed高级使用
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