Linux运维养成记-sed 编辑器

1. 初识 sed 编辑器

sed 编辑器称为流编辑器,流编辑器会在编辑处理数据前基于预先提供的一组规则来编辑数据流。
sed 编辑器可以根据命令来处理数据流中的数据,这些命令可以是从命令行中输入,也可以是从文本文件中输入。sed编辑器工作流程如下:

  • 一次从输入中读取一行数据
  • 根据所提供的命令匹配数据
  • 根据命令操作所匹配的数据
  • 将处理后的数据输出 STDOUT
  • sed处理数据默认不会再源文件中修改,除非使用-i参数,不建议使用该参数

sed格式如下:
sed optition script file

sed所提供的选项如下:

选项 解释
-e 需要在 sed 上执行多条命令时用-e,命令之前用“;”隔开
-f file 将命令保存至 file 文件里,使用-f 可读取 file 里的命令加以使用
-n 取消默认输出

例:

[root@localhost tmp]# cat data.txt 
one two three four five six
one two three four five six
one two three four five six
one two three four five six
one two three four five six

#将每行第一次出现的 six 改为 6
[root@localhost tmp]# sed 's/six/6/' data.txt 			
one two three four five 6
one two three four five 6
one two three four five 6
one two three four five 6
one two three four five 6

#将每行第一次出现的 six 改为 6,将 one 改为 1,这里使用-e 选项执行多个命令
[root@localhost tmp]# sed -e 's/six/6/;s/one/1/' data.txt 	
1 two three four five 6
1 two three four five 6
1 two three four five 6
1 two three four five 6
1 two three four five 6

#将命令保存在 script 里面
[root@localhost tmp]# cat script1 
s/one/1/
s/two/2/

#使用 script 里面的命令作为 sed 命令
[root@localhost tmp]# sed -f script1 data.txt 
1 2 three four five six
1 2 three four five six
1 2 three four five six
1 2 three four five six
1 2 three four five six

2. sed 编辑器基础

2.1 替换标记

sed有5 种替换标记:

  • s/ A/B/:将每一行的第一个 A 用 B 替换
  • s/A/B/2:将每一行第二个 A 用 B 替换,后面数字为替换第几处的模式匹配的地方
  • s/A/B/g:将所有的 A 用 B 替换
  • s/A/B/p:表明原先的内容要打印出来
  • s/A/B/w file:将替换的结果保存到 file 里面

例:

[root@localhost tmp]# cat data2
This is the first line of the test data
This is the second line of the test data
This is the third line of the test data

#将每行第二个 the 替换成 a
[root@localhost tmp]# sed s/the/a/2 data2
This is the first line of a test data
This is the second line of a test data
This is the third line of a test data

#将所有 the 替换成 a
[root@localhost tmp]# sed s/the/a/g data2
This is a first line of a test data
This is a second line of a test data
This is a third line of a test data

#-p与-n 连用打印匹配行
[root@localhost tmp]# sed -n s/first/1/p data2
This is the 1 line of the test data

2.2 使用寻址

使用方式:[address] command
例:

[root@localhost tmp]# cat data2
This is the first line of the test data
This is the second line of the test data
This is the third line of the test data

#打印第二行
[root@localhost tmp]# sed -n 2p data2
This is the second line of the test data

#替换第一行至第三行的 This 为 It
[root@localhost tmp]# sed '1,3s/This/It/' data2
It is the first line of the test data
It is the second line of the test data
It is the third line of the test data

#替换 2 至最后一行的 This 为 It
[root@localhost tmp]# sed '2,$s/This/It/' data2
This is the first line of the test data
It is the second line of the test data
It is the third line of the test data

#找到有 second 的行,将 test 替换为 tttt
[root@localhost tmp]# sed '/second/s/test/tttt/' data2
This is the first line of the test data
This is the second line of the tttt data
This is the third line of the test data

2.3 删除行

删除行使用选项d
例:

[root@localhost tmp]# cat data2
This is the first line of the test data
This is the second line of the test data
This is the third line of the test data


