sed 编辑器简单使用(2)

sed 编辑器 可以 指定行号 ,进行 操作,比如 在指定行进行 替换, 在指定行范围进行替换。 当然 也可以通过 匹配模式串进行 替换。
看看下面的例子
1 使用行号 ,进行过滤。

[root@myCentOS shell]# cat data1

The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy dog
[root@myCentOS shell]#
比如 可以指定 只匹配 第2行的模式,进行替换。
[root@myCentOS shell]# sed '2s/dog/cat/' data1
The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy cat
The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy dog
指定 2到5行 ,进行匹配。
[root@myCentOS shell]# sed '2,5s/dog/cat/' data1
The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy cat
The quick brown foe jumps over the lazy cat
The quick brown foe jumps over the lazy cat
The quick brown foe jumps over the lazy cat
The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy dog
指定 从2 到尾行 ,进行匹配。
[root@myCentOS shell]# sed '2, $s/dog/cat/' data1
The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy cat
The quick brown foe jumps over the lazy cat
The quick brown foe jumps over the lazy cat
The quick brown foe jumps over the lazy cat
The quick brown foe jumps over the lazy cat
The quick brown foe jumps over the lazy cat
The quick brown foe jumps over the lazy cat
The quick brown foe jumps over the lazy cat
还有一种 不是通过行号,而是通过正则表达式进行匹配行, 之后进行 替换,
举个例子, 比如我要把 changyubiao 这个用户的shell 替换为 /bin/dash

2使用文本模式进行过滤  
[root@myCentOS shell]# grep chang mypasswd
changyubiao:x:500:500::/home/ changyubiao:/bin/bash
yubiao:x:530:530::/home/ changyubiao:/sbin/nologin

我想把第一行 changyubiao 的shell 替换为 /bin/dash
sed /pattern/command 这样就可以了。
sed -r /^chang/s/bash/dash mypasswd
sed -r '/^chang/s/bash/dash/g ' mypasswd

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
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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
changyubiao:x:500:500::/home/changyubiao:/bin/ dash
ntp:x:38:38::/etc/ntp:/sbin/nologin
aming:x:501:504::/home/aming:/bin/bash
aming12:x:502:505::/home/aming12:/bin/bash
aming123:x:503:506::/home/aming123:/bin/bash
biaoge:x:504:502::/home/biaoge:/bin/bash
biaoge2:x:520:502::/home/biaoge2:/bin/bash
biaoge3:x:521:521::/home/biaoge3:/bin/bash
user1:x:522:522::/home/user1:/bin/bash
user2:x:523:523::/home/user2:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin
mysql:x:524:524::/home/mysql:/sbin/nologin
php-fpm:x:525:525::/home/php-fpm:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
user3:x:526:526::/home/user3:/bin/bash
virftp:x:527:527::/home/virftp:/sbin/nologin
smbuser:x:528:528::/home/smbuser:/bin/bash
redis:x:529:529::/home/redis:/sbin/nologin
这个时候 可以用 -n 禁止输出就可以了, 此时changyubiao 的shell就替换为 /bin/dash 了。
[root@myCentOS shell]# sed -r -n ' /^chang/s/bash/dash/g p' mypasswd
changyubiao:x:500:500::/home/changyubiao:/bin/dash

甚至可以通过组合命令,来完成 这些操作, 一下就是 替换 两个 单词,同时 作用于 第2行。
[root@myCentOS shell]# sed '2{
> s/fox/elephant/
> s/dog/cat/
> }' data1
The quick brown fox jumps over the lazy dog
The quick brown elephant jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
当然也可以指定一个范围: 比如 从 第2 行到第5行, 执行 以下的组合替换。
[root@myCentOS shell]# sed '2,5{
> s/fox/elephant/
> s/dog/cat/
> }' data1
The quick brown fox jumps over the lazy dog
The quick brown elephant jumps over the lazy cat
The quick brown elephant jumps over the lazy cat
The quick brown elephant jumps over the lazy cat
The quick brown elephant jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
类似的是: 从第2行到尾行 进行 组合命令
sed '2,${回车
s/fox/elephant/
s/dog/cat/
}' data1

[root@centos6_8 111]# cat  data1
The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy dog
The quick brown foe jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
[root@centos6_8 111]# sed  '2,$  {
> s/foe/elephant/
> s/dog/cat/
> }' data1
The quick brown foe jumps over the lazy dog
The quick brown elephant jumps over the lazy cat
The quick brown elephant jumps over the lazy cat
The quick brown elephant jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值