Linux Bash sed

sed工具
sed -[nefri] [action]

-n : silent模式,将通过sed处理(操作)过的行信息显示出来,未处理的不予显示;
-e : 直接在命令行上进行sed的action编辑;
-f : 将sed的action写入一个文件内,-f filename 可以读取并执行 filename 里面的action;
-r : sed将变为支持扩展型正则表达式语法(默认是基础正则表达式语法);
-i : 直接修改文件内容,而不是修改stdout;

 acion 的具体操作:action前可以加数字:[n1, [n2]] action, n1, n2 可有可无,代表操作的文件内的行数;
 
 在action后如下参数的含义:
 a: 新增 ==> [action]a [String] 在a后的String将会在下一行插入
 c: 替换 ==> n1, n2[actioon]c [String] 将n1到n2的行用后面这个String替换;
 d: 删除 ==> n1, n2d 删除 从n1到n2 所有的行;
 i: 与a不同的是,它将在上一行进行插入;
 p: 打印;
 s:(重要) : 搭配正则表达式,替换掉匹配到的数据;常规用法 's/[old]/[new]/g';

直接上例题看吧:
例1:取得/etc/passwd的内容,删除2到6行的数据并输出;

[cocan@bogon ~]$ cat -n /etc/passwd | sed '2,6d'
     1	root:x:0:0:root:/root:/bin/bash
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8	halt:x:7:0:halt:/sbin:/sbin/halt
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10	operator:x:11:0:operator:/root:/sbin/nologin
	...省略...

例2:承上例,在/etc/passwd的 第2行 插入: ‘Deleted 5 Lines.’

[cocan@bogon ~]$ cat -n /etc/passwd | sed '2,6d'| sed '1a Deleted 5 Lines.'
     1	root:x:0:0:root:/root:/bin/bash
Deleted 5 Lines.
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8	halt:x:7:0:halt:/sbin:/sbin/halt
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10	operator:x:11:0:operator:/root:/sbin/nologin
    ...省略...

例3: 打印/etc/passwd第11到20行的数据;
注意:使用了sed -n, 代表打印出来的这些数据都是经过了sed操作处理的,若不加则输出整个文件数据;

[cocan@bogon ~]$ cat -n /etc/passwd | sed -n '11,20p'
   11	games:x:12:100:games:/usr/games:/sbin/nologin
   12	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
   13	nobody:x:99:99:Nobody:/:/sbin/nologin
   14	systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
   15	dbus:x:81:81:System message bus:/:/sbin/nologin
   16	polkitd:x:999:998:User for polkitd:/:/sbin/nologin
   17	libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
   18	colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
   19	rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
   20	gluster:x:996:993:GlusterFS daemons:/run/gluster:/sbin/nologin

例4:去除last命令前五行的空行

[cocan@bogon ~]$ last -n 5  # 实际上总是会多出两行,即最下面的两行
cocan    pts/1        :0               Thu Oct 10 16:03   still logged in   
cocan    pts/0        :0               Thu Oct 10 10:57   still logged in   
cocan    :0           :0               Thu Oct 10 10:56   still logged in   
reboot   system boot  3.10.0-957.27.2. Thu Oct 10 10:55 - 16:13  (05:18)    
cocan    :0           :0               Thu Oct 10 10:49 - 10:51  (00:02)    

wtmp begins Wed Aug 14 07:39:59 2019
[cocan@bogon ~]$ 
[cocan@bogon ~]$ last -n 5 | sed '/^$/d' # 可以看到,此处的正则表达式需要用 // 包围起来使用
cocan    pts/1        :0               Thu Oct 10 16:03   still logged in   
cocan    pts/0        :0               Thu Oct 10 10:57   still logged in   
cocan    :0           :0               Thu Oct 10 10:56   still logged in   
reboot   system boot  3.10.0-957.27.2. Thu Oct 10 10:55 - 16:16  (05:20)    
cocan    :0           :0               Thu Oct 10 10:49 - 10:51  (00:02)    
wtmp begins Wed Aug 14 07:39:59 2019

例5:承上例,将各行内’cocan’替换为’root’

[cocan@bogon ~]$ last -n 5 | sed '/^$/d' | sed 's/cocan/root/g'
root    pts/1        :0               Thu Oct 10 16:03   still logged in   
root    pts/0        :0               Thu Oct 10 10:57   still logged in   
root    :0           :0               Thu Oct 10 10:56   still logged in   
reboot   system boot  3.10.0-957.27.2. Thu Oct 10 10:55 - 16:19  (05:23)    
root    :0           :0               Thu Oct 10 10:49 - 10:51  (00:02)    
wtmp begins Wed Aug 14 07:39:59 2019

例6: 承上例,将以’reboot’开头的行删除,同时不能出现空行;
注意:行删除操作会生成空行,故要在删除后再去匹配空行然后将其删除;

[cocan@bogon ~]$ last -n 5 | sed '/^reboot/d' | sed '/^$/d'
cocan    pts/1        :0               Thu Oct 10 16:03   still logged in   
cocan    pts/0        :0               Thu Oct 10 10:57   still logged in   
cocan    :0           :0               Thu Oct 10 10:56   still logged in   
cocan    :0           :0               Thu Oct 10 10:49 - 10:51  (00:02)    
wtmp begins Wed Aug 14 07:39:59 2019
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值