#删除第二行
[root@localhost tmp]# sed '2d' data2
This is the first line of the test data
This is the third line of the test data

#删除 2 至最后一行
[root@localhost tmp]# sed '2,$d' data2
This is the first line of the test data

#删除有 third 的行
[root@localhost tmp]# sed '/third/d' data2
This is the first line of the test data
This is the second line of the test data

2.4 插入附加文本

sed 编辑器可以对文本进行插入或附加文本:

类别 命令 解释
插入 insert i 在指定行前增加一新行
附加 append a 在指定一行后增加一行

例:

[root@localhost tmp]# cat data2
This is the first line of the test data
This is the second line of the test data
This is the third line of the test data

#在第二行前增加一行
[root@localhost tmp]# sed '2iThis is a new line' data2
This is the first line of the test data
This is a new line
This is the second line of the test data
This is the third line of the test data

#在第二行后增加一行
[root@localhost tmp]# sed '2aThis is a new line' data2
This is the first line of the test data
This is the second line of the test data
This is a new line
This is the third line of the test data

2.5 修改行

修改行用c参数,例如下:

[root@localhost tmp]# cat data2
This is the first line of the test data
This is the second line of the test data
This is the third line of the test data

#将第二行更改
[root@localhost tmp]# sed '2cThis line update' data2
This is the first line of the test data
This line update
This is the third line of the test data

2.6 转换命令

命令转换用y选项,例:

[root@localhost tmp]# cat data3
This is 1
This is 2
This is 3
This is 4
This is 5

#将 1,2,3 转换为 7,8,9
[root@localhost tmp]# sed 'y/123/789/' data3
This is 7
This is 8
This is 9
This is 4
This is 5
#注意如果前者和后者字符不相同会报错

2.7 从文件中读取数据

从文件中读取数据用选项r,它允许将文本中的内容插入到数据流中,例:

[root@localhost tmp]# cat data2
This is the first line of the test data
This is the second line of the test data
This is the third line of the test data

[root@localhost tmp]# cat data3
This is 1
This is 2
This is 3
This is 4
This is 5

#将 data3 中的数据插入到 data2 的第二行后面
[root@localhost tmp]# sed '2r data3' data2 
This is the first line of the test data
This is the second line of the test data
This is 1
This is 2
This is 3
This is 4
This is 5
This is the third line of the test data

3. sed 的正则表达式

3.1 锚字符

锚字符有两种:

锚字符 解释
^ 锁定在行首
$ 锁定在行尾

3.1.1 锁定行首

锁定行首用托字符^,使用时必须将之放于正则表达式的模式前面,例:

[root@localhost tmp]# echo "My name is xxx" |sed -n '/^name/p'
[root@localhost tmp]# 
[root@localhost tmp]# echo "My name is xxx" |sed -n '/^My/p'
My name is xxx
[root@localhost tmp]#

如果将^字符放到非开头位置,那么它就和普通字符一样取匹配,如:

[root@localhost tmp]# echo "My^ name is xxx" |sed -n '/My^/p'
My^ name is xxx

3.1.2 锁定行尾

锁定行尾用字符$,该字符必须处于文本模式之后去匹配结束文本模式,例:

[root@localhost tmp]# echo "this is my job" |sed -n '/job$/p'
this is my job
[root@localhost tmp]#

3.1.2 组合锚字符

当我们需要匹配一个空行时,可以使用组合起来的锚字符:

[root@localhost tmp]# cat data3
This is 1

This is 2
This is 3
This is 4
This is 5

# 匹配空行后删除
[root@localhost tmp]# sed '/^$/d' data3
This is 1
This is 2
This is 3
This is 4
This is 5

3.1.3 点号字符

点号字符.表示匹配任意一个字符,例:

[root@localhost tmp]# echo "hello world" |sed -n '/wor.d/p'
hello world

3.1.4 字符组匹配

字符组匹配用符号[],表示希望匹配方括号中的一个字符,例:

#匹配以 H 或者 h 后接 ello 的行
[root@localhost tmp]# echo "hello world" |sed -n '/[Hh]ello/p'
hello world

