sed命令使用总结
sed usage
语法结构
sed [options] '起始位置,结束为止[ai添加,d删除,p打印,s替换,r从文件读,w写入文件]' files
注:p与-n配合使用
1:定址方式
用法:
行号定址:
sed [options] 'r1,r2[ai添加,d删除,p打印,s替换]' files
正则定址:
sed [options] '/起始位置/,/结束位置/[ai添加,d删除,p打印,s替换]' files
混用:
sed [options] 'r1,/结束位置/[ai添加,d删除,p打印,s替换]' files
sed [options] '/起始位置/,r2[ai添加,d删除,p打印,s替换]' files
行号定址:
#打印1-3行
cat test.txt | sed -n '1,3p'
#删除1-3行
cat test.txt | sed '1,3d'
正则定址:
#显示以Tom开头的行
cat test.txt | sed -n '/^Tom/p'
#通过正则来匹配行范围,从匹配到第一个ftp开始,到之后匹配到的第一个rpc结束的所有行,没有rpc会匹配到结尾
cat -n /etc/passwd |sed -n '/ftp/,/rpc/p'
2:a添加 #i和a的用法相同,i表示在匹配行的前面插入内容
#在指定行号的位置插入新行
#在第三行后加入'===============this is a test'
cat test.txt | sed '3a===============this is a test'
#在匹配到Archie的行后插入新行===============================
cat test.txt | sed '/Archie/a==============================='
#插入多行
cat /etc/passwd |sed '5a \
> 324\
> 534\
> 535\'
3:d删除
#删除包含Archie的行
cat test.txt | sed '/Archie/d'
#删除5,8行
cat -n /etc/passwd |sed '5,8d'
#删除第一行/末行
cat -n /etc/passwd |sed '^d'
cat -n /etc/passwd |sed '$d'
4:s替换
#将5-7行的:替换为-
cat -n /etc/passwd |sed '5,7s#:#-#g'
#不加g表示仅替换每行匹配到的第一个
cat -n /etc/passwd |sed '5,7s#:#-#'
#替换包含root行的:为-
cat -n /etc/passwd |sed '/root/s#:#-#g'
5:-e实现多次编辑
#先删除包含root的行,再替换符号
cat -n /etc/passwd |sed -e '/root/d' -e 's/:/#/g'
6:反选实现
cat -n /etc/passwd |sed '/root/!d'
进阶用法:
#h用法(个人理解:剪切板)
% sed -e '/northeast/h' -e '$G' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
northeast NE AM Main Jr. 5.1 .94 3 13
在 sed 处理的文件的时候,每一行都被保存在一个叫作模式空间(pattern space)的临时缓冲区
中。除非行被删除或者输出被取消,否则所有被处理过的行都将打印在屏幕上。接着模式空间被清
空,并存入新的一行等待处理。在这个例子中,包含模板的 northeast 行被找到,并被放入模式空间
中,h 命令将其复制并存入一个称为保持缓冲区(holding buffer)的特殊缓冲区内。在第二个 sed 结
构中,当达到最后一行后,G 命令告诉 sed 从保持缓冲区中取得该行,然后把它放回到模式空间中,
且追加到现在已经存在于模式空间中的行的末尾——在这个例子中,就是追加到最后一行。简单来
讲:任何包含模板 northeast 的行都将被复制并追加到该文件的末尾。
#h和g的配合使用(个人理解:g将剪切板内容插入匹配的位置)
% sed –e '/WE/{h;d; } '-e '/CT/{G; }' datafile
northwest NW Charles Main 3.0 .98 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
western WE Sharon Gray 5.3 .97 5 23
说明
如果模板 WE 在某一行中被匹配,h 命令将使得该行被从模式空间中复制并放入保持缓冲区中。
当存储到保持缓冲区中以后,该行还可以被重新利用(通过 G 命令或者 g 命令)。在这个例子中,模
板 WE 被匹配以后,该行被放入保持缓冲区内。命令 d 在模式空间中删除该行。第二个命令搜索 CT,
一旦被找到,(通过 G 命令)sed 就从保持缓冲区中取回行,并追加到当前模式空间中行的末尾。简单
地说:包含 WE 是行被移动并追加到包含 CT 的行的后面 (参考“保持和互换:h 命令和 x 命令”)。
#h和x配合使用(个人理解:x将剪切板内容替换匹配到的内容)
% sed –e '/patricia/h' -e '/Margot/x' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
southeast SE Patricia Hemenway 4.0 .7 4 17
central CT Ann Stephens 5.7 .94 5 13
说明
命令 x 表示互换模式空间和保持缓冲区的内容。当模板 patricia 被匹配,该行将被保存到保持缓
冲区中。当包含 Margot 的行被找到,模式空间将与保持缓冲区交换彼此的行。简单地说,就是包含
Margot 的行被包含 patricia 的行替换了。

浙公网安备 33010602011771号