Linux——sed命令

Sed

sed 常用参数

  • -e 多条件编辑
  • -r 支持扩展正则表达式
  • -n 只显示匹配出的行
  • -f 指定sed脚本
  • -i 直接修改源文件
  • = 显示文件行号

文件内容

[root@localhost ~]# cat test.txt 
1
2
3
4
5
6
7
8
9
10

sed在文件中查询文本的方式:

1)使用行号,可以是一个简单数字,或是一个行号范围

xx为行号
x,y表示行号从x到y
/pattern查询包含模式的行
/pattern /pattern查询包含两个模式的行
pattern/,x在给定行号上查询包含模式的行
x,/pattern/通过行号和模式查询匹配的行
x,y!查询不包含指定行号x和y的行
#打印文件中的第二行,默认会全部输出,但是会输出x参数的2编。加-n 选项,只打印匹配行
[root@localhost ~]# sed '2p' test.txt 
1
2
2
3
4
5
6
7
8
9
10
[root@localhost ~]# sed -n '2p' test.txt 
2
[root@localhost ~]# sed -n '2,5p' test.txt 
2
3
4
5

#取反:打印除了x的参数行数字
[root@localhost ~]# sed  -n '2!p' test.txt 
1
3
4
5
6
7
8
9
10

#打印2到6行
[root@localhost ~]# sed  -n '2,6p'  test.txt 
2
3
4
5
6

删除命令

  • d :可删除指定的行
#文件内容
[root@localhost ~]# vim sed.txt
[root@localhost ~]# cat sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line

#将文件的第一行删除后输出到屏幕
[root@localhost ~]# sed '1d' sed.txt 
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line

#sed默认不修改原文件,如果希望保存修改后的文件则需要用重定向
[root@localhost ~]# sed '1d' sed.txt > saved_file
[root@localhost ~]# cat saved_file 
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line

#直接修改文件,使用‘-i’参数,删除saved_file第一行数据
[root@localhost ~]# sed -i '1d' saved_file 
[root@localhost ~]# cat saved_file 
this is line 4,this is Fourth line
this is line 5,this is Fifth line

#删除指定范围的行(第1行到第3行)
[root@localhost ~]# sed '1,3d' sed.txt 
this is line 5,this is Fifth line

#删除指定行到下2行之间的内容,比如删除第3行到下两行之间的内容
[root@localhost ~]# sed '3,+2d' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

#删除第3行到最后行
[root@localhost ~]# sed '3,$d' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

#删除最后一行
[root@localhost ~]# sed '$d' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line

#'$d'是删除最后一行,加上!号是什么意思呢?
[root@localhost ~]# cat t2
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line
[root@localhost ~]# sed '$!d' t2 
this is line 5,this is Fifth line
#结果得出是取反的意思,删除除了最后一行的数据!

跨奇数、偶数删除

#添加数据
[root@localhost ~]# cat >> sed.txt  <<EOF
> 6
> 7
> 8
> 9
> EOF
[root@localhost ~]# cat sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
7
8
9

#跨奇数删除
[root@localhost ~]# sed '1~2d' sed.txt 
this is line 2,the Second line,Empty line followed
this is line 5,this is Fifth line
7
9

#跨偶数删除
[root@localhost ~]# sed '1~2!d' sed.txt 
this is line 1,this is First line
this is line 4,this is Fourth line
6
8
#或者
[root@localhost ~]# sed '2~2d' sed.txt 
this is line 1,this is First line
this is line 4,this is Fourth line
6
8

#从指定的行再进行跨奇数或偶素删除,假设这里从第3行开始,跨偶素数删除
[root@localhost ~]# cat sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
7
8
9
[root@localhost ~]# sed  '3~2d' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 5,this is Fifth line
7
9

定点删除

#添加数据
[root@localhost ~]# cat >> sed.txt  << EOF
> this is line 5,this is Fifth line
> 6
> You jump!
> 7
> 8
> I jump!
> 9
> 10
> EOF
[root@localhost ~]# cat sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
7
8
9
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

#删除所有包含‘Empty’的行
[root@localhost ~]# sed '/Empty/d' sed.txt 
this is line 1,this is First line
this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
7
8
9
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

#删除所包含'Empty' 到最后的行
[root@localhost ~]# sed '/Empty/,$d' sed.txt 
this is line 1,this is First line

#删除所包含'Empty' 到下两行的行
[root@localhost ~]# sed '/Empty/,+2d' sed.txt 
this is line 1,this is First line
6
7
8
9
this is line 5,this is Fifth line
6
You jump!
7
8
I jump!
9
10

