sed

  流编辑器,逐行处理文本内容。处理方式由子命令指定,在内存中完成处理,默认不修改源文件。

语法格式:sed 【-选项】 '/过滤筛选/子命令/子命令选项' 文本对象文件
 

  选项:

    n 静默模式

    i 修改源文件

    e 多次执行命令

    f 指定sed脚本文件

    r 扩展正则表达式

  过滤筛选:

    1  处理第一行

    1,3  第1行开始,到第三行

    3,+2  第3行开始处理,往后2行……

    /Regular Expression/  正则表达式匹配相应行

    /RE1/,/RE2/  从匹配到的第一个正则表达式表示的行,到第二个……

    

 

  子命令:

    d 删除

    p 显示

    a 后增加一行内容(a“string”)

    i 前增加一行

    s 替换内容

    r 行后读取一个文件的内容

    w 选定的行输出到文件

  子命令选项:
   g 替换时全局替换

 

例子

$ cat setip.sh
ifconfig eth0 190.15.11.26 netmask 255.255.255.0 up
ifconfig eth0:0 190.25.12.27 netmask 255.255.255.0 up
ifconfig eth1 192.168.11.15 netmask 255.255.255.0 up

1 取出指定行的内容

  在脚本中依赖了一个环境变量,而且注释部分包含了生成环境变量的定义。需要取出这些行的内容可以这么用:

$ sed -n '7,8p' example.sh
# DATE=`which --skip-alias date`
# export ZCW_TS=`$DATE +%Y%m%d%H`

  选项“-n”,过滤了其他内容的显示;默认情况下,sed会逐行全部打印。

  使用shell变量,获取行号。在sed中使用shell变量时,需要加上单引号:

$ k=3
$ sed -n '1,'$k'p' example.sh
1
2
3

2 过滤

  找出java池程序。

[root@iZ28lyaw0o0Z ~]# netstat -nltp | sed '/java/p'
tcp        0      0 127.0.0.1:28005             0.0.0.0:*                   LISTEN      10109/java          
tcp        0      0 0.0.0.0:52168               0.0.0.0:*                   LISTEN      10109/java          
tcp        0      0 0.0.0.0:28009               0.0.0.0:*                   LISTEN      10109/java

  打印出包含“44”的行,直到文件最后一行。

#  sed -n '/44/,/$$/p' test/hhaa.txt

  打印出包含某个变量“tmp_datestamp”的行,直到文件最后一行。

# sed -n '/'$tmp_datestamp'/,/$$/p' $TMPFILE

3 替换修改

  修改主ip为181.37.1.43;备用ip为182.37.4.43。

$ sed '/eth0 /s/190.15.11.26/181.37.1.43/' setip.sh
ifconfig eth0 181.37.1.43 netmask 255.255.255.0 up
ifconfig eth0:0 190.25.12.27 netmask 255.255.255.0 up
ifconfig eth1 192.168.11.15 netmask 255.255.255.0 up
$ sed '/eth0:/s/190.25.12.27/182.37.4.43/' setip.sh
ifconfig eth0 190.15.11.26 netmask 255.255.255.0 up
ifconfig eth0:0 182.37.4.43 netmask 255.255.255.0 up
ifconfig eth1 192.168.11.15 netmask 255.255.255.0 up

  默认情况下,替换时只对行中首次匹配的内容替换,一行中所有内容都需要替换,需要显式指定。

  符号“&”代表匹配模式的字串。

# sed 's/one/first-&/' demo.txt

  把“空白符”变成“换行符”,便于过滤出有用信息:

[root@iZ286nwssi4Z ~]# ps -p 9003 -o cmd | sed 's/[[:space:]]/
/g'
CMD
/usr/lib/jdk/jdk1.7.0_79/bin/java
-Djava.util.logging.config.file=/home/zhaocai/tomcat-p2p/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.endorsed.dirs=/home/zhaocai/tomcat-p2p/endorsed
-classpath
/home/zhaocai/tomcat-p2p/bin/bootstrap.jar:/home/zhaocai/tomcat-p2p/bin/tomcat-juli.jar
-Dcatalina.base=/home/zhaocai/tomcat-p2p
-Dcatalina.home=/home/zhaocai/tomcat-p2p
-Djava.io.tmpdir=/home/zhaocai/tomcat-p2p/temp
org.apache.catalina.startup.Bootstrap
start

