一、什么是sed
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作。
二、sed命令格式和选项
·2.1 语法形式
命令
功能描述
a| 在当前行的后面加入一行或者文本。
c| 用新的文本改变或者替代本行的文本。
d
从pattern space位置删除行。
i| 在当前行的上面插入文本。
h
拷贝pattern space的内容到holding buffer(特殊缓冲区)。
H
追加pattern space的内容到holding buffer。
g
获得holding buffer中的内容,并替代当前pattern space中的文本。
G
获得holding buffer中的内容,并追加到当前pattern space的后面。
n
读取下一个输入行,用下一个命令处理新的行而不是用第一个命令。
p
打印pattern space中的行。
P
打印pattern space中的第一行。
q
退出sed。
w file
写并追加pattern space到file的末尾。
!
表示后面的命令对所有没有被选定的行发生作用。
s/re/string
用string替换正则表达式re。
=
打印当前行号码。
替换标记
-
g
行内全面替换,如果没有g,只替换第一个匹配。
p
打印行。
x
互换pattern space和holding buffer中的文本。
y
把一个字符翻译为另一个字符(但是不能用于正则表达式)。
选项
-
-e
允许多点编辑。
-n
取消默认输出。
三、sed实例
# cat testfile
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
实例1.1:如果模板north被找到,sed除了打印所有行之外,还有打印匹配行。
# sed '/north/p' testfile
northwest NW Charles Main 3.0 .98 3 34
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
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
实例1.2:-n选项取消了sed的默认行为。在没有-n的时候,包含模板的行被打印两次,但是在使用-n的时候将只打印包含模板的行。
# sed -n '/north/p' testfile
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
实例:列出第5-7行
# nl testfile |sed -n '5,7p'
5 southeast SE Patricia Hemenway 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
实例2.1:删除第三行,其他行默认输出到屏幕。
# nl testfile |sed '3d'
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
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
实例2.2:删除2~5行
# nl testfile |sed '2,5d'
1 northwest NW Charles Main 3.0 .98 3 34
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
8 north NO Margot Weber 4.5 .89 5 9 9
central CT Ann Stephens 5.7 .94 5 13
实例2.3:从第三行删除到最后一行,其他行被打印。$表示最后一行。
# nl testfile |sed '3,$d'
1 northwest NW Charles Main 3.0 .98 3 34
2 western WE Sharon Gray 5.3 .97 5 23
实例2.4:删除最后一行,其他行打印。
# nl testfile |sed '$d'
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
实例2.5:删除所有包含north的行,其他行打印。
# nl testfile |sed '/north/d'
2 western WE Sharon Gray 5.3 .97 5 23
3 southwest SW Lewis Dalsass 2.7 .8 2 18
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Hemenway 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
9 central CT Ann Stephens 5.7 .94 5 13
参考文献:
本文详细介绍了sed命令的基本概念、语法形式、常用选项,并通过实例演示如何利用sed进行文件行的操作,如查找、替换、删除和插入等。重点讲解了如何使用-n选项控制输出行为和多点编辑功能。
1102

被折叠的 条评论
为什么被折叠?



