Linux中的sed命令

时间:2023-11-16 10:30:08

sed - stream editor for filtering and transforming text

   流编辑器的过滤和转换文本

sed [-nerf] [动作]

参数:

  -i 修改源文件 危险

  -e 直接在命令行模式上执行sed的动作编辑

  -f 直接将sed的动作写在一个文件内,-f filename 则可以执行filename内的sed动作

  -r :使用扩展的正则表达式

    使用扩展的正则表达式需要“//”正斜线

[root@lamp scripts]# cat sed11.txt 

[root@lamp scripts]# sed -nr '/2|3/p' sed11.txt    //打印包含2或3的行        

[root@lamp scripts]# sed -nr '/2|3$/p' sed11.txt     //打印以2或3结尾的行,但是打印的是以包含2和以3结尾的行

[root@lamp scripts]# sed -nr '/(2|3)$/p' sed11.txt          //这个才是正确的以2或3结尾的行

[root@lamp scripts]# 

  -n 静默模式,默认的sed中所有来自stdin的数据一般都会被列出到屏幕上,但如果加上-n之后,则只有经过sed特殊处理的那一行才会被列出来。

[root@mysql tmp]# sed "/^a/p" sed.txt   //将以”a“开头的行打印出来,可以看到b也被打印出来了,并且a被打印了两遍,一遍是sed默认就是将模式空间中的内容打印出来,第二遍是p参数又打印了一遍
a
a
b
[root@mysql tmp]# sed -n "/^a/p" sed.txt  //使用-n参数,只讲匹配到的内容打印出来
a
[root@mysql tmp]# 

 "n"和“i”不能同时使用

[root@lamp scripts]# sed -ni "s#Port 22#Port 52113#p" /tmp/sshd_config
[root@lamp scripts]# cat /tmp/sshd_config                         //啥都没有了,经测试只有“-ni”会发生这种情况,“-in”则不会
#Port
[root@lamp scripts]# 

动作说明:[n1,n2] function

  1.LineNumber

    精确匹配的行

  2.StarLine, +N

    从startline开始,到之后的n行结束,一共是n+1行

   3.StartlLine~n

    从指定行开始指定步长

[root@lamp scripts]# cat sed.txt.
one
two
three
four
five
[root@lamp scripts]# sed -n '1~2p' sed.txt. //从第一行开始只有1,3,5行被打印出来
one
three
five
[root@lamp scripts]# 

  4.StartLine,EndLine

    $ 表示最后一行

[root@mysql tmp]# sed -n "1,2p" /etc/passwd    //打印1-2行的内容
root:x:::root:/root:/bin/bash
bin:x:::bin:/bin:/sbin/nologin
[root@mysql tmp]# sed -n "30,\$p" /etc/passwd   //答应30到最后一行的内容
mysql:x::::/home/mysql:/sbin/nologin
apache:x:::Apache:/var/www:/sbin/nologin
[root@mysql tmp]# sed -n "30p" /etc/passwd        //打印第30行的内容
mysql:x::::/home/mysql:/sbin/nologin

  5./匹配模式/    支持正则表达式

    如:/^root/

  6./partern/,/partern/

    第一次被partern1匹配到的行开始,至第一次被partern2匹配到的行结束,     这中间的所有行

[root@mysql tmp]# sed -n "/bash$/p" /etc/passwd     //打印以bash结尾的行
root:x:::root:/root:/bin/bash
along:x::::/home/along:/bin/bash
[root@mysql tmp]# sed -n "/^rpc/p" /etc/passwd      //打印以rpc开头的行
rpc:x:::Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rpcuser:x:::RPC Service User:/var/lib/nfs:/sbin/nologin
[root@mysql tmp]# sed -n "/^\(.*\)root/p" /etc/passwd    //打印以任意字符开头,中间有root的行
root:x:::root:/root:/bin/bash
operator:x:::operator:/root:/sbin/nologin
[root@mysql tmp]# 

function  

  = 打印行号

[root@lamp scripts]# sed -n '1~2=' sed.txt.  //打印1 3 5行号

[root@lamp scripts]# sed -n '1~2=;p' sed.txt.  //不仅打印行号,内容也打印,但是结果显示是将所有的内容都打印了,因为“p”操作前边什么都没有指定,默认就是将所有的打印

one
two

three
four

five
[root@lamp scripts]# sed -n '1~2{=;p}' sed.txt. //针对同一内容的不同操作应该使用“{}”括起来,并且不同的操作之间应该使用分号隔开

one

three

five
[root@lamp scripts]# 

  d:删除符合条件的行

  p:打印符合条件的行

    使用此命令的话,指定的内容会打印两次,这是因为sed命令默认的就是 将模式空间中的内容打印出来,而p也是打印的意思,所以会重复打印

  a \string : 在指定的行后面追加新的行,内容是string

    a的意思append追加的意思,即在指定行的后边追加内容

