shell编程----sed


1. sed

  sed:stream editor(行编辑器),是一种在线编辑器,它一次处理以行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed编辑命令处理缓冲区的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。
  我们之前使用的vim编辑器时交互式编辑器。

sed命令选项

选项描述
-e script在处理输入时,将script中指定的命令添加到运行的命令中
-f file在处理输入时,将file中指定的命令添加到运行的命令中
-n不要为每个命令生成输出,等待script命令来输出

2. 替换

[root@localhost shell]# echo "this is a test"|sed 's/test/testing/'
this is a testing

sed编辑器不会修改原始文本数据

[root@localhost sed]# sed 's/job/work/' test
I like windows shell linux python.I want to a good work.
I like windows shell linux python.I want to a good work.
I like windows shell linux python.I want to a good work.
I like windows shell linux python.I want to a good work.
I like windows shell linux python.I want to a good work.
I like windows shell linux python.I want to a good work.
[root@localhost sed]# cat test 
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.

使用多条sed命令,用;号将命令隔开

[root@localhost sed]# sed -e 's/job/work/;s/windows/java/' test
I like java shell linux python.I want to a good work.
I like java shell linux python.I want to a good work.
I like java shell linux python.I want to a good work.
I like java shell linux python.I want to a good work.
I like java shell linux python.I want to a good work.
I like java shell linux python.I want to a good work.
[root@localhost sed]# sed -e '
s/shell/ljl/
s/linux/xixi/
s/python/haha/' test
I like windows ljl xixi haha.I want to a good job.
I like windows ljl xixi haha.I want to a good job.
I like windows ljl xixi haha.I want to a good job.
I like windows ljl xixi haha.I want to a good job.
I like windows ljl xixi haha.I want to a good job.
I like windows ljl xixi haha.I want to a good job.

将sed操作写入脚本中,使用-f

[root@localhost sed]# sed -f script test
I like windows ljl xixi haha.I want to a good job.
I like windows ljl xixi haha.I want to a good job.
I like windows ljl xixi haha.I want to a good job.
I like windows ljl xixi haha.I want to a good job.
I like windows ljl xixi haha.I want to a good job.
I like windows ljl xixi haha.I want to a good job.
[root@localhost sed]# cat script 
s/shell/ljl/
s/linux/xixi/
s/python/haha/

加g表示将文件中的所有test都替换为trail

[root@localhost sed]# sed 's/test/trail/' test1
This is a trail,which match test.I want to get 100 grade.
[root@localhost sed]# sed 's/test/trail/g' test1
This is a trail,which match trail.I want to get 100 grade.

将第二次出现的test替换为trail

[root@localhost sed]# sed 's/test/trail/2' test1
This is a test,which match trail.I want to get 100 grade.

标记p和-n结合使用,输出更改的内容,没有更改的内容不输出

[root@localhost sed]# vim test2
[root@localhost sed]# sed -n 's/test/trial/p' test2
This is a trial line.
[root@localhost sed]# cat test2
This is a test line.
This is a different line.

w标记表示显示修改的内容,并将修改的内容输出的指定的data文件中

[root@localhost sed]# sed 's/test/trial/w data' test2
This is a trial line.
This is a different line.
[root@localhost sed]# ls
data  script  test  test1  test2
[root@localhost sed]# cat data
This is a trial line.

3. 替换字符

将/使用\转义

[root@localhost sed]# sed 's/\/bin\/bash/\/bin\/csh/' passwd |grep csh
root:x:0:0:root:/root:/bin/csh
student:x:1000:1000:Student User:/home/student:/bin/csh

#和!可以代替\转义字符

[root@localhost sed]# sed 's!/bin/bash!/bin/csh!' passwd |grep csh
root:x:0:0:root:/root:/bin/csh
student:x:1000:1000:Student User:/home/student:/bin/csh
[root@localhost sed]# sed 's#/bin/bash#/bin/csh#' passwd |grep csh
root:x:0:0:root:/root:/bin/csh
student:x:1000:1000:Student User:/home/student:/bin/csh

将第2行的job替换为work

[root@localhost sed]# sed '2s/job/work/' test
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good work.
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.

将第2,3行的job替换为work

[root@localhost sed]# sed '2,3s/job/work/' test
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good work.
I like windows shell linux python.I want to a good work.
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.

将从第二行开始到结尾的job替换为work

[root@localhost sed]# sed '2,$s/job/work/' test
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good work.
I like windows shell linux python.I want to a good work.
I like windows shell linux python.I want to a good work.
I like windows shell linux python.I want to a good work.
I like windows shell linux python.I want to a good work.

组合命令(多个命令)

[root@localhost sed]# sed '2{
> s/python/xixi/
> s/job/work/
> }' test
I like windows shell linux python.I want to a good job.
I like windows shell linux xixi.I want to a good work.
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.
[root@localhost sed]# sed '3,${
s/python/xixi/
s/job/work/
}' test
I like windows shell linux python.I want to a good job.
I like windows shell linux python.I want to a good job.
I like windows shell linux xixi.I want to a good work.
I like windows shell linux xixi.I want to a good work.
I like windows shell linux xixi.I want to a good work.
I like windows shell linux xixi.I want to a good work.

删除sed编辑器中的内容’d’

[root@localhost sed]# sed '2d' test3
This is line number1.
This is line number3.
This is line number4.
This is line number5.
[root@localhost sed]# sed '3,$d' test3
This is line number1.
This is line number2.

匹配到number1的内容删除

[root@localhost sed]# sed '/number1/d' test3
This is line number2.
This is line number3.
This is line number4.
This is line number5.

number*1匹配r,0次或多次

[root@localhost sed]# cat test3
This is line numberrrr1.
This is line number2.
This is line number3.
This is line number4.
This is line number5.
[root@localhost sed]# sed '/number*1/d' test3
This is line number2.
This is line number3.
This is line number4.
This is line number5.

i表示插入(在指定行前增加一行)

[root@localhost sed]# echo 'Test line2'|sed 'i\Test line1'
Test line1
Test line2

a表示追加(在指定行后增加一行)

[root@localhost sed]# echo 'Test line2'|sed 'a\Test line1'
Test line2
Test line1

在第三行前插入

[root@localhost sed]# sed '3i\This is a inserted line.' test3
This is line number1.
This is line number2.
This is a inserted line.
This is line number3.
This is line number4.
This is line number5.

在第三行后插入

[root@localhost sed]# sed '3a\This is a inserted line.' test3
This is line number1.
This is line number2.
This is line number3.
This is a inserted line.
This is line number4.
This is line number5.

$a表示行尾插入

[root@localhost sed]# sed '$a\This is a inserted line.\nThis is a inserted line2.' test3
This is line number1.
This is line number2.
This is line number3.
This is line number4.
This is line number5.
This is a inserted line.
This is a inserted line2.

采用\的方式多条指令追加

[root@localhost sed]# sed '1i \
> This is one line of line.\
> This is two line of line.' test3
This is one line of line.
This is two line of line.
This is line number1.
This is line number2.
This is line number3.
This is line number4.
This is line number5.

修改第三行

[root@localhost sed]# sed '3c\
> This is changed three line.' test3
This is line number1.
This is line number2.
This is changed three line.
This is line number4.
This is line number5.

采用文本匹配修改第一行

[root@localhost sed]# sed '/number1/c\
> This is changed one line.' test3
This is changed one line.
This is line number2.
This is line number3.
This is line number4.
This is line number5.

替代两行文本,而不是逐一替代

[root@localhost sed]# sed '2,3c\
> This is changed line.' test3
This is line number1.
This is changed line.
This is line number4.
This is line number5.

4. 替换命令

替换单个字符

[root@localhost sed]# sed 'y/1234/6789/' test3
This is line number6.
This is line number7.
This is line number8.
This is line number9.
This is line number5.

p打印

[root@localhost sed]# sed '2,3p' test3
This is line number1.
This is line number2.
This is line number2.
This is line number3.
This is line number3.
This is line number4.
This is line number5.

打印行号

[root@localhost sed]# sed '=' test3
1
This is line number1.
2
This is line number2.
3
This is line number3.
4
This is line number4.
5
This is line number5.

向文件中写入

[root@localhost sed]# sed '1,2w data1' test3
This is line number1.
This is line number2.
This is line number3.
This is line number4.
This is line number5.
[root@localhost sed]# ls
data  data1  passwd  script  test  test1  test2  test3
[root@localhost sed]# cat data1
This is line number1.
This is line number2.

将test4中的内容插入到test3的第三行后面

[root@localhost sed]# sed '3r test4' test3
This is line number1.
This is line number2.
This is line number3.
This is an added line.
This is the third added line.
This is line number4.
This is line number5.
[root@localhost sed]# cat test4
This is an added line.
This is the third added line.

匹配test3中的number2的行,将test4的内容插入到匹配的number2的行的后面

[root@localhost sed]# sed '/number2/r test4' test3
This is line number1.
This is line number2.
This is an added line.
This is the third added line.
This is line number3.
This is line number4.
This is line number5.

5. 练习题

自定义一个文件

  1. 删除头3行
[root@localhost sed]# cat test3
This is line number1.
This is line number2.
This is line number3.
This is line number4.
This is line number5.
This is line number6.
This is line number7.
This is line number8.
This is line number9.
This is line number10.
This is line number11.
end
[root@localhost sed]# sed '1,3d' test3 
This is line number4.
This is line number5.
This is line number6.
This is line number7.
This is line number8.
This is line number9.
This is line number10.
This is line number11.
end
  1. 2.打印第5~10行
[root@localhost sed]# cat test3
This is line number1.
This is line number2.
This is line number3.
This is line number4.
This is line number5.
This is line number6.
This is line number7.
This is line number8.
This is line number9.
This is line number10.
This is line number11.
[root@localhost sed]# sed -n '5,10p' test3 
This is line number5.
This is line number6.
This is line number7.
This is line number8.
This is line number9.
This is line number10.
  1. 删除含有line的所有行
[root@localhost sed]# cat test3
This is line number1.
This is line number2.
This is line number3.
This is line number4.
This is line number5.
This is line number6.
This is line number7.
This is line number8.
This is line number9.
This is line number10.
This is line number11.
end
[root@localhost sed]# sed '/line/d' test3
end
  1. 把/etc/passwd复制到/root/test.txt,用sed打印所有行
[root@localhost sed]# cp /etc/passwd /root/test.txt|cat /root/test.txt |sed '=' /root/test.txt
1
root:x:0:0:root:/root:/bin/bash
2
bin:x:1:1:bin:/bin:/sbin/nologin
3
daemon:x:2:2:daemon:/sbin:/sbin/nologin
4
adm:x:3:4:adm:/var/adm:/sbin/nologin
5
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6
sync:x:5:0:sync:/sbin:/bin/sync
7
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  1. 删除test.txt的15行以及以后的所有行
[root@localhost sed]# sed '15,$d' /root/test.txt
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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/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
  1. 替换test.txt中‘/sbin/nologin’为‘/bin/login’
[root@localhost sed]# sed 's!/sbin/nologin!/bin/login!' /root/test.txt |grep login
bin:x:1:1:bin:/bin:/bin/login
daemon:x:2:2:daemon:/sbin:/bin/login
adm:x:3:4:adm:/var/adm:/bin/login
lp:x:4:7:lp:/var/spool/lpd:/bin/login
mail:x:8:12:mail:/var/spool/mail:/bin/login
operator:x:11:0:operator:/root:/bin/login
games:x:12:100:games:/usr/games:/bin/login
ftp:x:14:50:FTP User:/var/ftp:/bin/login
nobody:x:99:99:Nobody:/:/bin/login
dbus:x:81:81:System message bus:/:/bin/login
polkitd:x:999:998:User for polkitd:/:/bin/login
  1. 删除test.txt中5到10行中所有的数字
[root@localhost sed]# sed -n '5,10p' /root/test.txt
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
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost sed]# sed -n '5,10p' /root/test.txt | sed 's/[0-9]//g'
lp:x:::lp:/var/spool/lpd:/sbin/nologin
sync:x:::sync:/sbin:/bin/sync
shutdown:x:::shutdown:/sbin:/sbin/shutdown
halt:x:::halt:/sbin:/sbin/halt
mail:x:::mail:/var/spool/mail:/sbin/nologin
operator:x:::operator:/root:/sbin/nologin
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值