简介
sed:Stream Editor文本流编辑,sed是一个“非交互式的”面向字符流的编辑器。能同时处理多个文件多行的内容,可以不对原文件改动,把整个文件输入到屏幕,可以把只匹配到模式的内容输入到屏幕上。还可以对原文件改动,但是不会再屏幕上返回结果。
sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。
sed的选项、命令、替换标记
选项
sed [options] '{command}[flags]' [filename]
# 中括号内容必有 大括号内容可有可无
sed # 执行命令
[options] # 命令选项
{command}[flags] # sed内部选项和参数
[filename] # 文件
命令选项
-e script 将脚本中指定的命令添加到处理输入时执行的命令中, 多条件,一行中要有多个操作
-f script 将文件中指定的命令添加到处理输入时执行的命令中
-n 抑制自动输出
-i 编辑文件内容
-i.bak 修改时同时创建.bak备份文件。
-r 使用扩展的正则表达式
! 取反 (跟在模式条件后与shell有所区别)
sed常用内部命令
a 在匹配后面添加
i 在匹配前面添加
p 打印
d 删除
s 查找替换
c 更改
y 转换 N D P
flags
数字 表示新文本替换的模式
g: 表示用新文本替换现有文本的全部实例
p: 表示打印原始的内容
w filename: 将替换的结果写入文件
sed常用命令
a\ 在当前行下面插入文本;
i\ 在当前行上面插入文本;
c\ 把选定的行改为新的文本;
d 删除,删除选择的行;
D 删除模板块的第一行;
s 替换指定字符;
h 拷贝模板块的内容到内存中的缓冲区;
H 追加模板块的内容到内存中的缓冲区;
g 获得内存缓冲区的内容,并替代当前模板块中的文本;
G 获得内存缓冲区的内容,并追加到当前模板块文本的后面;
l 列表不能打印字符的清单;
n 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令;
N 追加下一个输入行到模板块后面并在二者间嵌入一个新行,改变当前行号码;
p 打印模板块的行。 P(大写) 打印模板块的第一行;
q 退出Sed;
b lable 分支到脚本中带有标记的地方,如果分支不存在则分支到脚本的末尾;
r file 从file中读行;
t label if分支,从最后一行开始,条件一旦满足或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾;
T label 错误分支,从最后一行开始,一旦发生错误或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾;
w file 写并追加模板块到file末尾;
W file 写并追加模板块的第一行到file末尾;
! 表示后面的命令对所有没有被选定的行发生作用;
= 打印当前行号;
# 把注释扩展到下一个换行符以前;
sed替换标记
g 表示行内全面替换;
p 表示打印行;
w 表示把行写入一个文件;
x 表示互换模板块中的文本和缓冲区中的文本;
y 表示把一个字符翻译为另外的字符(但是不用于正则表达式);
\1 子串匹配标记;
& 已匹配字符串标记;
sed元字符集
^ 匹配行开始,如:/^sed/匹配所有以sed开头的行;
$ 匹配行结束,如:/sed$/匹配所有以sed结尾的行;
. 匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d;
* 匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行;
[] 匹配一个指定范围内的字符,如/[ss]ed/匹配sed和Sed;
[^] 匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行;
\(..\) 匹配子串,保存匹配的字符,如s/\(love\)able/\1rs,loveable被替换成lovers;
& 保存搜索字符用来替换其他字符,如s/love/**&**/,love这成**love**;
\< 匹配单词的开始,如:/\
\> 匹配单词的结束,如/love\>/匹配包含以love结尾的单词的行;
x\{m\} 重复字符x,m次,如:/0\{5\}/匹配包含5个0的行;
x\{m,\} 重复字符x,至少m次,如:/0\{5,\}/匹配至少有5个0的行;
x\{m,n\} 重复字符x,至少m次,不多于n次,如:/0\{5,10\}/匹配5~10个0的行;
示例操作
文件内容增加操作,将数据追加到某个位置之后,使用命令 a 。
在指定行后面追加内容
在第二到四行每行后新开一行追加数据: append data “haha”
[root@jiangnan data]# sed '2,4a\append data "haha"' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
append data "haha"
3 the quick brown fox jumps over the lazy dog.
append data "haha"
4 the quick brown fox jumps over the lazy dog.
append data "haha"
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
说明:a表示在匹配的行后面新开一行新增内容。2,4a表示第2到4行,如果不写数字,表示所有行。
匹配字符串追加
找到包含"3 the"的行,在其后新开一行追加内容: append data “haha”
[root@jiangnan data]# sed '/3 the/a\append data "haha"' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
append data "haha"
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
这里注意:/要匹配的字符串/
这种方法比较常用,匹配指定的字符串然后进行增加,比如XML文件等。
文件内容增加操作,将数据插入到某个位置之前,使用命令 i 。
匹配字符串插入
找到包含"3 the"的行,在其前新开一行插入内容: insert data “haha”
[root@jiangnan data]# sed '/3 the/i\append data "haha"' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
append data "haha"
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
这里注意:/要匹配的字符串/
使用方法同a类似。
文件内容修改操作—替换,将一行中匹配的内容替换为新的数据,使用命令s。
在指定行后面追加内容
将sed.txt 中第二到第四行的dog替换为cat
[root@jiangnan data]# sed '2,4s/dog/cat/' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy cat.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy cat.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
匹配字符串所在行指定字符的替换
将包含字符串"3 the"的行中的dog替换为cat
[root@jiangnan data]# sed '/3 the/s/dog/cat/' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
这里注意:/匹配字符串所在的行/s/指定匹配字符串/要替换的字符串/
文件内容修改操作—更改,将一行中匹配的内容替换为新的数据,使用命令c。
匹配行整行的替换
将sed.txt 文件中的第二、三、四行的内容更改为:change data “haha”
[root@jiangnan data]# sed '2,4c\change data haha' sed.txt
1 the quick brown fox jumps over the lazy dog.
change data haha
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
匹配字符串所在行整行的替换
将sed.txt文件中包含"3 the"的行内容更改为: change data “haha”
[root@jiangnan data]# sed '/3 the/c\change data haha' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
change data haha
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
文件内容修改操作—字符转换,将一行中匹配的内容替换为新的数据,使用命令y。
将sed.txt中的a b c字符转换为对应的 A B C字符
[root@jiangnan data]# sed 'y/abc/ABC/' sed.txt
1 the quiCk Brown fox jumps over the lAzy dog.
2 the quiCk Brown fox jumps over the lAzy dog.
3 the quiCk Brown fox jumps over the lAzy dog.
4 the quiCk Brown fox jumps over the lAzy dog.
5 the quiCk Brown fox jumps over the lAzy dog.
[root@jiangnan data]#
[点击并拖拽以移动]
文件内容删除,将文件中的指定数据删除,使用命令d。
删除文件sed.txt 第三到第四行的数据
[root@jiangnan data]# sed '3,4d' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
文件内容查看,将文件内容输出到屏幕,使用命令p。
打印sed.txt的内容
[root@jiangnan data]# sed 'p' sed.txt
1 the quick brown fox jumps over the lazy dog.
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
打印sed.txt文件包含字符串"3 the"的行
[root@jiangnan data]# sed '/3 the/p' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
现象:可以看得出,打印内容是重复的行,原因是打印了指定文件内容一次,又将读入缓存的所有数据打印了一次,所以会看到这样的效果, 如果不想看到这样的结果,可以加命令选项-n抑制内存输出即可。
[root@jiangnan data]# sed -n '/3 the/p' sed.txt
3 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
命令选项说明
在命令行中使用多个命令 -e
将brown替换为green dog替换为cat
[root@jiangnan data]# sed -e 's/brown/green/;s/dog/cat/' sed.txt
1 the quick green fox jumps over the lazy cat.
2 the quick green fox jumps over the lazy cat.
3 the quick green fox jumps over the lazy cat.
4 the quick green fox jumps over the lazy cat.
5 the quick green fox jumps over the lazy cat.
[root@jiangnan data]#
从文件读取编辑器命令 -f 适用于日常重复执行的场景
1)将命令写入文件
[root@jiangnan data]# vim abc.txt
[root@jiangnan data]# cat abc.txt
s/brown/green/
s/dog/cat/
s/fox/elephant/
[root@jiangnan data]#
2)使用-f命令选项调用命令文件
[root@jiangnan ~]# sed -f abc.txt sed.txt
1 the quick green elephant jumps over the lazy cat.
2 the quick green elephant jumps over the lazy cat.
3 the quick green elephant jumps over the lazy cat.
4 the quick green elephant jumps over the lazy cat.
5 the quick green elephant jumps over the lazy cat.
[root@jiangnan data]#
抑制内存输出 -n
打印sed.txt文件的第二行到最后一行内容 $最后的意思
[root@jiangnan data]# sed -n '2,$p' sed.txt
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
使用正则表达式 -r
打印sed.txt中以字符串"3 the"开头的行内容
在这里插入图片描述
说明:在正则表达式中如果出现特殊字符(^$.*/[]),需要以前导 “\” 号做转义
注意:做完了上面的案例,但是我们发现源文件并没有任何改变,原因就是sed操作的是缓存区里的内容,对我们的源文件没有做改变。如果需要修改文件内容可以直接使用-i命令选项,-i命令选项提供了备份功能,比如参数使用-i.bak,那么在修改源文件的同时会先备份一个以.bak结尾的源文件,然后再进行修改操作。
[root@jiangnan data]# sed -i.bak 's/brown/green/' sed.txt
[root@jiangnan data]# cat sed.txt
1 the quick green fox jumps over the lazy dog.
2 the quick green fox jumps over the lazy dog.
3 the quick green fox jumps over the lazy dog.
4 the quick green fox jumps over the lazy dog.
5 the quick green fox jumps over the lazy dog.
[root@jiangnan data]# cat sed.txt.bak
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
标志
准备数据
[root@jiangnan data]# vi sed2.txt
[root@jiangnan data]# cat sed2.txt
1 the quick brown fox jumps over the lazy dog . dog
2 the quick brown fox jumps over the lazy dog . dog
3 the quick brown fox jumps over the lazy dog . dog
4 the quick brown fox jumps over the lazy dog . dog
5 the quick brown fox jumps over the lazy dog . dog
[root@jiangnan data]#
数字标志:此标志是一个非零正数,默认情况下,执行替换的时候,如果一行中有多个符合的字符串,如果没有标志位定义,那么只会替换第一个字符串,其他的就被忽略掉了,为了能精确替换,可以使用数字位做定义。
替换一行中的第二处dog为cat
对比有标志和没有标志的区别
g标志:将一行中的所有符合的字符串全部执行替换
将包含3 the所在行中的dog替换为cat
[root@jiangnan data]# sed '/3 the/s/dog/cat/g' sed2.txt
1 the quick brown fox jumps over the lazy dog . dog
2 the quick brown fox jumps over the lazy dog . dog
3 the quick brown fox jumps over the lazy cat . cat
4 the quick brown fox jumps over the lazy dog . dog
5 the quick brown fox jumps over the lazy dog . dog
[root@jiangnan data]#
‘g’表示global,全部替换
p标志:打印文本内容,类似于-p命令选项
[root@jiangnan data]# sed '3s/dog/cat/p' sed2.txt
1 the quick brown fox jumps over the lazy dog . dog
2 the quick brown fox jumps over the lazy dog . dog
3 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy dog . dog
5 the quick brown fox jumps over the lazy dog . dog
[root@jiangnan data]#
w filename标志:将修改的内容存入filename文件中
[root@jiangnan data]# sed '3s/dog/cat/w sed3.txt' sed2.txt
1 the quick brown fox jumps over the lazy dog . dog
2 the quick brown fox jumps over the lazy dog . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy dog . dog
5 the quick brown fox jumps over the lazy dog . dog
[root@jiangnan data]# cat sed3.txt
3 the quick brown fox jumps over the lazy cat . dog
[root@jiangnan data]#
可以看出,将修改的第三行内容存在了sed3.txt文件中
三、sed小技巧
统计data2有多少行
[root@jiangnan ~]# sed -n '$=' data2
5
打印data2内容时加上行号
[root@jiangnan ~]# sed '=' data2
1
1 the quick brown fox jumps over the lazy dog . dog
2
2 the quick brown fox jumps over the lazy dog . dog
3
3 the quick brown fox jumps over the lazy dog . dog
4
4 the quick brown fox jumps over the lazy dog . dog
5
5 the quick brown fox jumps over the lazy dog . dog
输出包含the的所在行的行号(= 用来输出行号)
[root@jiangnan ~]# sed -n '/the/=' test.txt
输出以PI开头的行
[root@jiangnan ~]# sed -n '/^PI/p' test.txt
输出以数字结尾的行
[root@jiangnan ~]# sed -n '/[0-9]$/p' test.txt
输出包含单词wood的行 \< ,\>表示单词边界
[root@jiangnan ~]# sed -n '/\<wood\>/p' test.txt
a wood cross!
每行开始添加#字符
[root@jiangnan ~]# sed 's/^/#/' test.txt
在包含the的每行行首添加#字符
[root@jiangnan ~]# sed '/the/s/^/#/' test.txt
在每行末尾添加EOF字符
[root@jiangnan ~]# sed 's/$/EOF/' test.txt
将3-5行所有的the替换为THE
[root@jiangnan ~]# sed '3,5s/the/THE/g' test.txt
将包含the的行中的o替换为O
[root@jiangnan ~]# sed '/the/s/o/O/g' test.txt
迁移符合条件的文本
H 复制到剪贴板;
g,G 将剪贴板中的数据覆盖/追加到指定行;
w保存为文件;
r读取指定文件;
a 追加指定内容
将包含the的行迁移到行尾(;用于多个操作)
H复制到剪贴板---d删除---$G追加到行尾
[root@jiangnan ~]# sed '/the/{H;d};$G' test.txt
将1-5行迁移到17行后
[root@jiangnan ~]# sed '1,5{H;d};17G' test.txt
将包含the的行另存为新文件
[root@jiangnan ~]# sed '/the/w out.file' test.txt
[root@jiangnan ~]# ls
anaconda-ks.cfg out.file test.txt :wq
[root@jiangnan ~]# cat out.file
在包含the每行后添加文件hostname内容
[root@jiangnan ~]# sed '/the/r /etc/hostname' test.txt
在第3行后插入新行,内容为New
[root@jiangnan ~]# sed '3aNew' test.txt
在包含the的每行后插入新行
[root@jiangnan ~]# sed '/the/aNew' test.txt
在第3行后插入多行(\n 换行符)
[root@jiangnan ~]# sed '3aNew1\nNew2' test.txt
ngnan ~]# sed '1,5{H;d};17G' test.txt
将包含the的行另存为新文件
[root@jiangnan ~]# sed '/the/w out.file' test.txt
[root@jiangnan ~]# ls
anaconda-ks.cfg out.file test.txt :wq
[root@jiangnan ~]# cat out.file
在包含the每行后添加文件hostname内容
[root@jiangnan ~]# sed '/the/r /etc/hostname' test.txt
在第3行后插入新行,内容为New
[root@jiangnan ~]# sed '3aNew' test.txt
在包含the的每行后插入新行
[root@jiangnan ~]# sed '/the/aNew' test.txt
在第3行后插入多行(\n 换行符)
[root@jiangnan ~]# sed '3aNew1\nNew2' test.txt
参考:
Linux之sed命令详解 - zakun - 博客园
【Linux篇】sed命令详解_傻啦猫@_@的博客-CSDN博客_linux sed命令