[root@node201 ~]# cat -n passwd | sed '2,5d' //将2到5行删除后显示
1 root:$1$QUjP8B2.$ND50PLxs.tKYDypovEFab1:0:0:root:/root:/bin/bash
6 sync:*:5:0:sync:/sbin:/bin/sync
7 shutdown:*:6:0:shutdown:/sbin:/sbin/shutdown
[root@node201 ~]# cat -n passwd | sed '2a hello china!' //在第二行的后面的一行加上hello china字符串
1 root:$1$QUjP8B2.$ND50PLxs.tKYDypovEFab1:0:0:root:/root:/bin/bash
2 bin:*:1:1:bin:/bin:/sbin/nologin
hello china!
3 daemon:*:2:2:daemon:/sbin:/sbin/nologin
[root@node201 ~]# cat -n passwd |sed '2a this is first line!\ //在第二行后面加上this is first line \表示加入后续行,以‘结束
> this is first line'
1 root:$1$QUjP8B2.$ND50PLxs.tKYDypovEFab1:0:0:root:/root:/bin/bash
2 bin:*:1:1:bin:/bin:/sbin/nologin
this is first line!
this is first line
[root@node201 ~]# cat -n passwd | sed '2,5c my name is linsco!' //将2,5行替换成my name is linsco!
1 root:$1$QUjP8B2.$ND50PLxs.tKYDypovEFab1:0:0:root:/root:/bin/bash
my name is linsco!
6 sync:*:5:0:sync:/sbin:/bin/sync
[root@node201 ~]# cat -n passwd |sed -n '5,7p' //只显示5~7行
5 lp:*:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:*:5:0:sync:/sbin:/bin/sync
7 shutdown:*:6:0:shutdown:/sbin:/sbin/shutdown
[root@node201 ~]# ifconfig eth0 | grep 'inet addr'|sed 's/^.*addr://g' |sed 's/Bcast.*.$//g'
's/^.*addr://g' :表示将从开头到addr:的字符串替换成空
's/Bcast.*.$//g':表示从Bcast到结束的字符串替换成空
192.168.0.201
[root@node201 ~]#cat 1 | sed 's/^[\t]*//' >2 //删除文件1的tab字符
[root@node1 oldboy]# cat oldboy.txt
oldboy
oldboy is not old
haiquan
kangjia
[root@node1 oldboy]# grep -E "oldboy$|^h" oldboy.txt
oldboy
haiquan
[root@node1 oldboy]# sed -i 's#oldboy$#etiantian#g' oldboy.txt #将替换写入源文件
[root@node1 oldboy]# cat oldboy.txt
etiantian
oldboy is not old
haiquan
kangjia
sed命令使用双引号的情况下,可以使用$var(变量)直接引用:
echo | sed "s/^/$RANDOM.rmvb_/g"
13562.rmvb_
2、sed命令使用单引号的情况下,可以使用'"$var"'引用(单引号,然后双引号,变量):
echo | sed 's/^/'"$RANDOM"'.rmvb_/g'
2442.rmvb_
sed中执行外部命令。
1、sed命令使用单引号的情况下使用'`shell command`'或者'$(shell command)'引用命令执行的结果:
echo | sed 's/^/'`echo $RANDOM`'.rmvb_/g'
7700.rmvb_# 结果
# 上面的命令使用了旧式的命令替换,也可以采用新式的命令替换方法,如下:
echo | sed 's/^/'$(echo $RANDOM)'.rmvb_/g'
13856.rmvb_# 结果
# 下面例子取用当前日期作为结果的一部分,如下:
echo | sed 's/^/'$(date +"%Y%m%d")'.rmvb_/g'
20130401.rmvb_ # 结果
2、sed命令中使用双引号的情况下,直接`shell command`或者$(shell command)引用命令执行。
echo | sed "s/^/$(date +"%Y%m%d").rmvb_/g"
20130401.rmvb_# 结果
# 使用环境变量$RANDOM以及旧式命令替换的例子:
echo | sed "s/^/`echo $RANDOM`.rmvb_/g"
29484.rmvb_# 结果
总结一下
在sed语句里面,变量替换或者执行shell命令,双引号比单引号少绕一些弯子;所以,sed和变量的关键词搜索的结果,众多都写上替换单引号为双引号。
[root@node201 sed]# cat list
John Daggett,341 King Road,Plymouth MA
Alice Ford,22 East Broadway,Richmond VA
Orville Thomas,11345 0ak Bridge Road,Tulsa OK
Terry Kalkas,402 Lans Road,Beaver Falls PA
Eric Adams,20 Post Road,Sudbury MA
Hubert Sims,328A Brook Road,Roanoke VA
Amy Wilde ,334 Bayshore Pkwy,Mountain View CA
Sal Carpenter,73 6th Street,Bostom MA
[root@node201 sed]# sed 's/MA/,Massachusetts/; s/PA/,Pennsylvania/' list
John Daggett,341 King Road,Plymouth ,Massachusetts
Alice Ford,22 East Broadway,Richmond VA
Orville Thomas,11345 0ak Bridge Road,Tulsa OK
Terry Kalkas,402 Lans Road,Beaver Falls ,Pennsylvania
Eric Adams,20 Post Road,Sudbury ,Massachusetts
Hubert Sims,328A Brook Road,Roanoke VA
Amy Wilde ,334 Bayshore Pkwy,Mountain View CA
Sal Carpenter,73 6th Street,Bostom ,Massachusetts
[root@node201 sed]# sed -e 's/MA/,Massachusetts/' -e 's/PA/,Pennsylvania/' list
John Daggett,341 King Road,Plymouth ,Massachusetts
Alice Ford,22 East Broadway,Richmond VA
Orville Thomas,11345 0ak Bridge Road,Tulsa OK
Terry Kalkas,402 Lans Road,Beaver Falls ,Pennsylvania
Eric Adams,20 Post Road,Sudbury ,Massachusetts
Hubert Sims,328A Brook Road,Roanoke VA
Amy Wilde ,334 Bayshore Pkwy,Mountain View CA
Sal Carpenter,73 6th Street,Bostom ,Massachusetts
[root@node201 sed]# cat scriptfile
#Usage:sed -f scriptfile list
s/MA/,Massachusetts/
s/PA/,Pennsylvania/
s/CA/,California/
s/VA/,Virginia/
s/OK/,Oklahoma/
输入重定向:
[root@node201 sed]# sed -f scriptfile list >newlist
[root@node201 sed]# cat newlist
John Daggett,341 King Road,Plymouth ,Massachusetts
Alice Ford,22 East Broadway,Richmond ,Virginia
Orville Thomas,11345 0ak Bridge Road,Tulsa ,Oklahoma
Terry Kalkas,402 Lans Road,Beaver Falls ,Pennsylvania
Eric Adams,20 Post Road,Sudbury ,Massachusetts
Hubert Sims,328A Brook Road,Roanoke ,Virginia
Amy Wilde ,334 Bayshore Pkwy,Mountain View ,California
Sal Carpenter,73 6th Street,Bostom ,Massachusetts
[root@node201 sed]# sed -f scriptfile list
John Daggett,341 King Road,Plymouth ,Massachusetts
Alice Ford,22 East Broadway,Richmond ,Virginia
Orville Thomas,11345 0ak Bridge Road,Tulsa ,Oklahoma
Terry Kalkas,402 Lans Road,Beaver Falls ,Pennsylvania
Eric Adams,20 Post Road,Sudbury ,Massachusetts
Hubert Sims,328A Brook Road,Roanoke ,Virginia
Amy Wilde ,334 Bayshore Pkwy,Mountain View ,California
Sal Carpenter,73 6th Street,Bostom ,Massachusetts
-n 与/p要一起使用:只输出受影响的行
[root@node201 sed]# sed -n -e 's/MA/Massachusetts/p' list
John Daggett,341 King Road,Plymouth Massachusetts
Eric Adams,20 Post Road,Sudbury Massachusetts
Sal Carpenter,73 6th Street,Bostom Massachusetts
[root@node201 awk]# awk '{print $0}' list
John Daggett,341 King Road,Plymouth MA
Alice Ford,22 East Broadway,Richmond VA
Orville Thomas,11345 0ak Bridge Road,Tulsa OK
Terry Kalkas,402 Lans Road,Beaver Falls PA
Eric Adams,20 Post Road,Sudbury MA
Hubert Sims,328A Brook Road,Roanoke VA
Amy Wilde ,334 Bayshore Pkwy,Mountain View CA
Sal Carpenter,73 6th Street,Bostom MA
[root@node201 awk]# awk '{print $2}' list
Daggett,341
Ford,22
Thomas,11345
Kalkas,402
Adams,20
Sims,328A
Wilde
Carpenter,73
[root@node201 awk]# awk '/MA/' list
John Daggett,341 King Road,Plymouth MA
Eric Adams,20 Post Road,Sudbury MA
Sal Carpenter,73 6th Street,Bostom MA
[root@node201 awk]# awk '/MA/{print $1}' list
John
Eric
Sal
[root@node201 awk]# awk -F , '/MA/{print $1}' list
John Daggett
Eric Adams
Sal Carpenter
[root@node201 awk]# awk -F, '{print $1; print $2; print $3}' list
John Daggett
341 King Road
Plymouth MA
Alice Ford
22 East Broadway
Richmond VA
Orville Thomas
11345 0ak Bridge Road
Tulsa OK
Terry Kalkas
选项总结:
-f 跟随脚本的文本名
-F 改变字段分隔符
-v 跟随var = value
[root@node201 awk]# cat namestate
s/ CA/,California/
s/ MA/,Massachusetts/
s/ OK/,Oklahome/
s/ PA/,Pennsylvania/
s/ VA/,Virginia/
[root@node201 awk]# sed -f namestate list | awk -F, '{print $4}'
Massachusetts
Virginia
Oklahome
Pennsylvania
Massachusetts
Virginia
California
Massachusetts
[root@node201 awk]#
[root@node201 sed]# cat -n list | sed '9d'
1 John Daggett,341 King Road,Plymouth MA
2 Alice Ford,22 East Broadway,Richmond VA
3 Orville Thomas,11345 0ak Bridge Road,Tulsa OK
4 Terry Kalkas,402 Lans Road,Beaver Falls PA
5 Eric Adams,20 Post Road,Sudbury MA
6 Hubert Sims,328A Brook Road,Roanoke VA
7 Amy Wilde ,334 Bayshore Pkwy,Mountain View CA
8 Sal Carpenter,73 6th Street,Bostom MA
[root@node201 sed]# cat -n list | sed '$d'
1 John Daggett,341 King Road,Plymouth MA
2 Alice Ford,22 East Broadway,Richmond VA
3 Orville Thomas,11345 0ak Bridge Road,Tulsa OK
4 Terry Kalkas,402 Lans Road,Beaver Falls PA
5 Eric Adams,20 Post Road,Sudbury MA
6 Hubert Sims,328A Brook Road,Roanoke VA
7 Amy Wilde ,334 Bayshore Pkwy,Mountain View CA
8 Sal Carpenter,73 6th Street,Bostom MA
[root@node201 sed]# cat -n list | sed '7,$d'
1 John Daggett,341 King Road,Plymouth MA
2 Alice Ford,22 East Broadway,Richmond VA
3 Orville Thomas,11345 0ak Bridge Road,Tulsa OK
4 Terry Kalkas,402 Lans Road,Beaver Falls PA
5 Eric Adams,20 Post Road,Sudbury MA
6 Hubert Sims,328A Brook Road,Roanoke VA
2016-11-16
sed实例
评论
发表评论
姓 名: