文本三剑客之sed编辑器

122 阅读5分钟

sed编辑器

sed是一种流编辑器,流编辑器会在编辑器处理数据,之前基于预先提供的一组规则来编辑数据。

sed编辑器可以根据命令来处理数据流中的数据,这些命令要么从命令行中输入,要么储存在一个命令文本文件中。

sed的工作流程

sed的工作流程主要包括、读取,执行和显示三个过程

  • 读取:sed从输出流(文件、管道、标准输入 )中读取一行内容并存储到临时的缓冲区中(又称模式空间,pallern space)。
  • 执行:默认情况下,所有sed命令都在模式空间中顺序的执行,除非指定了行的地址,否则sed命令将会在所有的行上一次执行
  • 显示:发送修改后的内容到输出流,在发送数据后,模式空间将会被清空。所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。

在所有的文件内容都被处理完成之前,上诉过程将重复执行,直至所有内容被处理完。

注意:默认情况下所有的sed命令都是在模式空间内执行的,因此输入的文件并不会发送任何变化,除非是用重定向存储输入。

sed命令格式

sed -e '操作' 文件1 文件2 . . .

sed -n -e '操作' 文件1 文件2 . . .

sed -f 脚本文件 文件1 文件2 . . .

sed -i -e '操作' 文件1 文件2 . . .

格式

sed -e 'n {

操作1

操作2

. . .

}' 文件1 文件2
[root@localhost ~]# cat 1.txt 
one
tow
three
four
five
six
seven
eight
nine
ten
eieven
twelve
[root@localhost ~]# sed -e 'p' 1.txt 
one
one
tow
tow
three
three
four
four
five
five
six
six
seven
seven
eight
eight
nine
nine
ten
ten
eieven
eieven
twelve
twelve

-e.png

常用选项

sed常用选项作用
-e :或--expression=:表示用来指定命令来处理输入的文本文件,只有一个操作命令时可省略,一般在执行多个操作命令中使用
-f 或--filc=:表示用指定的脚本文件来处理输入的文本文件
-h 或--help:显示帮助
-n 、--quiet 或 silent:禁止sed编辑器输出,但可以与p命令一起使用完成输出。
-i :直接修改目标文本文件。
常用操作作用
s:替换,替换指定字符
d:删除,删除选定的行
a:增加,在当前行下面增加一行指定内容
i:插入,在选定行上面插入一行指定内容
c:替换,将选定行替换为指定内容。
y:字符转换,转换前后的字符长度必须相同
p:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果非打印字符,则以ASCII码输出。其通常与”-n“选项一起使用
=:打印行数
l:(小写L):打印数据流中的文本和不可以打印的ASCII字符(比如结束符$、制表符\t)

打印内容:

1.sed 查找打印 -p

sed -e "p" :每行内容打印两次。

sed -n "p" :每行内容只打印一次。

-p.png

[root@localhost ~]# cat 1.txt ##查看文件内容
one
tow
three
four
five
six
seven
eight
nine
ten
eieven
twelve
[root@localhost ~]# sed -e 'p' 1.txt  ##打印两遍
one
one
tow
tow
three
three
four
four
five
five
six
six
seven
seven
eight
eight
nine
nine
ten
ten
eieven
eieven
twelve
twelve
[root@localhost ~]# sed -n -e 'p' 1.txt   ##打印一遍
one
tow
three
four
five
six
seven
eight
nine
ten
eieven
twelve

2、'n' 打印行号。

sed -n '=' :只打印行号。

sed -e 'n' : 打印行号和行内容。

sed -n '=;p' :打印行号和行内容。

[root@localhost ~]# sed -n  '=' 1.txt 
1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# sed  -e '=' 1.txt 
1
one
2
tow
3
three
4
four
5
five
6
six
7
seven
8
eight
9
nine
10
ten
11
eieven
12
twelve
[root@localhost ~]# sed -n '=;p' 1.txt 
1
one
2
tow
3
three
4
four
5
five
6
six
7
seven
8
eight
9
nine
10
ten
11
eieven
12
twelve

打印行号.png

行号和内容.png

行号和内容1.png

sed -n -e 'l' testfilel

'l' 打印文本即隐藏字符(结束符$,制表符\t)

[root@localhost ~]# sed -n -e 'l' file.txt 
one$
two$
three$
fore$
five$
six$
seven$
eight$
nine$
ten$

l.png

sed对指定行进行操作

两种方法:

  1. 以数字形式表示行区间;
  2. 用文本模式(字符串)来过滤出行(一般结合正则表达式)。

以数字形式表示行区间:

操作含义
'1p'打印第一行
'$p'打印最后一行
'1,3p'打印连续行,打印第一行到第三行
'6,$p'打印第六行到最后一行
'1,+3p'打印第一行加后面三行(即打印第一到第四行)
'5q'打印前五行后退出
'p;n'打印奇数行
'n;p'打印偶数行

使用字符串匹配出行:

操作含义
'/root/p'打印包含root的行
'/root/!p'打印不包含root的行。! 表示取反
'/^root/p'打印以root开头的行
'/bash$/'打印以bash结尾的行
'/root l bash/p'打印包含root或bash的行。"l"是扩展正则表达式的元字符,要使用sed -r
'6,/root/p'打印第6行到第一个包含root的行

以数字形式表示行区间

[root@localhost ~]# sed -n '1p' file.txt ##打印第一行
one
[root@localhost ~]# sed -n '3p' file.txt ##打印第三行
three
[root@localhost ~]# sed -n '$p' file.txt ##打印最后一行
ten
[root@localhost ~]# sed -n -e '1p' -e '5p' file.txt  ##打印第一行和第五行
one
five

数字形式.png

打印连续的行

[root@localhost ~]# sed -n '1,4p' file.txt 
one
two
three
fore
[root@localhost ~]# sed -n '5,$p' file.txt 
five
six
seven
eight
nine
ten

连续打印.png

使用sed输出奇数行或者偶数行

'n' :next,读取下一行。

'p' :打印当前所在行。

[root@localhost ~]# sed -n 'p;n' file.txt 
one
three
five
seven
nine
[root@localhost ~]# sed -n 'n;p' file.txt 
two
fore
six
eight
ten

基数偶数行.png

sed删除 d

[root@localhost ~]# cat file.txt 
one
two
three
fore
five
six
seven
eight
nine
ten
[root@localhost ~]# sed '3d' file.txt  ##删除第3one
two
fore
five
six
seven
eight
nine
ten
[root@localhost ~]# sed '5,7d' file.txt   ##删除5-7one
two
three
fore
eight
nine
ten
[root@localhost ~]# sed '$d' file.txt   ##删除最后一行
one
two
three
fore
five
six
seven
eight
nine


删除.png

删除空行的三种方法:

  1. grep -v "^$" file.txt //过滤出非空行
  2. cat file.txt |tr -s "\n" //压缩换行符
  3. sed '/^$/d' file.txt //删除空行

sed 修改替换s c y

s:替换字符串

c:整行替换

y:字符替换,替换前后的字符串长度必须相同

格式:

 行范围 s/旧字符串/新字符串/替换标记
 ​
 #4种替换标记:
 数字:表明新字符串将替换第几处匹配的地方
 g:表面新字符串将会替换所有匹配的地方
 p:打印与替换命令匹配的行,与-n一起使用
 w 文件:将替换的结果写入文件中
[root@localhost ~]# sed -n '/root/p' pass.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# sed -n 's/root/aaa/p' pass.txt 
aaa:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/aaa:/sbin/nologin
[root@localhost ~]# sed -n 's/root/bbb/2p' pass.txt 
root:x:0:0:bbb:/root:/bin/bash
[root@localhost ~]# sed -n 's/root/aaa/gp' pass.txt 
aaa:x:0:0:aaa:/aaa:/bin/bash
operator:x:11:0:operator:/aaa:/sbin/nologin

替换字符.png

替换1.png

[root@localhost ~]# sed -n 's/root/aaa/gp' pass.txt 
aaa:x:0:0:aaa:/aaa:/bin/bash
operator:x:11:0:operator:/aaa:/sbin/nologin

删除root.png

整行替换 c

[root@localhost ~]# cat file.txt 
one
two
three
fore
five
six
seven
eight
nine
ten
[root@localhost ~]# sed '/th/c 33' file.txt ##将含有th的行整行替换成33
one
two
33
fore
five
six
seven
eight
nine
ten

整行替换.png

单字符替换 y

[root@localhost ~]# sed 'y/no/52/' file.txt 
25e
tw2
three
f2re
five
six
seve5
eight
5i5e
te5

单字符替换.png

sed 增加a i r

a:在行后添加内容

i:在行前插入内容

r:在行后读入文件内容

[root@localhost ~]# sed 'y/no/52/' file.txt 
25e
tw2
three
f2re
five
six
seve5
eight
5i5e
te5

ai.png

[root@localhost ~]# echo "123456" > 2.txt 
[root@localhost ~]# cat 2.txt 
123456
[root@localhost ~]# sed '1r 2.txt' file.txt 
one
123456
two
three
fore
five
six
seven
eight
nine
ten

r.png

sed 复制粘贴

比较vi/vim编辑器和 sed编辑器:

 #vi//vim编辑器:
 命令模式
 dd p  剪切 粘贴
 yy p  复制 粘贴
 
 末行模式
 :1,3 co 10  复制 粘贴(将第1~3行复制粘贴到第10行下方)
 :1,3 m 10   剪切 粘贴(将第1~3行剪切到第10行下方)
 ​
 #sed命令:
 H复制、d删除、G粘贴到指定行下方
[root@localhost ~]# sed '1,4 {H;d};$G' file.txt 
five
six
seven
eight
nine
ten

one
two
three
fore
[root@localhost ~]# sed '1,4 H;$G' file.txt 
one
two
three
fore
five
six
seven
eight
nine
ten

one
two
three
fore

复制粘贴.png