Sed与AWK入门教程之Sed篇

来源:互联网 发布:node v6.2.0 x64.msi 编辑:程序博客网 时间:2024/04/29 12:17

http://blog.csdn.net/hitlion2008/article/details/9247173


Sed和AWK是*nix命令行里面文本处理的神器,相当的强大.它们都是面向行的,或者说它们处理文本的方式都是一行接着一行的处理,从标准输入或者文件中读取内容,一行一行的执行脚本命令,然后打印输出到标准输出,直到文件结尾(EOF).


Sed

Sed是一个流编辑器(Stream editor),它的功能在于对于一个输入流进行编辑和处理.相当于是对一个输入流进行脚本性的编辑.其实它就是对一个输入流进行ed(一个面向行的编辑器)的脚本编辑.
Sed命令包括二部分,一部分是命令行参数或者说命令的执行方式,另一部分就是它的编辑命令,也常称作脚本.

命令执行方式:

sed [OPTIONS] -e 'scripts' | -f script-file [input-files]
如:
[plain] view plaincopyprint?
  1. [alex@alexon:~]$sed -n -e 'p' speech.txt  
  2. With great power comes great responsibility.  
  3. The freedom is nothing but a chance to be better.  
看出来了吧,这相当于cat 命令.
[html] view plaincopyprint?
  1. [alex@alexon:~]$cat speech.txt   
  2. With great power comes great responsibility.  
  3. The freedom is nothing but a chance to be better.  

命令行参数

可以参考man手册.比较常用的有:

-n --quiet --silent
      不自动打印模式空间.简单来讲就是不自动打印当前要处理的行.sed会读入一行,放入到一个叫Pattern space(模式空间)里,以便于执行编辑命令来处理它.默认情况下,会自动把这一行(Pattern space里的内容)打印出来.对比下不指定这个参数时的情况就明白了:
[html] view plaincopyprint?
  1. [alex@alexon:~]$sed -e 'p' speech.txt  
  2. With great power comes great responsibility.  
  3. With great power comes great responsibility.  
  4. The freedom is nothing but a chance to be better.  
  5. The freedom is nothing but a chance to be better.  
你会看到重复,原因就是第一行是默认打印的Pattern space的内容(也就是要处理的行的内容). 然后执行编辑命令,因为编辑命令是简单的p(打印内容),所以你就看到重复的输出.
但如果加了-n(或者--quiet --silent)会变成这样:
[html] view plaincopyprint?
  1. [alex@alexon:~]$sed -n -e 'p' speech.txt  
  2. With great power comes great responsibility.  
  3. The freedom is nothing but a chance to be better.  
-e 'scripts'
    指定编辑命令,或者叫做脚本.就是一要执行的sed所支持的编辑命令.主要是模式匹配和文本替换,插入,删除之类的编辑操作.
    这个选项可以指定多个,sed会按从左到右的顺序,一个一个的执行.
[html] view plaincopyprint?
  1. [alex@alexon:~]$sed -e '=' -e 'p' -e 's/great/poor/' speech.txt  
  2. 1  
  3. With great power comes great responsibility.  
  4. With poor power comes great responsibility.  
  5. 2  
  6. The freedom is nothing but a chance to be better.  
  7. The freedom is nothing but a chance to be better.  
解析:第一个命令'='是打印行号;第二个是打印这一行;第三个是做替换.

-f script-file
   执行指定的文件中的脚本.也就是不把编辑命令放在命令行中,而是放在一个文件里面.让sed去执行文件里面的命令.
-i[Suffix] --in-place[=Suffix]
即时的编辑输入的文件.如果指定Suffix,则会用其作后缀来备份输入文件.默认的行为是从输入文件中一行一行的读入文本,然后执行命令,然后输出结果到标准输出,也就是说对原文本没有影响,并不会改动原文件.但有些时候我们想改变原文件,也就是说要对原文件进行编辑.这时就需要用到此选项.为了不丢失数据,可以指定后缀来备份原文件.
例如:
[plain] view plaincopyprint?
  1. [alex@alexon:~]$cat speech.txt   
  2. With great power comes great responsibility.  
  3. The freedom is nothing but a chance to be better.  
  4. [alex@alexon:~]$sed -i.bak -e 's/great/poor/g' speech.txt   
  5. [alex@alexon:~]$cat speech.txt  
  6. With poor power comes poor responsibility.  
  7. The freedom is nothing but a chance to be better.  
  8. [alex@alexon:~]$cat speech.txt.bak   
  9. With great power comes great responsibility.  
  10. The freedom is nothing but a chance to be better.  
命令是把文件中的great替换成poor,并把原文件备份为.bak.
到这里,是不是让你想起了强大的perl命令,也有类似的功能:
[plain] view plaincopyprint?
  1. [alex@alexon:~]$perl -p -i.bak -e 's/poor/great/g' speech.txt  
  2. [alex@alexon:~]$cat speech.txt  
  3. With great power comes great responsibility.  
  4. The freedom is nothing but a chance to be better.  
  5. [alex@alexon:~]$cat speech.txt.bak   
  6. With poor power comes poor responsibility.  
  7. The freedom is nothing but a chance to be better.  


命令行参数仅是sed的一部分,它的主要核心部分是它的编辑命令,或者称作它的脚本,也就是通过-e选项指定的,或者通过-f指定的脚本文件.

编辑命令的格式:

[命令作用范围][!] cmd [cmd-args]
如,
[plain] view plaincopyprint?
  1. [alex@alexon:~]$sed -n -e '1 p' speech.txt  
  2. With great power comes great responsibility.  

命令的作用范围

也可以称作是寻址.通俗的讲就是指定后面的编辑命令的作用范围,通常有几种方式来指定范围:

不指定 --- 如果不指定具体的范围,那么将作用到所有的行.
[plain] view plaincopyprint?
  1. [alex@alexon:~]$sed -n -e 'p' speech.txt  
  2. With great power comes great responsibility.  
  3. 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行为止,的一个相对量,如:
[plain] view plaincopyprint?
  1. [alex@alexon:~]$sed -n -e '2,+3 p' speech.txt  
  2. 2. The freedom is nothing but a chance to be better.  
  3. 3. The tree of liberty must be refreshed from time to time with blood of patroits  
  4. 4. and tyrants.  
  5. 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.....:
[plain] view plaincopyprint?
  1. [alex@alexon:~]$sed -n -e '1~2 p' speech.txt  
  2. 1. With great power comes great responsibility.  
  3. 3. The tree of liberty must be refreshed from time to time with blood of patroits  
  4. 5. Life is like a box of chocolates, you never know what you gonna get.  

模式匹配
指定作用范围的最强大的地方就是在于可以使用模式匹配来指定.模式匹配的格式是:
[/pattern1/], [/pattern2/]
如果仅指定一个匹配,则会在所有匹配的行为执行编辑命令,如果指定二个,则是第一个匹配pattern1的行到,第一次匹配pattern2的行.
[plain] view plaincopyprint?
  1. [alex@alexon:~]$sed -n -e '/great/ p' speech.txt  
  2. 1. With great power comes great responsibility.  
  3. [alex@alexon:~]$sed -n -e '/great/, /chocolates/ p' speech.txt  
  4. 1. With great power comes great responsibility.  
  5. 2. The freedom is nothing but a chance to be better.  
  6. 3. The tree of liberty must be refreshed from time to time with blood of patroits  
  7. 4. and tyrants.  
  8. 5. Life is like a box of chocolates, you never know what you gonna get.  

正则表达式

涉及到模式匹配就会涉及到正则表达式,sed的正则表达式与标准的略有不同.在命令行还可以指定-r --regexp-extended来使用扩展正则表达式.

位置符:

^ --- 行首
$ ----行尾
. ----任意非换行符'\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 插入 --- 在指定的行的前面插入文字
[plain] view plaincopyprint?
  1. [alex@alexon:~]$sed -e '3 i abcd' speech.txt  
  2. 1. With great power comes great responsibility.  
  3. 2. The freedom is nothing but a chance to be better.  
  4. abcd  
  5. 3. The tree of liberty must be refreshed from time to time with blood of patroits  
  6. 4. and tyrants.  
  7. 5. Life is like a box of chocolates, you never know what you gonna get.  

add1[,add2] a text 添加 --- 在指定的行的后面添加文字
[plain] view plaincopyprint?
  1. [alex@alexon:~]$sed -e '3 a abcd' speech.txt  
  2. 1. With great power comes great responsibility.  
  3. 2. The freedom is nothing but a chance to be better.  
  4. 3. The tree of liberty must be refreshed from time to time with blood of patroits  
  5. abcd  
  6. 4. and tyrants.  
  7. 5. Life is like a box of chocolates, you never know what you gonna get.  
add1[,add2] d删除 --- 删除指定的行
[plain] view plaincopyprint?
  1. [alex@alexon:~]$sed -e '3 d' speech.txt  
  2. 1. With great power comes great responsibility.  
  3. 2. The freedom is nothing but a chance to be better.  
  4. 4. and tyrants.  
  5. 5. Life is like a box of chocolates, you never know what you gonna get.  

add1[,add2] s/pattern/replace/[opts] 替换把指定的行内的pattern替换为replace
[plain] view plaincopyprint?
  1. [alex@alexon:~]$sed -e '1, 3 s/great/poor/' speech.txt  
  2. 1. With poor power comes great responsibility.  
  3. 2. The freedom is nothing but a chance to be better.  
  4. 3. The tree of liberty must be refreshed from time to time with blood of patroits  
  5. 4. and tyrants.  
  6. 5. Life is like a box of chocolates, you never know what you gonna get.  
默认情况,只会替换行内第1个pattern.
            opts,可以指定选项以控制替换的行为
                n --- 替换行内第n个pattern为replace
                g --- 替换行内所有的pattern为replace
                p --- 打印这一行,如果替换成功的话.
add1[,add2] c text  把指定的行完整的替换为text
[plain] view plaincopyprint?
  1. [alex@alexon:~]$sed -e '1, 3 c abcd' speech.txt  
  2. abcd  
  3. 4. and tyrants.  
  4. 5. Life is like a box of chocolates, you never know what you gonna get.  
p   打印

=  打印行号

知道了这些就可以应付大部分的文本处理.sed也有一些高级编辑命令如操作Pattern Space,或者分支等,但比较复杂,通常也用不到.

可以看出sed是一个流编辑器,它的强大之处在于可以以行的方式来脚本化处理文本.它的主要功能就是删,查,换和添加.但它毕竟不是编程语言,所以它不能有变量,和循环,分支等逻辑.所以,sed通常与AWK一起使用.AWK更具有编程语言的特性,,它们刚好互补,一起使用构成了文本处理的二个利器.