您的位置:首页 > 其它

sed高级使用

2015-09-17 20:46 316 查看
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]#


6、单行next的命令
[root@digitcube-test1 tmp]# cat data1
the quick green fox jumps over the lazy cat1
the quick green fox jumps over the lazy cat2
the quick green fox jumps over the lazy cat3
the quick green fox jumps over the lazy cat4
the quick green fox jumps over the lazy cat5
the quick green fox jumps over the lazy cat6

[root@digitcube-test1 tmp]# sed '/cat2/{n;d}' data1
the quick green fox jumps over the lazy cat1
the quick green fox jumps over the lazy cat2
the quick green fox jumps over the lazy cat4
the quick green fox jumps over the lazy cat5
the quick green fox jumps over the lazy cat6


7、合并文本行
N的命令将下一行合并到那一行,然后用替换命令s将换行符转换成空格
[root@digitcube-test1 tmp]# sed '/cat1/{N;s/\n/ /}' data1
the quick green fox jumps over the lazy cat1  the quick green fox jumps over the lazy cat2
the quick green fox jumps over the lazy cat3
the quick green fox jumps over the lazy cat4
the quick green fox jumps over the lazy cat5
the quick green fox jumps over the lazy cat6


8、删除结尾空白行
data1末尾多两行空白行

[root@digitcube-test1 tmp]# vim sed1.sh

#!/bin/bash
#

sed '{
:start
/^\n*$/{$d;N;b start}
}' $1

[root@digitcube-test1 tmp]# sh sed1.sh data1
the quick green fox jumps over the lazy cat1
the quick green fox jumps over the lazy cat2
the quick green fox jumps over the lazy cat3
the quick green fox jumps over the lazy cat4
the quick green fox jumps over the lazy cat5
the quick green fox jumps over the lazy cat6

9、模式替代
and符号(&)用来代表替换命令中匹配的模式
[root@logicserver tmp]# echo "The cat sleeps in his hat."|sed 's/.at/"&"/g'
The "cat" sleeps in his "hat".
当模式匹配了了单词cat,“cat”就出现在替换后的单词里,当它匹配了hat,“hat”就出现在替换后单词里

替换单独的单词
sed编辑器用圆括号来定义替换模式的子字符串,替代字符由反斜线和数字组成,数字表明字符串模块的位置,sed的编辑器给第一个模块分配字符
\1,给第二个模块分配字符\2,依此类推
[root@logicserver tmp]# echo "my lover is pangfeng"|sed '
> s/my \(lover\)/qingyun \1/'
qingyun lover is pangfeng

10、在两个或多个字符串模式中插入文本
[root@node3 conf]# echo "hi tom what are you doing"|sed '{
> :start
> s/hi \(tom\)/hello \1 miki/
> t start
> }'
hello tom miki what are you doing
[root@node3 conf]# echo "121234"|sed '{
:start
s/\(.*[0-9]\)\([0-9]\{3\}\)/\1.\2/
t start
}'
121.234

本文出自 “散人” 博客,请务必保留此出处http://zouqingyun.blog.51cto.com/782246/1695822
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: