sed 利用脚本来处理文本文件

作用

sed 命令是利用脚本来处理文本文件。sed 可依照脚本的指令来处理、编辑文本文件。Sed 主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。

语法

sed [-hnV][-e<script>][-f<script文件>][文本文件]

参数

  • -e<script>或--expression=<script> 以选项中指定的script来处理输入的文本文件。

  • -f<script文件>或--file=<script文件> 以选项中指定的script文件来处理输入的文本文件。

  • -h或--help 显示帮助。

  • -n或--quiet或--silent 仅显示script处理后的结果。

  • -V或--version 显示版本信息。

动作说明:

  • a  新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~

  • c  取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!

  • d  删除,因为是删除啊,所以 d 后面通常不接任何东东;

  • i  插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);

  • p  打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~

  • s  取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正则表达式!例如 1,20s/old/new/g 就是啦!

示例

我们先创建一个 demo 文件,内容如下:

# 查看 demo 中的内容
hxstrive@localhost:~$ cat demo
See You agaie
It's been a lnng day without you my friend
And I'll tell you all about it when I see you again
We've come a long way from where we began
Oh I'll tell you all about it when I see you again
When I see you again

(1)新增一行。在 demo 文件的第四行后添加一行,并将结果输出到标准输出,命令如下:

hxstrive@localhost:~$ sed -e 4a\newLine demo
See You agaie
It's been a lnng day without you my friend
And I'll tell you all about it when I see you again
We've come a long way from where we began
newLine
Oh I'll tell you all about it when I see you again
When I see you again

(2)以行为单位的新增/删除。将 demo 的内容列出并且列出行号,同时,请将第 2~4 行删除!

hxstrive@localhost:~$ nl demo | sed '2,4d'
     1See You agaie
     5Oh I'll tell you all about it when I see you again
     6When I see you again

注意:sed 的动作为 2,4d,那个 d 是删除的意思,因为删除了 2-4 行,所以显示的数据就没有 2-4 行了。另外,原本应该是要下达 sed -e 才对,但没有 -e 也是可以的,同时也要注意的是,sed 后面接的动作,请务必以 '...' 两个单引号括住喔!

(3)只要删除第 2 行,命令如下:

hxstrive@localhost:~$ nl demo | sed '2d'
     1See You agaie
     3And I'll tell you all about it when I see you again
     4We've come a long way from where we began
     5Oh I'll tell you all about it when I see you again
     6When I see you again

(4)要删除第 3 到最后一行,命令如下:

hxstrive@localhost:~$ nl demo | sed '3,$d'
     1See You agaie
     2It's been a lnng day without you my friend

(5)在第二行后(即加在第三行)加上 “drink tea?” 字符串,命令如下:

hxstrive@localhost:~$ nl demo | sed '2a drink tea'
     1See You agaie
     2It's been a lnng day without you my friend
drink tea
     3And I'll tell you all about it when I see you again
     4We've come a long way from where we began
     5Oh I'll tell you all about it when I see you again
     6When I see you again

(6)如果是要在第二行前,命令如下:

hxstrive@localhost:~$ nl demo | sed '2i drink tea'
     1See You agaie
drink tea
     2It's been a lnng day without you my friend
     3And I'll tell you all about it when I see you again
     4We've come a long way from where we began
     5Oh I'll tell you all about it when I see you again
     6When I see you again

(7)如果是要增加两行以上,在第二行后面加入两行字,例如 “Drink tea or .....” 与 “drink beer?” 字符串。命令如下:

hxstrive@localhost:~$ nl demo | sed '2a Drink tea or ......\
drink beer ?'
     1See You agaie
     2It's been a lnng day without you my friend
Drink tea or ......
drink beer ?
     3And I'll tell you all about it when I see you again
     4We've come a long way from where we began
     5Oh I'll tell you all about it when I see you again
     6When I see you again

注意:每一行之间都必须要以反斜杠 \ 来进行新行标记。上面的例子中,我们可以发现在第一行的最后面就有 \ 存在。

(8)以行为单位的替换与显示,将第 2-4 行的内容取代成为 “No 2-5 number” ,命令如下:

hxstrive@localhost:~$ nl demo | sed '2,4c No 2-5 number'
     1See You agaie
No 2-5 number
     5Oh I'll tell you all about it when I see you again
     6When I see you again

注意:透过这个方法我们就能够将数据整行取代了。

(9)仅列出 demo 文件内的第 3-4 行,命令如下:

hxstrive@localhost:~$ nl demo | sed -n '3,4p'
     3And I'll tell you all about it when I see you again
     4We've come a long way from where we began

(10)数据的搜寻并显示,搜索 demo 有 on 关键字的行,命令如下:

hxstrive@localhost:~$ nl demo | sed -n '/on/p'
     4We've come a long way from where we began

(11)数据的搜寻并删除,删除 demo 所有包含 on 的行,命令如下:

hxstrive@localhost:~$ nl demo | sed  '/on/d'
     1See You agaie
     2It's been a lnng day without you my friend
     3And I'll tell you all about it when I see you again
     5Oh I'll tell you all about it when I see you again
     6When I see you again

(12)数据的搜寻并执行命令,搜索 demo 文件,找到 on 对应的行,执行后面花括号中的一组命令,每个命令之间用分号分隔,这里把 on 替换为 kk,再输出这行:

hxstrive@localhost:~$ nl demo | sed -n '/on/{s/on/kk/;p;q}'
     4We've come a lkkg way from where we began

注意:最后的 q 是退出。

(13)数据的查找与替换,除了整行的处理模式之外,sed 还可以用行为单位进行部分数据的查找与替换 <。sed 的查找与替换的与 vi 命令类似,语法格式如下:

sed 's/要被取代的字串/新的字串/g' 将 demo 文件中每行第一次出现的 on 用字符串 kk 替换,然后将该文件内容输出到标准输出:

hxstrive@localhost:~$ sed -e 's/on/kk/' demo
See You agaie
It's been a lnng day without you my friend
And I'll tell you all about it when I see you again
We've come a lkkg way from where we began
Oh I'll tell you all about it when I see you again
When I see you again

g 标识符表示全局查找替换,使 sed 对文件中所有符合的字符串都被替换,修改后内容会到标准输出,不会修改原文件,命令如下:

sed -e 's/on/kk/g' demo

选项 i 使 sed 修改文件,命令如下:

sed -i 's/on/kk/g' demo

批量操作当前目录下以 test 开头的文件,命令如下:

sed -i 's/on/kk/g' ./test*

(14)接下来我们使用 /sbin/ifconfig 查询 IP,命令如下:

hxstrive@localhost:~$ /sbin/ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
...

本机的 ip 是 192.168.1.100。将 IP 前面的部分予以删除:

hxstrive@localhost:~$ /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'
192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

接下来则是删除后续的部分,即:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0。

将 IP 后面的部分予以删除:

hxstrive@localhost:~$ /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
192.168.1.100

(15)多点编辑,一条 sed 命令,删除 demo 第三行到末尾的数据,并把 you 替换为 ###:

hxstrive@localhost:~$ nl demo | sed -e '3,$d' -e 's/you/###/'
     1See You agaie
     2It's been a lnng day without ### my friend

注意:-e 表示多点编辑,第一个编辑命令删除 demo 第三行到末尾的数据,第二条命令搜索 you 替换为 ###。

(16)直接修改文件内容(危险动作),sed 可以直接修改文件的内容,不必使用管道命令或数据流重导向!不过,由于这个动作会直接修改到原始的文件,所以请你千万不要随便拿系统配置来测试!我们还是使用文件 demo.txt 文件来测试看看吧!

demo.txt 文件内容如下:

$ cat demo.txt 
runoob.
google.
taobao.
facebook.
zhihu-
weibo-

利用 sed 将 demo.txt 内每一行结尾若为 “.” 则换成 “!”,命令如下:

$ sed -i 's/\.$/\!/g' demo.txt
$ cat demo.txt 
google!
taobao!
facebook!
zhihu-
weibo-:q:q

利用 sed 直接在 demo.txt 最后一行加入 # This is a test:

$ sed -i '$a # This is a test' demo.txt
$ cat demo.txt 
google!
taobao!
facebook!
zhihu-
weibo-
# This is a test

注意:由于 $ 代表的是最后一行,而 a 的动作是新增,因此该文件最后新增 # This is a test!

sed 的 -i 选项可以直接修改文件内容,这功能非常有帮助!举例来说,如果你有一个 100 万行的文件,你要在第 100 行加某些文字,此时使用 vim 可能会疯掉!因为文件太大了!那怎办?就利用 sed 啊!通过 sed 直接修改/取代的功能,你甚至不需要使用 vim 去修订!

更多关于命令详细参考手册,请使用 man 命令或者 --help 参数获取帮助信息

关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号