sed

 

My heart is with you!  
You're uinique, nothing can replace you.
all or nothing, now or never.  
she who has never loved, has never lived

  在第四行后面添加一行,并将结果输出到标准输出(不修改原文件):

$ sed -e 4a
ewline dayone #使用sed 在第四行后添加新字符串  
My heart is with you! #dayone文件原有的内容  
You're uinique, nothing can replace you..  
all or nothing, now or never.  
she who has never loved, has never lived  
newline 

以行为单位的新增/删除

  • 将 /etc/passwd 的内容列出并且列印行号,同时,请将第 2到第5 行删除!
[root@www ~]# nl /etc/passwd | sed '2,5d'

只要不加-i就不会修改原文件,   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'

  

  • 那如果是要在第二行前
nl /etc/passwd | sed '2i drink tea'

  

  • 如果是要增加两行以上,在第二行后面加入两行字,例如 Drink tea or ..... 与 drink beer?
[root@www ~]# nl /etc/passwd | sed '2a Drink tea or ......
> drink beer ?'

  

以行为单位的替换与显示

  • 将第2-5行的内容取代成为『No 2-5 number』呢?
[root@www ~]# nl /etc/passwd | sed '2,5c No 2-5 number'
  • 仅列出 /etc/passwd 文件内的第 5-7 行
[root@www ~]# nl /etc/passwd | sed -n '5,7p'

数据的搜寻并显示

  • 搜索 /etc/passwd有root关键字的行
nl /etc/passwd | sed '/root/p'  #除了输出所有行,还会输出匹配行。
nl /etc/passwd | sed -n '/root/p'  #使用-n的时候将只打印包含模板的行

数据的搜寻并删除

删除/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

原文地址:https://www.cnblogs.com/linuxws/p/10766348.html