您的位置:首页 > 运维架构 > Linux

linux命令-sed命令使用(2)

2017-08-29 23:20 169 查看
用 sed 对 一些匹配行进行操作,比如替换 。下面 举例来看
替换 用命令 关键字 s
基本 语法 : sed 's/old/new/g' 1.txt

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 lazycat
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

使用文本模式进行过滤
[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 brownelephant jumps over the lazycat
The quick brownelephant jumps over the lazycat
The quick brownelephant jumps over the lazycat
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

总结: 本文通过 使用 匹配空间,来实现 sed 的替换, 有两种方式,一种是行号,一种 是匹配空间进行匹配。

分享感动,留住快乐. --biaoge 20170829-23:26
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux sed