linux sed命令常用操作

概述

sed是stream editor的简称,也就是流编辑器。它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”pattern space,接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出

使用语法

sed命令的使用规则是这样的:

sed [option] 'command' input_file

其中option是可选的,常用的option有如下几种:

  • -n 使用安静silent模式(想不通为什么不是-s)。在一般sed的用法中,所有来自stdin的内容一般都会被列出到屏幕上。但如果加上-n参数后,则只有经过sed特殊处理的那一行(或者动作)才会被列出来;
  • -e 直接在指令列模式上进行 sed 的动作编辑;
  • -f 直接将 sed 的动作写在一个文件内, -f filename 则可以执行filename内的sed命令;
  • -r 让sed命令支持扩展的正则表达式(默认是基础正则表达式);
  • -i 直接修改读取的文件内容,而不是由屏幕输出。

常用的命令command有以下几种:

  • a \:追加行append, a \的后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选择的行的后面都加上字符串s;
  • i \:插入行insert,i \后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选中的行的前面都插入字符串s;
  • c \:取代/替换行change,c \后面跟上字符串s(多行字符串可以用\n分隔),则会将当前选中的行替换成字符串s;
  • d:删除行delete,该命令会将当前选中的行删除;
  • p:打印print,该命令会打印当前选择的行到屏幕上;
  • s:替换字符串subs,通常s命令的用法是这样的:1,2s/old/new/g,将old字符串替换成new字符串命令示例

命令示例

假设有一个本地文件test.txt,文件内容如下:

this is first line

this is second line

this is third line

this is fourth line

this fifth line

happy everyday

end

本节将使用该文件详细演示每一个命令的用法

a命令(追加行)

例一

  1. [qifuguang@winwill~]$ sed '1a \add one' test.txt
    this is first line
    add one
    this is second line
    this is third line
    this is fourth line
    this is fifth line
    happy everyday
    end

     

本例命令部分中的1表示第一行,同样的第二行写成2,第一行到第三行写成1,3,用$表示最后一行,比如2,$表示第二行到最后一行中间所有的行(包含第二行和最后一行)。

本例的作用是在第一行之后增加字符串”add one”,从输出可以看到具体效果。

例二

  1. [qifuguang@winwill~]$ sed '1,$a \add one' test.txt
    this is first line
    add one
    this is second line
    add one
    this is third line
    add one
    this is fourth line
    add one
    this is fifth line
    add one
    happy everyday
    add one
    end
    add one

     

本例表示在第一行和最后一行所有的行后面都加上”add one”字符串,从输出可以看到效果。

例三

  1. [qifuguang@winwill~]$ sed '/first/a \add one' test.txt
    this is first line
    add one
    this is second line
    this is third line
    this is fourth line
    this is fifth line
    happy everyday
    end

     

本例表示在包含”first”字符串的行的后面加上字符串”add one”,从输出可以看到第一行包含first,所以第一行之后增加了”add one”

例四

  1. [qifuguang@winwill~]$ sed '/^ha.*day$/a \add one' test.txt
    this is first line
    this is second line
    this is third line
    this is fourth line
    this is fifth line
    happy everyday
    add one
    end

     

本例使用正则表达式匹配行,^ha.*day$表示以ha开头,以day结尾的行,则可以匹配到文件的”happy everyday”这样,所以在该行后面增加了”add one”字符串。

i命令(插入行)

i命令使用方法和a命令一样的,只不过是在匹配的行的前面插入字符串,所以直接将上面a命令的示例的a替换成i即可,在此就不啰嗦了。

c命令(替换行)

例五

  1. [qifuguang@winwill~]$ sed '$c \add one' test.txt
    this is first line
    this is second line
    this is third line
    this is fourth line
    this is fifth line
    happy everyday
    add one

     

本例表示将最后一行替换成字符串”add one”,从输出可以看到效果。

例六

  1. [qifuguang@winwill~]$ sed '4,$c \add one' test.txt
    this is first line
    this is second line
    this is third line
    add one

     

本例将第四行到最后一行的内容替换成字符串”add one”。

例七

  1. [qifuguang@winwill~]$ sed '/^ha.*day$/c \replace line' test.txt
    this is first line
    this is second line
    this is third line
    this is fourth line
    this is fifth line
    replace line
    end

     

本例将以ha开头,以day结尾的行替换成”replace line”。

d命令(删除行)

例八

  1. [qifuguang@winwill~]$ sed '/^ha.*day$/d' test.txt
    this is first line
    this is second line
    this is third line
    this is fourth line
    this is fifth line
    end

     

本例删除以ha开头,以day结尾的行。

例九

  1. [qifuguang@winwill~]$ sed '4,$d' test.txt
    this is first line
    this is second line
    this is third line

     

本例删除第四行到最后一行中的内容。

p命令(打印行)

例十

  1. [qifuguang@winwill~]$ sed -n '4,$p' test.txt
    this is fourth line
    this is fifth line
    happy everyday
    end

     

本例在屏幕上打印第四行到最后一行的内容,p命令一般和-n选项一起使用。

