sed命令

sed命令格式:

sed -e '操作' 文件1 文件2  
sed -n -e '操作' 文件1 文件2  
sed -f 脚本文件 文件1 文件2  
sed -i -e '操作' 文件1 文件2

常用选项:

选项作用
-e 或--expression=表示用指定命令来处理输入的文本文件,只有一个操作命令时可省略,一般在执行多个操作命令使用
-f 或--file=表示用指定的脚本文件来处理输入的文本文件
-h 或--help显示帮助
-n、--quiet或--silent禁止sed编辑器输出,但可以与p命令一起使用完成输出
-i直接修改目标文本文件

常用操作:

操作作用
s替换,替换指定字符
d删除,删除选定的行
a增加,在当前行下方增加一行指定内容
i插入,在选定行上方插入一行指定内容
c替换,将选定行替换为指定内容
y字符转换,转换前后的字符长度必须相同
p打印行内容。如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以ASCII码输出。其通常与"-n"选项一起使用
=打印行号
l (小写L)打印数据流中的文本和不可打印的ASCII字符(比如结束符$、制表符\t)

sed 查找打印 -p

1、sed编辑器默认输出行内容,-n选项可以禁止输出。如果不加-n,却使用p操作,那么每行内容会打印两次。

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

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

[root@localhost ~]# cat 11.txt 
one
two
three
four
five
six
seven
eight
nine
ten
[root@localhost ~]# sed -e 'p' 11.txt         //每行内容会打印两次
one
one
two
two
three
three
four
four
five
five
six
six
seven
seven
eight
eight
nine
nine
ten
ten
[root@localhost ~]# sed -n -e 'p' 11.txt     //每行内容只打印一次
one
two
three
four
five
six
seven
eight
nine
ten

 2、'n' 打印行号

[root@localhost ~]# sed -n '=' 11.txt     //只打印行号
1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# sed -e '=' 11.txt     //打印行号和行内容
1
one
2
two
3
three
4
four
5
five
6
six
7
seven
8
eight
9
nine
10
ten
[root@localhost ~]# sed -n '=;p' 11.txt        //打印行号和行内容
1
one
2
two
3
three
4
four
5
five
6
six
7
seven
8
eight
9
nine
10
ten

 3、sed 执行多条命令的三种方法。

 #方法一
 sed -n -e '=' -e 'p'  file.txt
 ​
 #方法二
 sed -n -e '=;p'  file.txt
 ​
 #方法三:换行操作
 sed -n '
 =
 p
 ' file.txt

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

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

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的行

 以数字形式表示行区间

1、打印单行

[root@localhost ~]# sed -n '1p' 11.txt 
one
[root@localhost ~]# sed -n '2p' 11.txt 
two
[root@localhost ~]# sed -n '3p' 11.txt 
three
[root@localhost ~]# sed -n '$p' 11.txt          //打印最后一行
ten
[root@localhost ~]# sed -n -e '1p' -e '5p' 11.txt      //打印第一行、第五行
one
five

 2、打印连续的行,使用逗号

[root@localhost ~]# sed -n '1,3p' 11.txt 
one
two
three
[root@localhost ~]# sed -n '6,$p' 11.txt 
six
seven
eight
nine
ten

 使用正则表达式,匹配行内容

注意:sed 使用扩展正则表达式时,要加 -r 。

示例1:

[root@localhost ~]# sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# sed -n '/^root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash

 示例2:

[root@localhost ~]# sed -n '6,/root/p' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# sed -n '6,/root/=' /etc/passwd
6
7
8
9
10

 sed 删除d

示例1:
以数字形式表示行区间。

[root@localhost ~]# cat 11.txt 
one
two
three
four
five
six
seven
eight
nine
ten
[root@localhost ~]# sed '3d' 11.txt          //删除第3行
one
two
four
five
six
seven
eight
nine
ten
[root@localhost ~]# sed '5,8d' 11.txt        //删除第5~8行
one
two
three
four
nine
ten
[root@localhost ~]# sed '$d' 11.txt          //删除最后一行
one
two
three
four
five
six
seven
eight
nine
[root@localhost ~]# sed 'd' 11.txt          //删除所有行
[root@localhost ~]# sed '1,+3d' 11.txt     //删除第1行及后面3行(即删除第1~4行)
five
six
seven
eight 
nine 
ten  
[root@localhost ~]# sed '6,$d' 11.txt     //删除第6行到最后一行  
one
two  
three 
four 
five  
[root@localhost ~]# sed '6!d' 11.txt     //除了第6行,其他都删除  
six
[root@localhost ~]# sed '6,$!d' 11.txt   //除了第6行到最后一行,其他都删除  
six 
seven 
eight 
nine  
ten

 示例2:
通过字符串匹配出想要的行。

[root@localhost ~]# sed '/e$/d' 11.txt   //删除以e结尾的行 
two  
four 
six  
seven  
eight 
ten  
[root@localhost ~]# sed '/e$/!d' 11.txt   //除了以e结尾的行,其他行都删除 
one  
three  
five  
nine

 替换字符串 s

格式:


 行范围 s/旧字符串/新字符串/替换标记
 ​
 #4种替换标记:
 数字:表明新字符串将替换第几处匹配的地方
 g:表面新字符串将会替换所有匹配的地方
 p:打印与替换命令匹配的行,与-n一起使用
 w 文件:将替换的结果写入文件中

 示例1:

[root@localhost ~]# sed -n 's/root//gp' /etc/passwd           //删除匹配行当所有root
:x:0:0::/:/bin/bash
operator:x:11:0:operator:/:/sbin/nologin
[root@localhost ~]# echo 000010101 | sed 's/^0*//'            //删除开头所有的0
10101

 示例2:
将root开头的行进行注释(在开头加上#),在包含root的行的行尾加上#

root@localhost ~]# sed -n '/^root/p' /etc/passwd         //打印所有以root开头的行
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# sed -n '/^root/ s/^/#/p' /etc/passwd  //过滤出以root开头的行,在行首加上#
#root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# sed -n '/root/p' /etc/passwd        //打印包含root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin 
[root@localhost ~]# sed -n '/root/ s/$/#/p' /etc/passwd       //在包含root的行的行尾加上#
root:x:0:0:root:/root:/bin/bash#
operator:x:11:0:operator:/root:/sbin/nologin#

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值