shell脚本编程-sed命令

1、sed编辑器使用

sed编辑器被称作流编辑器,基于预先提供一组规则来编辑数据流。
格式:sed option script file
(1)选项option
    -e script  在处理输入时,将script中指定的命令添加到运行的命令中,可以同时处理多个命令;
    -f file 在处理输入时,将file中指定的命令添加到运行的命令中;
    -n 不为每个命令生成输出,等待print命令来输出
(2)sed基本使用
在命令行使用sed编辑器,例如替换命令
    $  echo "this is a test " | sed 's/test/big test/'
    this is a big test
    使用s标识替换命令,'s/src/dst/',将命令行回显的数据中的src替换为dst。
编辑文件中的数据,需要指定需要处理的文件
    $ cat test
    There are two dogs
    $ sed 's/dogs/cats/' test
    There are two cats
    需要说明的是,sed不会修改文件中的数据,只是将数据编辑后输出到STDOUT上。
需要指定多个处理命令时,需要使用-e选项。
    $ sed 's/dogs/cats/; s/two/three/' test
    There are two cats
    命令之间需要分号隔开,并且命令末尾和分号之间不能有空格。
从文本中读取处理命令,使用-f选项指定命令文件
    $ cat script
    s/dogs/cats/
    s/two/three/
    $ sed -f script test

2、替换命令

(1)格式:s/src/dst/flags
(2)替换标记
    数字:表明文本中每行的第几个匹配需要被替换
    g:表明文本中每行出现的所有的匹配数据都会被替换
    p:表明修改行的内容需要被打印出来
    w fil:将替换的结果写到文件中
默认情况下,替换命令只会修改每行文本中第一处匹配到的数据;使用数字标记,可以指定文本中每行的第几个匹配需要被替换;使用g标记,可以指定文本中每行的所有匹配需要被替换。
    $ cat test
    There are two cats and two dogs
    There are two cats and two dogs
    $ sed 's/two/three/' test
    There are three cats and two dogs
    There are three cats and two dogs
    $ sed 's/two/three/2' test
    There are two cats and three dogs
    There are two cats and three dogs
    $ sed 's/two/three/g' test
    There are three cats and three dogs
    There are three cats and three dogs
默认情况下sed处理的数据将会被输出到STDOUT上,使用-n选项可以指定不输出处理后的数据;可以使用-n选项和增加p标记一起使用,只输出需要处理的数据。
(3)处理字符
在处理的字符中带有/时,替换会出现异常,需要使用来转义,例如:
    $ sed 's//bin/bash//bin/csh/' /etc/passwd
sed命令也允许使用其它分隔符,例如叹号  
    $ sed 's!/bin/bash!/bin/csh!' /etc/passwd
在这个例子中,路径名更容易读取和理解。
在shell脚本中,通常会用到需要替换的字符保存在一个变量中,直接替换的话会出现错误,需要使用单引号包含双引号的方式指定需要替换的变量;
    $ cat test.sh
    var=cats
    sed 's/dogs/'"$var"'/' test
(4)指定过滤选项
默认情况下,sed命令作用于文本数据的所有行,可以通过指定过滤选项进行行寻址。
有两种方式的行寻址:行的数字范围和文本模式过滤
数字的寻址方式,可以使用单个数字指定要处理的行号;可以使用起始行号、逗号、结束行号指定要处理的行范围;可以使用起始行号、逗号、$指定从某行开始到最后一行。
    $ cat test
    There are two dogs
    There are two dogs
    There are two dogs
    There are two dogs
    $ sed '2s/dogs/cats/' test
    There are two dogs
    There are two cats
    There are two dogs
    There are two dogs
    $ sed '2,3s/dogs/cats/' test
    There are two dogs
    There are two cats
    There are two cats
    There are two dogs
    $ sed '2,$s/dogs/cats/' test
    There are two dogs
    There are two cats
    There are two cats
    There are two cats
文本过滤的寻址方式,使用/string/指定匹配字符的行进行处理。
$ cat test
There are two dogs
There are two cats
There are two birds
$ sed -n '/cats/s/two/three/p' test
There are three cats
可以使用正则表达式创建高级文本模式进行过滤。
使用组合命令,可以将寻址应用于所有的命令,例如:
$ sed -e '2,${s/dogs/cats/; s/two/three/}' test
只需将要处理的所有命令使用{}来组合到一起即可。

3、删除、插入、修改、转换命令

(1)删除命令
格式:sed 'd' input_file
当不指定匹配选项时,d命令会将所有行都删除;
通常在使用删除命令时,需要指定行寻址,指定行号或字符匹配两种方式都适用;
可以指定行号,例如:sed '3d' input_file;
或者指定行范围,例如:sed '2,3d' input_file 或 sed '3,$d' input_file;
文本匹配模式与替换命令用法一致,例如:sed '/keyword/d' input_file;
$ cat test
the line num is 1
the line num is 2
the line num is 3
$ sed '2,$d' test
the line num is 1
$ sed '/2/$d' test
the line num is 1
the line num is 3
同样需要说明的是,d命令不会修改原文件内容,只是修改了输出。
(2)插入和附加文本
格式:sed 'istring line' test —— 向指定行前一行插入文本
           sed 'astring line' test —— 向指定行后一行附加文本
$ cat test 
the line num is 1
the line num is 2
the line num is 3
$ sed '2i	his is a test line' test
the line num is 1
this is a test line
the line num is 2
the line num is 3
$ sed '2a	his is a test line' test
the line num is 1
the line num is 2
this is a test line
the line num is 3
(3)修改行
格式:sed 'cstring text' input_file
修改命令和插入、附加格式基本一致,执行后会替换指定行内容
修改命令可以使用匹配模式。
(4)转换命令
格式:sed '[address]y/inchars/outchars/' input_file
转换命令y是唯一可以处理单个字符的sed编辑命令
$ cat test 
the line num is 1
the line num is 2
the line num is 3
$ sed 'y/12/45' test
the line num is 4
the line num is 5
the line num is 3
要求inchars和outchars长度要相同,否则会报错。
转换命令会替换文本中所有出现的字符。
 
 
 
原文地址:https://www.cnblogs.com/hancq/p/5278652.html