sed中关于pattern space和hold space
来源:互联网 发布:linux系统各种下载 编辑:程序博客网 时间:2024/04/27 12:54
sed的用法是: sed OPTIONS... [SCRIPT] [FILE...]
简单说下sed的工作流程。pattern space和hold space默认都是空的。sed读入一行内容,删除尾部的换行符,存入pattern space, 然后执行SCRIPT,如果OPTIONS里没有 -n, pattern space里的内容会被输出到stdout(若读入时含有换行,这里会输出换行), 如果有-n, 则不输出,接下来就读取下一行数据,重复上述步骤。
再说一下一会要在SCRIPT中用到的几个命令。
d : 清空pattern space中的内容,立即开始下个循环(意思是跳过默认的输出pattern space内容的阶段???不知理解的对不对)
h : 用pattern space中的内容替换hold pattern中的内容
H : 在hold space中的内容后面追加一个换行,把pattern space中的内容追加到hold space中
g : 用hold space中的内容替换pattern space中的内容
G : 在pattern space中的内容后面追加一个换行,把hold space中的内容追加到pattern space中
h, g会替换(可以理解为先清空,再复制), H, G是追加。
这个例子是这样的,有一个文件myfile.txt, 内容如下:
This is the 1st line.2nd line is here.i'm the 3rd.why i am the last one?
期望逆序输出其内容,也就是如下效果
why i am the last one?i'm the 3rd.2nd line is here.This is the 1st line.
解法如下:
$ sed '1!G;h;$!d' myfile.txt
执行步骤:
首先读取myfile.txt的第一行到pattern space,如下所示:
pattern space hold space ------------ ------------ This is the 1st line. <null>
执行命令'1!G;h;$!d', 由于是第一行,所以'1!G'被跳过,执行'h', 用pattern space中的内容替换hold space中的内容,由于不是最后一行,执行'$!d'清空pattern space,此时状态是:
pattern space hold space ------------ ------------ <null> This is the 1st line.
读取第二行到pattern space
pattern space hold space ------------ ------------ 2nd line is here. This is the 1st line.
已经不是第一行了,所以执行'1!G', hold space中的内容追加都pattern space
pattern space hold space ------------ ------------ 2nd line is here. This is the 1st line. This is the 1st line.
执行'h', pattern space的内容替换hold space
pattern space hold space ------------ ------------ 2nd line is here. 2nd line is here. This is the 1st line. This is the 1st line.
执行'$!d', 清空pattern space, 读取第三行到pattern space
pattern space hold space ------------ ------------ i’m the 3rd. 2nd line is here. This is the 1st line.
执行'1!G', hold space中的内容追加都pattern space
pattern space hold space ------------ ------------ i’m the 3rd. 2nd line is here. 2nd line is here. This is the 1st line. This is the 1st line.
执行'h', pattern space的内容替换hold space
pattern space hold space ------------ ------------ i’m the 3rd. i’m the 3rd. 2nd line is here. 2nd line is here. This is the 1st line. This is the 1st line.
执行'$!d', 清空pattern space, 读取第四行到pattern space
pattern space hold space ------------ ------------ why i am the last one? i’m the 3rd. 2nd line is here. This is the 1st line.
执行'1!G', hold space中的内容追加都pattern space
pattern space hold space ------------ ------------ why i am the last one? i’m the 3rd. i’m the 3rd. 2nd line is here. 2nd line is here. This is the 1st line. This is the 1st line.
执行'h', pattern space的内容替换hold space
pattern space hold space ------------ ------------ why i am the last one? why i am the last one? i’m the 3rd. i’m the 3rd. 2nd line is here. 2nd line is here. This is the 1st line. This is the 1st line.
现在已经是最后一行,'$!d'将不会被执行,所有SCRIPT执行完之后,输出pattern space中的内容
why i am the last one?i'm the 3rd.2nd line is here.This is the 1st line.
得到最终结果。
根据info sed,默认情况下,pattern space中的内容会在两次循环之间(输出之后,读取之前)被清空; 而hold space则不会自动清空。
----------------------------------
这个命令还有一种写法
$ sed -n '1!G;h;$p' myfile.txt
这里用了-n选项,如前面所说,它会跳过SCRIPT执行完后,pattern space的输出动作,而'$p'则是的执行到最后一行的时候,执行'p'来强制输出最终结果。
- sed中关于pattern space和hold space
- 玩玩sed -- pattern space和hold space
- sed中关于pattern space和hold space的小实例一则(tac)
- sed模式空间(pattern space)和保持空间(hold space)
- sed命令简单使用-pattern space与hold space
- sed高级用法:模式空间(pattern space)和保持空间(hold space)
- sed高级用法:模式空间(pattern space)和保持空间(hold space)
- sed高级用法:模式空间(pattern space)和保持空间(hold space)
- android中关于网页view space和content space的含义
- space
- space
- Permanent Space 和 Heap Space
- User space和Kernel space
- sed之:a;N;$!ba预读入全部内容至pattern space
- 关于VIM中TAB和SPACE的转换以及相关研究
- java heap space 和PermGen space
- java.lang.OutOfMemoryError: PermGen space 和 Java heap space 关于MyEclipse eclipse tomcat内存溢解决方法
- eclipse 中PermGen space
- 安装使用template toolkit
- Matlab基本函数-length函数
- Android 获得当前应用的版本号(转)
- 压力测试-WebApplicationStress1.1压力测试工具
- Ubuntu 12.04安装OpenCV2.4.6.1
- sed中关于pattern space和hold space
- STL库中vector的模拟
- 快到极致的Android模拟器——Genymotion
- java中serialversionuid的用途
- 银行业务中的常规概念理解
- centos 开机自动启动网络
- 软件工程之系统建模篇【设计接口类模型】
- 软工实验3
- 在二元树中找出和为某一值的所有路径