网站建设中 html,石家庄平台公司,合肥网站建设培训中心,商城网站如何建设前言 作者不善言谈#xff0c;如有错误请指正#xff01;#xff01;#xff01; 转载请注明出处#xff01;#xff01;#xff01; sed之G、H、g、h使用 什么是sed#xff1f; sed是面向流的行编辑器#xff0c;所谓面向流#xff0c;是指接受标准输入的输入#…前言 作者不善言谈如有错误请指正 转载请注明出处 sed之G、H、g、h使用 什么是sed sed是面向流的行编辑器所谓面向流是指接受标准输入的输入输出内容到标准输出上。 sed如何处理数据 sed在正常情况下将处理的行读入模式空间pattern space脚本中的“sed-commandsed命令”就一条接着一条进行处理知道脚本执行完毕。然后该行呗输出模式pattern space被清空接着在重复执行刚才的动作文件中的新的一行被读入直到文件处理完毕。 什么是Pattern Space什么是Hold Space pattern space相当于车间sed把流内容在这里处理。 hold space相当于仓库加工的半成品在这里临时储存。 PS你可以将pattern space看成是一个流水线所有的动作都是在“流水线”上执行的而hold space是一个“仓库”“流水线”上的东东都可以放到这里。 为什么要使用sed高级命令G、H、g、h、n、N、x 由于各种各样的原因比如用户希望在某个条件下脚本中的某个命令被执行或者希望模式空间得到保留以便下一次的处理都有可能使得sed在处理文件的时候不按照正常的流程来进行。这个时候sed设置了一些高级命令来满足用户的要求。 sed命令 g[address[,address]]g 将hold space中的内容拷贝到pattern space中原来pattern space里的内容清除 G[address[,address]]G 将hold space中的内容append到pattern space\n后 h[address[,address]]h 将pattern space中的内容拷贝到hold space中原来的hold space里的内容被清除 H[address[,address]]H 将pattern space中的内容append到hold space\n后 d[address[,address]]d 删除pattern中的所有行并读入下一新行到pattern中 D[address[,address]]D 删除multiline pattern中的第一行不读入下一行 PS不论是使用G、g还是H、h它们都是将hold space里面的内容“copy”到pattern space中或者将pattern space中的内容“copy”到hold space中。 附上英文的解释注意其中的高亮单词 The h command copies the pattern buffer into the hold buffer. The pattern buffer is unchanged. Instead of exchanging the hold space with the pattern space, you can copy the hold space to the pattern space with the g command. This deletes the pattern space. If you want to append to the pattern space, use the G command. This adds a new line to the pattern space, and copies the hold space after the new line. 示例用sed模拟出tac的功能倒序输出。 文件内容 cat mm 1 2 3 解决方法 sed ‘1!G;h;$!d’mm ps1!G第1行不 执行“G”命令从第2行开始执行。 $!d最后一行不删除保留最后1行 图解分析过程 PPattern Space HHold Space 蓝色Hold Space中的数据 绿色Pattern Space中的数据 参考 《sed and awk 第二版》 Sed - An Introduction and Tutorial by Bruce Barnett sed的高级应用 - haijd Article转载于:https://www.cnblogs.com/fhefh/archive/2011/11/22/2259097.html