[root@mysql tmp]# cat sed.txt
a
b
[root@mysql tmp]# a="c"                   //设置一个变量a
[root@mysql tmp]# sed "2a $a" sed.txt     //在第二行后边追加变量a的值
a
b
c
[root@mysql tmp]# sed "2a \$a" sed.txt    //特殊符号需要转义
a
b
$a
[root@mysql tmp]# 

  i \string:  在指定的行前面添加新的行,内容是string

    ”i“就是insert的意思

[root@mysql tmp]# cat sed.txt
a
b
[root@mysql tmp]# sed '1i 1' sed.txt      //在指定行的前边添加一行

a
b
[root@mysql tmp]# 

  r File:将指定文件的内容添加至符合条件的行处

[root@mysql tmp]# cat sed.txt
a
d
[root@mysql tmp]# cat sed1.txt
b
c
[root@mysql tmp]# sed "2r ./sed1.txt" sed.txt   //将sed1.txt的内容在sed.txt的第二行开始添加
a
d
b
c
[root@mysql tmp]# 

  w File:将指定范围的内容添加至指定的文件中去

[root@mysql tmp]# sed -n "30,\$w ./a.txt" /etc/passwd    //将passwd文件的最后两行写入到a.txt文件中去
[root@mysql tmp]# cat a.txt
mysql:x::::/home/mysql:/sbin/nologin
apache:x:::Apache:/var/www:/sbin/nologin
[root@mysql tmp]# 

  s /partern1/partern2/修饰符  查找替换 默认只替换第一次被匹配到的字符串

    不仅可使用s ///也可以使用s ### ,s @@@等,s 命令也可以支持后向引用

将garnett替换成kobe,html替换成HTML

[root@lamp ~]# echo garnett-linux.html|sed "s#garnett\(.*\)html#kobe\1HTML#g"
kobe-linux.HTML
[root@lamp ~]# 

修饰符:

  g:全局替换

[root@lamp scripts]# head /etc/passwd|sed "s#[a-z]##" //发现只是将每一行的第一个字母替换成空 ,因为”s“默认只替换第一次被匹配到的字符串
oot:x:::root:/root:/bin/bash,
::bin:/bin:/sbin/nologin
aemon:x:::daemon:/sbin:/sbin/nologin
dm:x:::adm:/var/adm:/sbin/nologin
p:x:::lp:/var/spool/lpd:/sbin/nologin
ync:x:::sync:/sbin:/bin/sync
hutdown:x:::shutdown:/sbin:/sbin/shutdown
alt:x:::halt:/sbin:/sbin/halt
ail:x:::mail:/var/spool/mail:/sbin/nologin
ucp:x:::uucp:/var/spool/uucp:/sbin/nologin
[root@lamp scripts]# head /etc/passwd|sed "s#[a-z]##g"   //加上"g"参数即可,可以重复替换
:::::/://
:::::/://
:::::/://
::::://://
:::::///://
:::::/://
:::::/://
:::::/://
:::::///://
:::::///://
[root@lamp scripts]# 

  i:忽略大小写  

实战:

  使用sed命令取ip地址

[root@lamp scripts]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr :0C:::CD:
          inet addr:192.168.220.129  Bcast:192.168.220.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe17:cd98/ Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:  Metric:
          RX packets: errors: dropped: overruns: frame:
          TX packets: errors: dropped: overruns: carrier:
          collisions: txqueuelen:
          RX bytes: ( (208.2 KiB)

[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*)  Bc(.*)#\2#gp"
192.168.220.129
[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*)  Bcast:(.*) (.*)#\2#gp"
192.168.220.129
[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*)  Bcast:(.*) (.*)#\2\3#gp"

[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*)  Bcast:(.*) (.*)#\2 \3#gp"
192.168.220.129 192.168.220.255
[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*)  Bcast:(.*) (.*)#\2\t\3#gp"
192.168.220.129 192.168.220.255
[root@lamp scripts]#
[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*)  Bcast:(.*) (.*)#ping \2#gp"
ping 192.168.220.129
[root@lamp scripts]# ifconfig eth0|sed -nr "s#^(.*)ddr:(.*)  Bcast:(.*) (.*)#ping \2#gp"|bash
PING () bytes of data.
 bytes from  ttl= time=0.032 ms
 bytes from  ttl= time=0.033 ms
 bytes from  ttl= time=0.045 ms
 bytes from  ttl= time=0.040 ms

^C
--- 192.168.220.129 ping statistics ---
 packets transmitted,  received, % packet loss, time 8225ms
rtt min/avg/max/mdev = 0.022/0.038/0.049/0.008 ms
[root@lamp scripts]#