例十一

  1. [qifuguang@winwill~]$ sed -n '/^ha.*day$/p' test.txt
    happy everyday

     

本例打印以ha开始,以day结尾的行。

s命令(替换字符串)

实际运用中s命令式最常使用到的。

例十二

  1. [qifuguang@winwill~]$ sed 's/line/text/g' test.txt
    this is first text
    this is second text
    this is third text
    this is fourth text
    this is fifth text
    happy everyday
    end

     

本例将文件中的所有line替换成text,最后的g是global的意思,也就是全局替换,如果不加g,则只会替换本行的第一个line。

例十三

  1. [qifuguang@winwill~]$ sed '/^ha.*day$/s/happy/very happy/g' test.txt
    this is first line
    this is second line
    this is third line
    this is fourth line
    this is fifth line
    very happy everyday
    end

     

本例首先匹配以ha开始,以day结尾的行,本例中匹配到的行是”happy everyday”这样,然后再将该行中的happy替换成very happy。

例十四

  1. [qifuguang@winwill~]$ sed 's/\(.*\)line$/\1/g' test.txt
    this is first
    this is second
    this is third
    this is fourth
    this is fifth
    happy everyday
    end

     

这个例子有点复杂,先分解一下。首先s命令的模式是s/old/new/g这样的,所以本例的old部分即\(.*\)line$,sed命令中使用\(\)包裹的内容表示正则表达式的第n部分,序号从1开始计算,本例中只有一个\(\)所以\(.*\)表示正则表达式的第一部分,这部分匹配任意字符串,所以\(.*\)line$匹配的就是以line结尾的任何行。然后将匹配到的行替换成正则表达式的第一部分(本例中相当于删除line部分),使用\1表示匹配到的第一部分,同样\2表示第二部分,\3表示第三部分,可以依次这样引用。比如下面的例子:

  1. [qifuguang@winwill~]$ sed 's/\(.*\)is\(.*\)line/\1\2/g' test.txt
    this first
    this second
    this third
    this fourth
    this fifth
    happy everyday
    end

     

正则表达式中is两边的部分可以用\1\2表示,该例子的作用其实就是删除中间部分的is。

实例若干:

常用操作:

#================源文件里面的内容===============================  
[root@jie1 ~]# cat test  
anonymous_enable=YES  
write_enable=YES  
local_umask=022  
xferlog_enable=YES  
connect_from_port_20=YES  
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  
DEVICE="eth0"  
BOOTPROTO="static"  
HWADDR="00:0C:29:90:79:78"  
ONBOOT="yes"  
IPADDR=172.16.22.1  
NETMASK=255.255.0.0  
#======================================================================  
[root@jie1 ~]# sed -i '/DEVICE/c\Ethernet' test   
        #匹配DEVICE的行,替换成Ethernet这行  
[root@jie1 ~]# sed -i 's/static/dhcp/' test       
        #把static替换成dhcp(/,@,#都是前面所说的地址定界符)  
[root@jie1 ~]# sed -i '/IPADDR/s@22\.1@10.12@' test  
        #匹配IPADDR的行,把22.1替换成10.12由于.号有特殊意义所有需要转义  
[root@jie1 ~]# sed -i '/connect/s#YES#NO#' test   
        #匹配connect的行,把YES替换成NO  
[root@jie1 ~]# sed -i 's/bin/tom/2g' test         
        #把所有匹配到bin的行中第二次及第二次之后出现bin替换成tom  
[root@jie1 ~]# sed -i 's/daemon/jerry/2p' test    
        #把所有匹配到bin的行中第二次出现的daemon替换成jerry,并在生产与匹配行同样的行  
[root@jie1 ~]# sed -i 's/adm/boss/2' test         
        #把所有匹配到adm的行中仅仅只是第二次出现的adm替换成boss  
[root@jie1 ~]# sed -i '/root/{s/bash/nologin/;s/0/1/g}' test  
        #匹配root的行,把bash替换成nologin,且把0替换成1  
[root@jie1 ~]# sed -i 's/root/(&)/g' test                   
        #把root用括号括起来,&表示引用前面匹配的字符  
[root@jie1 ~]# sed -i 's/BOOTPROTO/#BOOTPROTO/' test        
        #匹配BOOTPROTO替换成#BOOTPROTO,在配置文件中一般用于注释某行  
[root@jie1 ~]# sed -i 's/ONBOOT/#&/' test                   
        #匹配ONBOOT的行的前面添加#号,在配置文件中也表示注释某行  
[root@jie1 ~]# sed -i '/ONBOOT/s/#//' test                  
        #匹配ONBOOT的行,把#替换成空,即去掉#号,也一般用作去掉#注释  
#================执行以上sed命令之后文件显示的内容====================  
[root@jie1 ~]# cat test  
anonymous_enable=YES  
write_enable=YES  
local_umask=022  
xferlog_enable=YES  
connect_from_port_20=NO  
(root):x:1:1:(root):/(root):/bin/nologin  
bin:x:1:1:tom:/tom:/stom/nologin  
daemon:x:2:2:jerry:/sbin:/stom/nologin  
daemon:x:2:2:jerry:/sbin:/stom/nologin  
adm:x:3:4:boss:/var/adm:/sbin/nologin  
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin  
Ethernet  
#BOOTPROTO="dhcp"  
HWADDR="00:0C:29:90:79:78"  
ONBOOT="yes"  
IPADDR=172.16.10.12  
NETMASK=255.255.0.0  