#删除从‘You jump!’ 到 “I jump‘ 的行,关键在于查找关键字
[root@localhost ~]# sed '/^Y/,/^I/d' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
7
8
9
this is line 5,this is Fifth line
6
9
10

#删除空行
[root@localhost ~]# sed '/^$/d' sed.txt 

#这里注意标识空行,和含有空是有区别的,删除带有空格的行
[root@localhost ~]# sed '/[[:space:]]/d' sed.txt 
6
7
8
9
6
7
8
9
10

查找替换

  • s:命令可将查找到的匹配文本内容替换为新的文本,默认情况只替换第一次匹配到的内容
#将line替换为LINE
[root@localhost ~]# sed 's/line/LINE/' sed.txt 
this is LINE 1,this is First line
this is LINE 2,the Second line,Empty line followed
this is LINE 4,this is Fourth line
this is LINE 5,this is Fifth line
.....

#只替换第二个匹配到的line为LINE,每行的第二个
[root@localhost ~]# sed 's/line/LINE/2' sed.txt 
this is line 1,this is First LINE
this is line 2,the Second LINE,Empty line followed
this is line 4,this is Fourth LINE
......
  • g:可以完成所有匹配值的替换
#真个文件全部替换
[root@localhost ~]# sed 's/line/LINE/g' sed.txt 
this is LINE 1,this is First LINE
this is LINE 2,the Second LINE,Empty LINE followed
this is LINE 4,this is Fourth LINE
this is LINE 5,this is Fifth LINE
......

#将以this开头的this全部替换为that
[root@localhost ~]# sed 's/^this/that/g' sed.txt 
that is line 1,this is First line
that is line 2,the Second line,Empty line followed
that is line 4,this is Fourth line
that is line 5,this is Fifth line

#将符合"," 全部换成"-"并且打印出来
[root@localhost ~]# sed -n 's/,/-/gp' sed.txt 
this is line 1-this is First line
this is line 2-the Second line-Empty line followed
this is line 4-this is Fourth line
this is line 5-this is Fifth line
this is line 5-this is Fifth line

#删除每一行的第一个字符
[root@localhost ~]# sed  's/^.//g' sed.txt 
his is line 1,this is First line
his is line 2,the Second line,Empty line followed
his is line 4,this is Fourth line
his is line 5,this is Fifth line
......

#匹配到Empty的行,行首的单词前添加you can see
[root@localhost ~]# sed '/Empty/s/^/you can see/' sed.txt 
this is line 1,this is First line
you can seethis is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line

#匹配到Empty的行,将行首第一个字母替换为*
[root@localhost ~]# sed '/Empty/s/^./*/' sed.txt 
this is line 1,this is First line
*his is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line
......

字符转换

  • y:可以进行字符转换,其作用为将一系列字符逐个地变换为另外一系列字符

PS:注意转换字符和被转换字符的长度要相等,否则sed无法执行!

[root@localhost ~]# cat sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line
......

#字符转换
[root@localhost ~]# sed 'y/12345/ABCDE/' sed.txt 
this is line A,this is First line
this is line B,the Second line,Empty line followed
this is line D,this is Fourth line
this is line E,this is Fifth line
......

插入文本

  • i或a:插入文本,其中i代表在匹配行之前插入,而a代表在匹配行之后插入。
#行之前插入
[root@localhost ~]# sed '2 i Insert' sed.txt 
this is line 1,this is First line
Insert
this is line 2,the Second line,Empty line followed
......

#行之后插入
[root@localhost ~]# sed '2 a Insert' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
Insert
this is line 4,this is Fourth line

#在匹配行的上一行插入文本
[root@localhost ~]# sed '/Second/i\Insert' sed.txt 
this is line 1,this is First line
Insert
this is line 2,the Second line,Empty line followed

#同时新增多行,则每行之间要用反斜杠\n来进行新行的添加
[root@localhost ~]# sed '2 a\append1\nappend2\nappend3' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
append1
append2
append3
......

#匹配到You的句子后面添加一句:he'llo
[root@localhost ~]# sed '/You/s/$/ he'llo/' sed.txt 
> ^C
[root@localhost ~]# sed "/You/s/$/ he'llo/" sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line
6
7
8
9
this is line 5,this is Fifth line
6
You jump! he'llo
7
8
I jump!
9
10
#同样如果sed 框架的 单引号 或者双引号 ,和要输出的内容有冲突 ,就需要对它进行专业 然后再次隐起来

取代行

  • c:c的后面可以接字符串,这些字符串可以取代n1,n2之间的行
#将2-4行的数据更改为this is 2-4 line
[root@localhost ~]# sed '2,4c this is 2-4 line' sed.txt 
this is line 1,this is First line
this is 2-4 line
......