[root@localhost tmp]# echo "Yes" |sed -n '/[Yy][Ee][Ss]/p'
Yes

3.1.5 区间

匹配区间用-,例:

[root@localhost tmp]# echo "65535" |sed -n '/[0-9][0-9][0-9][0-9][0-9]/p'
65535

[root@localhost tmp]# cat data3
This is 1
This is 2
This is 3
This is 4
This is 5
[root@localhost tmp]# sed -n '/^[A-z]his/p' data3
This is 1
This is 2
This is 3
This is 4
This is 5

3.1.6 星号*

匹配文本中出现 0 次或多次的字符用*,例:

[root@localhost tmp]# echo "hello" |sed -n '/hell*o/p'
hello
[root@localhost tmp]# echo "hello" |sed -n '/hel*o/p'
hello

3. sed 进阶

3.1 多行命令

sed编辑器包含三个命令:

  • N :将数据流中下一行的文本加到当前行创建一个多行组来处理
  • D :删除多行组中的一行
  • P :打印多行组中的一行

3.1.1 单行 next命令

单行 next 命令用n,例:

[root@localhost tmp]# cat data2
This is the first line of the test data
This is the second line of the test data
This is the third line of the test data

[root@localhost tmp]# sed '/second/{n;d}' data2
This is the first line of the test data
This is the second line of the test data
#n命令让 sed 编辑器移动到匹配行的下一行,也就是有 third 那一行,然后执行 d 删除命令

3.1.2 多行 next 命令

多行 next 命令用N,例:

[root@localhost tmp]# sed '/second/{N;d}' data2
This is the first line of the test data
#N命令让 sed 编辑器移动到匹配行的下一行,也就是有 third 那一行,两行合并为一行然后执行 d 删除命令

3.1.3 多行删除命令

多行删除命令用D,例:

[root@localhost tmp]# cat data2
This is the first line of the test data
This is the second line of the test data
This is the third line of the test data

[root@localhost tmp]# sed '/second/{N;D}' data2
This is the first line of the test data
This is the third line of the test data
#两行合并为一行然后执行 D 删除命令,删除匹配两行中的第一行

3.2 模式空间与保持空间

模式空间与保持空间都是sed的缓冲区域,在sed执行命令时,读取的每一行会保存在模式空间然后再进行处理,而在处理模式空间时,保持空间可以用来临时保存一些行。
下列命令可以用来处理保持空间:

命令 描述
h 将模式空间复制到保持空间
H 将模式空间附加到保持空间
g 将保持空间复制到模式空间
G 将保持空间附加到模式空间
x 交换保持空间和模式空间

例:

[root@localhost tmp]# cat data2
This is the first line of the test data
This is the second line of the test data
This is the third line of the test data

[root@localhost tmp]# sed -n '/first/{h;p;n;p;g;p}' data2
This is the first line of the test data
This is the second line of the test data
This is the first line of the test data
#h 将第一行复制到保持空间
#p 打印此时模式空间内容
#n 提取第一行的下一行,放到模式空间
#p 打印此时模式空间内容
#g 将保持空间复制到模式空间中
#p 打印此时模式空间内容

3.3 排除命令

排除命令用!,例:

[root@localhost tmp]# cat data2
This is the first line of the test data
This is the second line of the test data
This is the third line of the test data

#打印不含 first 的行
[root@localhost tmp]# sed -n '/first/!p' data2
This is the second line of the test data
This is the third line of the test data

3.4 模式替代

模式替代用&,例:

[root@localhost tmp]# echo "hello cat and hat" |sed 's/.at/&/g'
hello cat and hat
#将首先匹配到的 cat 作为替换词,替换带有.at的词

此外,如果需要替代单独的词就不能用&符号了,例:

[root@localhost]# echo "this is linux system" |sed 's/\(linux\) system/\1 windows/g'
this is linux windows
#sed 用圆括号定义模式中的子模式,sed 会给第一个子模式分配字符\1,第二个为\2以此类推

猜你喜欢

转载自blog.csdn.net/weixin_45181224/article/details/94164421