看完 gawk ,再来学习 sed
   工具 sed stream editor, 流编辑器)是一个批处理(非交互式)编辑器。它常被用做管道中的过滤器。
   sed 的命令行语法如下:
  a sed [-n] 程序    待处理文件
  b sed [-n] –f 程序文件    待处理文件
“程序”是一些指令,它的格式在下面以例子给出。也可以预先把指令写入“程序文件”用 -f 选项来调用。 sed 通过执行指定的一系列指令来处理“待处理文件”。以下的例子均以文件 new 作为“待处理文件”。
  $ cat new
   Line one.
  The second line.
  The third.
  This is line four.
  Five.
  This is the sixth sentence.
  This is line seven.
  Eighth and last.
.
  当使用 -n 选项时, sed 仅仅在标准输出上输出特定的行,例如被 p print )选定的行。不带 -n 选项 sed 将输出所有的行。另外, sed 是用逐行扫描的方式来对行进行处理的。
 
  $ sed ‘/line/  p’  new
  Line one.
  The second line.
The second line.
  The third.
  This is line four.
This is line four.
  Five.
  This is the sixth sentence.
  This is line seven.
  This is line seven.
  Eighth and last.
上面的命令除了显示了一遍所有行,还额外显示了包含字符串 ’line’ 的行(第 2 4 7 行)。说明一下: ’/line’ 用的是正则表达式匹配,除此之外还可以根据行号来匹配,例如:
  $ sed  -n  ‘3,5  p’  new
The third.
  This is line four.
  Five.
上面的命令显示了 3~5 行的内容,因为使用了 -n 选项,结果没有再显示一遍所有的行。
前两个例子都使用了 p 这个指令,它的作用是 print ,下面介绍一些常用的指令:
 
         指令                         意义
         p                           print ,打印选择的行到标准输出
         d                         delete ,删除选择的行
         s                          substitute ,替换,与 vi 中的替换类似
         w  file                     write, 另存为文件 file
         q                          quit ,退出命令,使得 sed 立即结束执行
         n                         next ,直接跳到下一行,本行不处理
         a                           append ,在当前选择的行之后插入一行或多行
         i                          insert ,在当前选择的行之前插入一行或多行
         c                         change ,把选定的行改为新的文本
 
下面通过例子把上面的指令都演示一遍。
 
$ sed  ‘5  q’  new
Line one.
  The second line.
  The third.
  This is line four.
  Five.
上面的命令匹配到第 5 行就退出,由于没有 -n 选项,它输出了前 5 行。
 
  $ cat append_demo
2 a \
AFTER.
$ sed  -f  append_demo  new
   Line one.
  The second line.
  AFTER.
  The third.
  This is line four.
  Five.
  This is the sixth sentence.
  This is line seven.
  Eighth and last.
上面的命令采用了 -f 程序文件的形式调用 sed 程序。程序 append_demo 匹配第 2 行后在第 2 行之后插入一个换行符( \ )和 AFTER. 由于没有 -n 选项,其他行照常显示。
 
$ sed  ‘s/line/LINE/’  new
   Line one.
  The second LINE.
  The third.
  This is LINE four.
  Five.
  This is the sixth sentence.
  This is LINE seven.
  Eighth and last.
上面的命令将所有的 ’line’ 换成了 ’LINE’ ,并且显示所有的行(因为不带 -n 选项)
 
$ sed  ‘2, 5  d’  new
   Line one.
  This is the sixth sentence.
  This is line seven.
  Eighth and last.
上面的命令将 2~5 行删除,然后显示所有行。
 
$ cat next_demo
/the/ n
p
$ sed  -n  -f  next_demo  new
   Line one.
  The second line.
  The third.
  This is line four.
  Five.
  This is line seven.
  Eighth and last.
上面的命令通过 ’/the’ 找到包含 ’the’ 的行,执行 n ,即跳过(或者说忽略)该行(第六行),再执行 p 因为有 p 且使用了 -n 选项,所以该仅显示我们指定的那些行(仅忽略第六行)
 
$ cat change_demo
2, 4   c \
SED WILL INSERT THESE\
THREE LINES IN PLACE\
OF THE SELECTED LINES.
$ sed  -f  change_demo  new
   Line one.
SED WILL INSERT THESE\
THREE LINES IN PLACE\
OF THE SELECTED LINES.
  Five.
  This is the sixth sentence.
  This is line seven.
  Eighth and last.
上面的命令将 2~4 行的内容整体换成 c 之后指定的内容(一个换行和之后大写的三行)
 
$ sed  -n  ‘2,4  w  temp’  new
该命令不会有任何输出显示,但是文件 new 中的 2~4 行被写入 temp
$ cat  temp
  The second line.
  The third.
  This is line four.
 
sed 基本的语法和规则就是这些了,更多的实际应用是作为 shell 脚本的一部分进行工作,有关 shell 及脚本的内容将在以后的博文中给出。
以上的所有原理及例子均出自 Mark G.Sobell 所著《 Linux 命令、编辑器与 Shell 编程》(杨明军、王凤芹译),由于本人水平有限,疏漏之处在所难免,望发现者即使指出,在此谢过了!