读入文本

  • r:可从其他文件中读取文本,并插入匹配行之后
#将/etc/passwd中的内容读出放到Sed.txt空行之后,提前文件内必须有空行!!!!
[root@localhost ~]# sed '/^$/r /etc/passwd' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
......

打印

  • p:可进行打印,这里使用sed命令时一般都加-n参数,表示不打印没关系的行。
#不加-n参数,会输出所有行,找到的行会重复显示
[root@localhost ~]# sed -n '/the/p' sed.txt 
this is line 2,the Second line,Empty line followed

#当用到sed不同的编辑命令时,可以用{ },不同的编辑命令之间用分号隔开
#=:显示文件行号
[root@localhost ~]# sed -n "/the/{=;p}" sed.txt
2
this is line 2,the Second line,Empty line followed

#打印前5行
[root@localhost ~]# sed '5q' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line

#打印出匹配first的行到第4行
[root@localhost ~]# sed -n '/First/,4p' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Fourth line
this is line 5,this is Fifth line

#打印出不包含line的行
[root@localhost ~]# sed -n '/line/!p' sed.txt 

6
7
8
9
6
You jump!
7
8
I jump!
9
10

sed脚本

使用sed脚本可以加快工作效率,调用sed命令并使用-f参数指定文件

#作用是将全文的this改成THAT,并删除所有空行
[root@localhost ~]# cat > sed01.txt <<EOF
> \> s/this/THAT/g
> \> /^$/d
> \> EOF
> \> EOF
> ^C
[root@localhost ~]# cat > sed01.txt <<EOF
> s/this/THAT/g
> /^$/d
> EOF
[root@localhost ~]# cat sed01.txt 
s/this/THAT/g
/^$/d


#执行脚本
[root@localhost ~]# sed -f sed01.txt sed.txt 
THAT is line 1,THAT is First line
THAT is line 2,the Second line,Empty line followed
THAT is line 4,THAT is Fourth line
THAT is line 5,THAT is Fifth line
6
7
8
9
THAT is line 5,THAT is Fifth line
6
You jump!
7
8
I jump!
9
10

或者

[root@localhost ~]# cat > sed02.txt <<EOF
> #!/usr/bin/sed -f
> s/this/THAT/g
> /^$/d
> EOF
[root@localhost ~]# cat sed02.txt 
#!/usr/bin/sed -f
s/this/THAT/g
/^$/d
[root@localhost ~]# chmod +x sed02.txt 
[root@localhost ~]# ./sed02.txt sed.txt 
THAT is line 1,THAT is First line
THAT is line 2,the Second line,Empty line followed
THAT is line 4,THAT is Fourth line
THAT is line 5,THAT is Fifth line
6
7
8
9
THAT is line 5,THAT is Fifth line
6
You jump!
7
8
I jump!
9
10

将全文的this改成THAT,并删除所有空行

[root@localhost ~]# sed -e 's/this/that/g' -e '/^$/d' sed.txt
that is line 1,that is First line
that is line 2,the Second line,Empty line followed
that is line 4,that is Fourth line
that is line 5,that is Fifth line
6
7
8
9
that is line 5,that is Fifth line
6
You jump!
7
8
I jump!
9
10

#或者

[root@localhost ~]# sed 's/this/that/g; /^$/d' sed.txt
that is line 1,that is First line
that is line 2,the Second line,Empty line followed
that is line 4,that is Fourth line
that is line 5,that is Fifth line
6
7
8
9
that is line 5,that is Fifth line
6
You jump!
7
8
I jump!
9
10

Sed 总结

  • sed 默认不对原文件执行操作,如果向生效,则使用-i,而且默认全部打印输出,如果想只打印匹配到的行,用p,前面加上-n 参数。
  • 删除:d,定点删除,从哪里到哪里删除,删除开头,结尾等。跨奇数、偶数删除、包括单词的行删除、
  • 查找替换: s///,s@@@,s###,s&&&,等符号,都可以使用。
  • 字符转换:y/// 但注意的是,新的字符必须和旧的字符长度相同,否则无法使用。
  • 插入文本: i insert: 匹配行前边,a append: 匹配行后边。 添加多行,注意使用\n 换行符。
  • 取代行:选择到 几行到几行之后, 然后用c 后面就可以用一句话代替

sed中常用的符号解释:

  • \u : 就是表示把第一个字符转化成大写字母。uppercase
  • \l : 表示小写
  • & : 就是表示匹配前面正则表达式的那部分
  • \b : 匹配一个单词边界,也就是指单词和空格间的位置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值