sed引用变量:(在自动化shell脚本 中也经常会使用到变量)

第一种当sed命令里面没有默认的变量时可以把单引号改成双引号;

第二种当sed命令里面有默认的变量时,那自己定义的变量需要加单引号,且sed里面的语句必须用单引

[root@jie1 ~]# cat >> myfile << EOF  
> hello world  
> i am jie  
> how are you  
> EOF   #先生成一个文件  
[root@jie1 ~]# cat myfile  
hello world  
i am jie  
how are you  
[root@jie1 ~]# name=li  
         #定义一个变量,且给变量赋值  
[root@jie1 ~]# sed -i "s/jie/$name/" myfile  
         #把匹配jie的字符替换成变量的值  
[root@jie1 ~]# cat myfile  
hello world  
i am li  
how are you  
[root@jie1 ~]# sed -i "$a $name" myfile  
          #当sed命令也有默认变量时,在去引用自己定义的变量会出现语法错误  
sed: -e expression #1, char 3: extra characters after command  
[root@jie1 ~]# sed -i '$a '$name'' myfile  
          #在引用自定义的变量时,sed语句必须用单引引住,然后把自定义的变量也用单引号引住  
[root@jie1 ~]# cat myfile  
hello world  
i am li  
how are you  
li  
[root@jie1 ~]#  

sed的其它高级使用:

1)把正在用sed操作的文件的内容写到例外一个文件中

[root@jie1 ~]# cat test   #sed操作的文件中的内容  
Ethernet  
#BOOTPROTO="dhcp"  
HWADDR="00:0C:29:90:79:78"  
ONBOOT="yes"  
IPADDR=172.16.10.12  
NETMASK=255.255.0.0  
[root@jie1 ~]# sed -i 's/IPADDR/ip/w ip.txt' test  
       #把sed操作的文件内容保存到另外一个文件中,w表示保存,ip.txt文件名  
[root@jie1 ~]# cat ip.txt  #查看新文件的内容  
ip=172.16.10.12  
[root@jie1 ~]#  

2)读取一个文件到正在用sed操作的文件中

[root@jie1 ~]# cat myfile   #文件内容  
hello world  
i am li  
how are you  
li  
[root@jie1 ~]# cat test  #将用sed操作的文件的内容  
Ethernet  
#BOOTPROTO="dhcp"  
HWADDR="00:0C:29:90:79:78"  
ONBOOT="yes"  
IPADDR=172.16.10.12  
NETMASK=255.255.0.0  
[root@jie1 ~]# sed  -i '/Ethernet/r myfile' test  
      #在匹配Ethernet的行,读进来另一个文件的内容,读进来的文件的内容会插入到匹配Ethernet的行后  
[root@jie1 ~]# cat test  #再次查看用sed命令操作的行  
Ethernet  
hello world  
i am li  
how are you  
li  
#BOOTPROTO="dhcp"  
HWADDR="00:0C:29:90:79:78"  
ONBOOT="yes"  
IPADDR=172.16.10.12  
NETMASK=255.255.0.0  
[root@jie1 ~]#  

sed的经典例子:

##1)、处理以下文件内容,将域名取出并进行计数排序,如处理:  
http://www.baidu.com/index.<a target="_blank" href="http://www.2cto.com/kf/qianduan/css/" class="keylink" style="border:none; padding:0px; margin:0px; color:rgb(51,51,51); text-decoration:none; font-size:14px">html</a>  
http://www.baidu.com/1.html  
http://post.baidu.com/index.html  
http://mp3.baidu.com/index.html  
http://www.baidu.com/3.html  
http://post.baidu.com/2.html  
得到如下结果:  
域名的出现的次数 域名  
3 www.baidu.com  
2 post.baidu.com  
1 mp3.baidu.com  
[root@localhost shell]# cat file | sed -e ' s/http:\/\///' -e ' s/\/.*//' | sort | uniq -c | sort -rn  
3 www.baidu.com  
2 post.baidu.com  
1 mp3.baidu.com  
[root@codfei4 shell]# awk -F/ '{print $3}' file |sort -r|uniq -c|awk '{print $1"\t",$2}'  
3 www.baidu.com  
2 post.baidu.com  
1 mp3.baidu.com  
##2)、用grep结合sed取出网卡的ip地址  
[root@jie1 ~]# ifconfig | grep -B1 "inet addr" |grep -v "\-\-" |sed -n -e 'N;s/eth[0−9].*\n.*addr:[0−9]{1,3}\.[0−9]{1,3}\.[0−9]{1,3}\.[0−9]{1,3}.*/\1 \2/p' 

 

转载自:https://www.cnblogs.com/sunbeidan/p/4968314.html

https://www.cnblogs.com/ctaixw/p/5860221.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值