Chinaunix首页 | 论坛 | 博客
  • 博客访问: 176155
  • 博文数量: 57
  • 博客积分: 719
  • 博客等级: 上士
  • 技术积分: 320
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-25 17:15
文章分类

全部博文(57)

文章存档

2014年(1)

2013年(18)

2012年(9)

2011年(22)

2010年(7)

分类:

2012-02-03 15:32:49

原文地址:sed 学习笔记 作者:sjhf

   看完gawk,再来学习sed
   工具sedstream editor,流编辑器)是一个批处理(非交互式)编辑器。它常被用做管道中的过滤器。
  sed的命令行语法如下:
  ased [-n] 程序  待处理文件
  bsed [-n] –f 程序文件   待处理文件
“程序”是一些指令,它的格式在下面以例子给出。也可以预先把指令写入“程序文件”用-f选项来调用。sed通过执行指定的一系列指令来处理“待处理文件”。以下的例子均以文件new作为“待处理文件”。
  $ cat new
  Line one.
  Thesecond line.
  Thethird.
 This is line four.
 Five.
 This is the sixth sentence.
 This is line seven.
 Eighth and last.
.
 当使用-n选项时,sed仅仅在标准输出上输出特定的行,例如被pprint)选定的行。不带-n选项sed将输出所有的行。另外,sed是用逐行扫描的方式来对行进行处理的。
 
  $ sed ‘/line/  p’  new
  Lineone.
  Thesecond line.
The second line.
  Thethird.
 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’的行(第247行)。说明一下:’/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.
  Thesecond line.
  Thethird.
 This is line four.
 Five.
上面的命令匹配到第5行就退出,由于没有-n选项,它输出了前5行。
 
  $ cat append_demo
2 a\
AFTER.
$ sed  -f  append_demo  new
  Line one.
  Thesecond line.
 AFTER.
  Thethird.
 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.
  Thesecond LINE.
  Thethird.
 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.
  Thisis 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.
  Thesecond line.
  Thethird.
 This is line four.
 Five.
  Thisis 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
  Thesecond line.
  Thethird.
 This is line four.
 
sed基本的语法和规则就是这些了,更多的实际应用是作为shell脚本的一部分进行工作,有关shell及脚本的内容将在以后的博文中给出。
以上的所有原理及例子均出自MarkG.Sobell所著《Linux命令、编辑器与Shell编程》(杨明军、王凤芹译),由于本人水平有限,疏漏之处在所难免,望发现者即使指出,在此谢过了!

http://licong.blog.51cto.com/542131/152541

阅读(576) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~