Linux Shell常用技巧(三)

    Read

八. 流编辑器sed:
sed一次处理一行文件并把输出送往屏幕。sed把当前处理的行存储在临时缓冲区中,称为模式空间(pattern space)。一旦sed完成对模式空间中的行的处理,模式空间中的行就被送往屏幕。

行被处理完成之后,就被移出模式空间,程序接着读入下一行,处理,显示,移出......文件输入的最后一行被处理完以后sed结束。通过存储每一行在临时缓冲区,然后在缓冲区中操作该行,保证了原始文件不会被破坏。
 1.  sed的命令和选项:SourceByrd's Weblog-https://note.t4x.org/system/linux-shell-techniques-third/

命令 功能描述
a\  在当前行的后面加入一行或者文本。
c\  用新的文本改变或者替代本行的文本。
d  从pattern space位置删除行。
i\  在当前行的上面插入文本。
h  拷贝pattern space的内容到holding buffer(特殊缓冲区)。
H  追加pattern space的内容到holding buffer。
g  获得holding buffer中的内容,并替代当前pattern space中的文本。
G  获得holding buffer中的内容,并追加到当前pattern space的后面。
n  读取下一个输入行,用下一个命令处理新的行而不是用第一个命令。
p  打印pattern space中的行。
P  打印pattern space中的第一行。
q  退出sed。
w file  写并追加pattern space到file的末尾。
!  表示后面的命令对所有没有被选定的行发生作用。
s/re/string  用string替换正则表达式re。
=  打印当前行号码。
替换标记
g  行内全面替换,如果没有g,只替换第一个匹配。
p  打印行。
x  互换pattern space和holding buffer中的文本。
y  把一个字符翻译为另一个字符(但是不能用于正则表达式)。
选项
-e  允许多点编辑。
-n  取消默认输出。

     需要说明的是,sed中的正则和grep的基本相同,完全可以参照本系列的第一篇中的详细说明。
2. sed实例:


转载于:http://www.cnblogs.com/stephen-liu74/archive/2011/11/17/2245130.htmlSourceByrd's Weblog-https://note.t4x.org/system/linux-shell-techniques-third/ SourceByrd's Weblog-https://note.t4x.org/system/linux-shell-techniques-third/
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!
Byrd
  • by Published on August 25, 2014
  • 原文链接:https://note.t4x.org/system/linux-shell-techniques-third/
匿名

Comment

Anonymous

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: