https://www.runoob.com/linux/linux-comm-sed.htmlhtml
---linux
Linux sed 命令是利用脚原本处理文本文件。shell
sed 可依照脚本的指令来处理、编辑文本文件。express
Sed 主要用来自动编辑一个或多个文件、简化对文件的反复操做、编写转换程序等。vim
sed [-hnV][-e<script>][-f<script文件>][文本文件]
参数说明:bash
动做说明:测试
在testfile文件的第四行后添加一行,并将结果输出到标准输出,在命令行提示符下输入以下命令:ui
sed -e 4a\newLine testfile
首先查看testfile中的内容以下:google
$ cat testfile #查看testfile 中的内容 HELLO LINUX! Linux is a free unix-type opterating system. This is a linux testfile! Linux test
使用sed命令后,输出结果以下:spa
$ sed -e 4a\newline testfile #使用sed 在第四行后添加新字符串 HELLO LINUX! #testfile文件原有的内容 Linux is a free unix-type opterating system. This is a linux testfile! Linux test newline
将 /etc/passwd 的内容列出而且列印行号,同时,请将第 2~5 行删除!
[root@www ~]# nl /etc/passwd | sed '2,5d' 1 root:x:0:0:root:/root:/bin/bash 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown .....(后面省略).....
sed 的动做为 '2,5d' ,那个 d 就是删除!由于 2-5 行给他删除了,因此显示的数据就没有 2-5 行罗~ 另外,注意一下,本来应该是要下达 sed -e 才对,没有 -e 也行啦!同时也要注意的是, sed 后面接的动做,请务必以 '' 两个单引号括住喔!
只要删除第 2 行
nl /etc/passwd | sed '2d'
要删除第 3 到最后一行
nl /etc/passwd | sed '3,$d'
在第二行后(亦便是加在第三行)加上『drink tea?』字样!
[root@www ~]# nl /etc/passwd | sed '2a drink tea' 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin drink tea 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin .....(后面省略).....
那若是是要在第二行前
nl /etc/passwd | sed '2i drink tea'
若是是要增长两行以上,在第二行后面加入两行字,例如 Drink tea or ..... 与 drink beer?
[root@www ~]# nl /etc/passwd | sed '2a Drink tea or ......\ > drink beer ?' 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin Drink tea or ...... drink beer ? 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin .....(后面省略).....
每一行之间都必需要以反斜杠『 \ 』来进行新行的添加喔!因此,上面的例子中,咱们能够发如今第一行的最后面就有 \ 存在。
将第2-5行的内容取代成为『No 2-5 number』呢?
[root@www ~]# nl /etc/passwd | sed '2,5c No 2-5 number' 1 root:x:0:0:root:/root:/bin/bash No 2-5 number 6 sync:x:5:0:sync:/sbin:/bin/sync .....(后面省略).....
透过这个方法咱们就可以将数据整行取代了!
仅列出 /etc/passwd 文件内的第 5-7 行
[root@www ~]# nl /etc/passwd | sed -n '5,7p' 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
能够透过这个 sed 的以行为单位的显示功能, 就可以将某一个文件内的某些行号选择出来显示。
搜索 /etc/passwd有root关键字的行
nl /etc/passwd | sed '/root/p' 1 root:x:0:0:root:/root:/bin/bash 1 root:x:0:0:root:/root:/bin/bash 2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh 3 bin:x:2:2:bin:/bin:/bin/sh 4 sys:x:3:3:sys:/dev:/bin/sh 5 sync:x:4:65534:sync:/bin:/bin/sync ....下面忽略
若是root找到,除了输出全部行,还会输出匹配行。
使用-n的时候将只打印包含模板的行。
nl /etc/passwd | sed -n '/root/p' 1 root:x:0:0:root:/root:/bin/bash
删除/etc/passwd全部包含root的行,其余行输出
nl /etc/passwd | sed '/root/d' 2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh 3 bin:x:2:2:bin:/bin:/bin/sh ....下面忽略 #第一行的匹配root已经删除了
搜索/etc/passwd,找到root对应的行,执行后面花括号中的一组命令,每一个命令之间用分号分隔,这里把bash替换为blueshell,再输出这行:
nl /etc/passwd | sed -n '/root/{s/bash/blueshell/;p;q}' 1 root:x:0:0:root:/root:/bin/blueshell
最后的q是退出。
除了整行的处理模式以外, sed 还能够用行为单位进行部分数据的搜寻并取代。基本上 sed 的搜寻与替代的与 vi 至关的相似!他有点像这样:
sed 's/要被取代的字串/新的字串/g'
先观察原始信息,利用 /sbin/ifconfig 查询 IP
[root@www ~]# /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 前面的部分予以删除
[root@www ~]# /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 后面的部分予以删除
[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g' 192.168.1.100
一条sed命令,删除/etc/passwd第三行到末尾的数据,并把bash替换为blueshell
nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/' 1 root:x:0:0:root:/root:/bin/blueshell 2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh
-e表示多点编辑,第一个编辑命令删除/etc/passwd第三行到末尾的数据,第二条命令搜索bash替换为blueshell。
sed 能够直接修改文件的内容,没必要使用管道命令或数据流重导向! 不过,由於这个动做会直接修改到原始的文件,因此请你千万不要随便拿系统配置来测试! 咱们仍是使用文件 regular_express.txt 文件来测试看看吧!
regular_express.txt 文件内容以下:
[root@www ~]# cat regular_express.txt runoob. google. taobao. facebook. zhihu- weibo-
利用 sed 将 regular_express.txt 内每一行结尾若为 . 则换成 !
[root@www ~]# sed -i 's/\.$/\!/g' regular_express.txt [root@www ~]# cat regular_express.txt runoob! google! taobao! facebook! zhihu- weibo-
:q:q
利用 sed 直接在 regular_express.txt 最后一行加入 # This is a test:
[root@www ~]# sed -i '$a # This is a test' regular_express.txt [root@www ~]# cat regular_express.txt runoob! google! taobao! facebook! zhihu- weibo- # This is a test
由於 $ 表明的是最后一行,而 a 的动做是新增,所以该文件最后新增 # This is a test!
sed 的 -i 选项能够直接修改文件内容,这功能很是有帮助!举例来讲,若是你有一个 100 万行的文件,你要在第 100 行加某些文字,此时使用 vim 可能会疯掉!由于文件太大了!那怎办?就利用 sed 啊!透过 sed 直接修改/取代的功能,你甚至不须要使用 vim 去修订