日期:2014-05-16  浏览次数:20557 次

Linux Shell常用技巧(三)

流编辑器sed:

?? ?sed一次处理一行文件并把输出送往屏幕。sed把当前处理的行存储在临时缓冲区中,称为模式空间(pattern space)。一旦sed完成对模式空间中的行的处理,模式空间中的行就被送往屏幕。行被处理完成之后,就被移出模式空间,程序接着读入下一行,处理,显示,移出......文件输入的最后一行被处理完以后sed结束。通过存储每一行在临时缓冲区,然后在缓冲区中操作该行,保证了原始文件不会被破坏。
?? ?
?? ?1.?? ?sed的命令和选项:
?? ?命令?? ??? ?????? 功能描述
?? ?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实例:
?? ?/> cat testfile
?? ?northwest?????? NW???? Charles Main?????????? 3.0????? .98????? 3?????? 34
?? ?western????????? WE????? Sharon Gray?????????? 5.3????? .97???? 5?????? 23
?? ?southwest?????? SW???? Lewis Dalsass????????? 2.7????? .8????? 2?????? 18
?? ?southern???????? SO????? Suan Chin?????????????? 5.1???? .95???? 4?????? 15
?? ?southeast?????? SE?????? Patricia Hemenway?? 4.0????? .7????? 4?????? 17
?? ?eastern?????????? EA????? TB Savage?????????????? 4.4???? .84???? 5?????? 20
?? ?northeast??????? NE????? AM Main Jr.????????????? 5.1???? .94???? 3?????? 13
?? ?north????????????? NO????? Margot Weber???????? 4.5???? .89???? 5?????? 9
?? ?central??????????? CT????? Ann Stephens?????? ?? 5.7???? .94???? 5?????? 13
?? ?
?? ?/> sed '/north/p' testfile #如果模板north被找到,sed除了打印所有行之外,还有打印匹配行。
?? ?northwest?????? NW????? Charles Main?? ??? ??? 3.0???? .98???? 3?????? 34
?? ?northwest?????? NW????? Charles Main?? ??? ??? 3.0???? .98???? 3?????? 34
?? ?western????????? WE????? Sharon Gray??? ??? ?? 5.3???? .97???? 5?????? 23
?? ?southwest????? SW????? Lewis Dalsass? ??? ??? 2.7???? .8?????? 2?????? 18
?? ?southern??????? SO?????? Suan Chin????? ??? ???? 5.1???? .95???? 4?????? 15
?? ?southeast?????? SE?????? Patricia Hemenway?? 4.0???? .7?????? 4?????? 17
?? ?eastern?????????? EA????? TB Savage????? ??? ???? 4.4???? .84???? 5?????? 20
?? ?northeast??????? NE????? AM Main Jr.??? ??? ????? 5.1???? .94???? 3?????? 13
?? ?northeast??????? NE????? AM Main Jr.??? ??? ????? 5.1???? .94???? 3?????? 13
?? ?north????????????? NO????? Margot Weber?? ??? ? 4.5???? .89???? 5?????? 9
?? ?north????????????? NO????? Margot Weber?? ??? ? 4.5???? .89???? 5?????? 9
?? ?central??????????? CT????? Ann Stephens?? ??? ?? 5.7???? .94???? 5?????? 13
?? ?
?? ?#-n选项取消了sed的默认行为。在没有-n的时候,包含模板的行被打印两次,但是在使用-n的时候将只打印包含模板的行。
?? ?/> sed -n '/north/p' testfile
?? ?northwest??????