linux sed比较文件内容,Linux文件比较三剑客(awk/grep/sed)之sed

本文详细介绍了sed命令的基本概念、语法形式、常用选项,并通过实例演示如何利用sed进行文件行的操作,如查找、替换、删除和插入等。重点讲解了如何使用-n选项控制输出行为和多点编辑功能。
部署运行你感兴趣的模型镜像

一、什么是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

参考文献:

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值