不合格的程序猿
分类: LINUX
2016-01-27 15:24:06
sed [OPTIONS] -e 'scripts' | -f script-file [input-files]如:
可以参考man手册.比较常用的有:
-n --quiet --silent
不自动打印模式空间.简单来讲就是不自动打印当前要处理的行.sed会读入一行,放入到一个叫Pattern space(模式空间)里,以便于执行编辑命令来处理它.默认情况下,会自动把这一行(Pattern space里的内容)打印出来.对比下不指定这个参数时的情况就明白了:
你会看到重复,原因就是第一行是默认打印的Pattern space的内容(也就是要处理的行的内容). 然后执行编辑命令,因为编辑命令是简单的p(打印内容),所以你就看到重复的输出.
- [alex@alexon:~]$sed -e 'p' speech.txt
- With great power comes great responsibility.
- With great power comes great responsibility.
- The freedom is nothing but a chance to be better.
- The freedom is nothing but a chance to be better.
但如果加了-n(或者--quiet --silent)会变成这样:
-e 'scripts'
- [alex@alexon:~]$sed -n -e 'p' speech.txt
- With great power comes great responsibility.
- The freedom is nothing but a chance to be better.
指定编辑命令,或者叫做脚本.就是一要执行的sed所支持的编辑命令.主要是模式匹配和文本替换,插入,删除之类的编辑操作.
这个选项可以指定多个,sed会按从左到右的顺序,一个一个的执行.
解析:第一个命令'='是打印行号;第二个是打印这一行;第三个是做替换.
- [alex@alexon:~]$sed -e '=' -e 'p' -e 's/great/poor/' speech.txt
- 1
- With great power comes great responsibility.
- With poor power comes great responsibility.
- 2
- The freedom is nothing but a chance to be better.
- The freedom is nothing but a chance to be better.
-f script-file
执行指定的文件中的脚本.也就是不把编辑命令放在命令行中,而是放在一个文件里面.让sed去执行文件里面的命令.
-i[Suffix] --in-place[=Suffix]
即时的编辑输入的文件.如果指定Suffix,则会用其作后缀来备份输入文 件.默认的行为是从输入文件中一行一行的读入文本,然后执行命令,然后输出结果到标准输出,也就是说对原文本没有影响,并不会改动原文件.但有些时候我们 想改变原文件,也就是说要对原文件进行编辑.这时就需要用到此选项.为了不丢失数据,可以指定后缀来备份原文件.
例如:
命令是把文件中的great替换成poor,并把原文件备份为.bak.
- [alex@alexon:~]$cat speech.txt
- With great power comes great responsibility.
- The freedom is nothing but a chance to be better.
- [alex@alexon:~]$sed -i.bak -e 's/great/poor/g' speech.txt
- [alex@alexon:~]$cat speech.txt
- With poor power comes poor responsibility.
- The freedom is nothing but a chance to be better.
- [alex@alexon:~]$cat speech.txt.bak
- With great power comes great responsibility.
- The freedom is nothing but a chance to be better.
到这里,是不是让你想起了强大的perl命令,也有类似的功能:
- [alex@alexon:~]$perl -p -i.bak -e 's/poor/great/g' speech.txt
- [alex@alexon:~]$cat speech.txt
- With great power comes great responsibility.
- The freedom is nothing but a chance to be better.
- [alex@alexon:~]$cat speech.txt.bak
- With poor power comes poor responsibility.
- The freedom is nothing but a chance to be better.
命令行参数仅是sed的一部分,它的主要核心部分是它的编辑命令,或者称作它的脚本,也就是通过-e选项指定的,或者通过-f指定的脚本文件.
[命令作用范围][!] cmd [cmd-args]如,
也可以称作是寻址.通俗的讲就是指定后面的编辑命令的作用范围,通常有几种方式来指定范围:
不指定 --- 如果不指定具体的范围,那么将作用到所有的行.
- [alex@alexon:~]$sed -n -e 'p' speech.txt
- With great power comes great responsibility.
- The freedom is nothing but a chance to be better.
用行号来指定 --- n, m第n行到第m行,特别的$代表最后一行
1, 3 ---- 第1行到第3行
1,$ ---- 第1 行到最后一行,也就是所有的行
相对的行数,可以在逗号后面用+m,如n,+m来表示n行以后到n+m行为止,的一个相对量,如:
- [alex@alexon:~]$sed -n -e '2,+3 p' speech.txt
- 2. The freedom is nothing but a chance to be better.
- 3. The tree of liberty must be refreshed from time to time with blood of patroits
- 4. and tyrants.
- 5. Life is like a box of chocolates, you never know what you gonna get.
跳跃性选择行.-------可以用波浪~来跳跃性选择, n~m,是指从第n行开始,后面每隔m行,执行一次,如:1~2,从第1行开始,每隔2行执行一次,也就是是执行1,3,5,7.....:
- [alex@alexon:~]$sed -n -e '1~2 p' speech.txt
- 1. With great power comes great responsibility.
- 3. The tree of liberty must be refreshed from time to time with blood of patroits
- 5. Life is like a box of chocolates, you never know what you gonna get.
模式匹配
指定作用范围的最强大的地方就是在于可以使用模式匹配来指定.模式匹配的格式是:
[/pattern1/], [/pattern2/]
如果仅指定一个匹配,则会在所有匹配的行为执行编辑命令,如果指定二个,则是第一个匹配pattern1的行到,第一次匹配pattern2的行.
- [alex@alexon:~]$sed -n -e '/great/ p' speech.txt
- 1. With great power comes great responsibility.
- [alex@alexon:~]$sed -n -e '/great/, /chocolates/ p' speech.txt
- 1. With great power comes great responsibility.
- 2. The freedom is nothing but a chance to be better.
- 3. The tree of liberty must be refreshed from time to time with blood of patroits
- 4. and tyrants.
- 5. Life is like a box of chocolates, you never know what you gonna get.
^ --- 行首
$ ----行尾
. ----任意非换行符'\n'符
\b ---- 一个单词结尾,单词定义为一连串的字母或数字,可以单独放在一端,也可放二端.
* --- 0或1个或多个
\+ --- 1个或多个
\? --- 0或1
{m} --- 出现m次
{m,n} --- 出现m次到n次,如{1,5}表示出现1次到5次不等(1,2,3,4,5次)
\ --- 可以转义特殊字符
[] ---其内的任意字符
\| ---- 或操作,abc\|123匹配123或者abc
... ----组合,形成一个组,主要用于索引
\n ---- 前面第n个组合, /123\1/ 则匹配123123
add1[,add2] i text 插入 --- 在指定的行的前面插入文字
- [alex@alexon:~]$sed -e '3 i abcd' speech.txt
- 1. With great power comes great responsibility.
- 2. The freedom is nothing but a chance to be better.
- abcd
- 3. The tree of liberty must be refreshed from time to time with blood of patroits
- 4. and tyrants.
- 5. Life is like a box of chocolates, you never know what you gonna get.
add1[,add2] a text 添加 --- 在指定的行的后面添加文字
add1[,add2] d删除 --- 删除指定的行
- [alex@alexon:~]$sed -e '3 a abcd' speech.txt
- 1. With great power comes great responsibility.
- 2. The freedom is nothing but a chance to be better.
- 3. The tree of liberty must be refreshed from time to time with blood of patroits
- abcd
- 4. and tyrants.
- 5. Life is like a box of chocolates, you never know what you gonna get.
- [alex@alexon:~]$sed -e '3 d' speech.txt
- 1. With great power comes great responsibility.
- 2. The freedom is nothing but a chance to be better.
- 4. and tyrants.
- 5. Life is like a box of chocolates, you never know what you gonna get.
add1[,add2] s/pattern/replace/[opts] 替换把指定的行内的pattern替换为replace
默认情况,只会替换行内第1个pattern.
- [alex@alexon:~]$sed -e '1, 3 s/great/poor/' speech.txt
- 1. With poor power comes great responsibility.
- 2. The freedom is nothing but a chance to be better.
- 3. The tree of liberty must be refreshed from time to time with blood of patroits
- 4. and tyrants.
- 5. Life is like a box of chocolates, you never know what you gonna get.
opts,可以指定选项以控制替换的行为
n --- 替换行内第n个pattern为replace
g --- 替换行内所有的pattern为replace
p --- 打印这一行,如果替换成功的话.
add1[,add2] c text 把指定的行完整的替换为text
p 打印
- [alex@alexon:~]$sed -e '1, 3 c abcd' speech.txt
- abcd
- 4. and tyrants.
- 5. Life is like a box of chocolates, you never know what you gonna get.
= 打印行号