4 删除行

  删除第3行

# sed '3d' setip.sh
ifconfig eth0 190.15.11.26 netmask 255.255.255.0 up
ifconfig eth0:0 190.25.12.27 netmask 255.255.255.0 up

5 插入文本

  匹配行的前面、后面分别插入内容

$ sed '/eth0 /iPrimary ip address.' setip.sh
Primary ip address.
ifconfig eth0 190.15.11.26 netmask 255.255.255.0 up
ifconfig eth0:0 190.25.12.27 netmask 255.255.255.0 up
ifconfig eth1 192.168.11.15 netmask 255.255.255.0 up
$ sed '/eth0 /aSlave ip address.' setip.sh
ifconfig eth0 190.15.11.26 netmask 255.255.255.0 up
Slave ip address.
ifconfig eth0:0 190.25.12.27 netmask 255.255.255.0 up
ifconfig eth1 192.168.11.15 netmask 255.255.255.0 up

6 组合命令

  需要带上选项“-e”,执行多个“子命令”。 保留列头部分,剔除java以外的信息。

[root@iZ28lyaw0o0Z ~]# netstat -nltp | sed -e '1,2p' -e '/java/p'
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 127.0.0.1:28005             0.0.0.0:*                   LISTEN      10109/java          
tcp        0      0 0.0.0.0:52168               0.0.0.0:*                   LISTEN      10109/java          
tcp        0      0 0.0.0.0:28009               0.0.0.0:*                   LISTEN      10109/java

  获取文件的修改时间,文件多了还要能看清楚文件与时间对应关系。

#!/bin/bash
# 
if [ 0 -eq $# ]; then
    echo "Warning, without parameters."
    exit 1
fi
stat $@ | sed -n -e '/File:/p' -e '/Modify:/p' 2> /dev/null

  测试运行:

# modTime 
Warning, without parameters.
# modTime zcw_Virtualfile-1.3.sh 
  File: ‘zcw_Virtualfile-1.3.sh’
Modify: 2017-02-08 16:19:32.239958148 +0800

7 使用sed脚本

  把多条子命令放在一个sed脚本中,使用“-f”选项一次执行: sed -f sed.script setip.sh

$ cat sed.script
/eth0 /i# Primary ip address.
/eth0 /a# Slave ip address.
$ sed -f sed.script setip.sh
# Primary ip address.
ifconfig eth0 190.15.11.26 netmask 255.255.255.0 up
# Slave ip address.
ifconfig eth0:0 190.25.12.27 netmask 255.255.255.0 up
ifconfig eth1 192.168.11.15 netmask 255.255.255.0 up

  还可以直接使用sed脚本处理目标文本。类似bash shell脚本一样,首行指定解释该脚本的命令"#!/bin/sed -f"。

$ cat sed.script
#!/bin/sed -f
/eth0 /i# Primary ip address.
/eth0 /a# Slave ip address.
$ ./sed.script setip.sh
# Primary ip address.
ifconfig eth0 190.15.11.26 netmask 255.255.255.0 up
# Slave ip address.
ifconfig eth0:0 190.25.12.27 netmask 255.255.255.0 up
ifconfig eth1 192.168.11.15 netmask 255.255.255.0 up

8 子命令r、w

  使用“w”命令把eth的配置输出到文件“eth0.set”中。

$ sed '/eth0/weth0.set' setip.sh
$ cat eth0.set
ifconfig eth0 190.15.11.26 netmask 255.255.255.0 up
ifconfig eth0:0 190.25.12.27 netmask 255.255.255.0 up

  同样可以从文件读入一并输出

$ sed '3reth0.set' setip.sh
ifconfig eth0 190.15.11.26 netmask 255.255.255.0 up
ifconfig eth0:0 190.25.12.27 netmask 255.255.255.0 up
ifconfig eth1 192.168.11.15 netmask 255.255.255.0 up
ifconfig eth0 190.15.11.26 netmask 255.255.255.0 up
ifconfig eth0:0 190.25.12.27 netmask 255.255.255.0 up
一切代码都是为了生活,一切生活都是调剂
原文地址:https://www.cnblogs.com/argor/p/7909047.html