您的位置:首页 > 其它

Sed 的man手册参数详细解释(八)

2010-12-12 18:01 176 查看
g G Copy/append hold space to pattern space.
将保留空间(Hold space)的数据复制/追加到模式空间(Pattern space)中。
提示:原理同h/H,只是内容传输方向相反,不多说了。
/ +++++++++++++++++++++++++++++++++++++++例子23+++++++++++++++++++++++++++++++++++++
sed.txt的内容为:
This is a dog.
This is a cat.
This is a horse
sed.script脚本内容如下(每个指令一行,这个脚本有点难度的):
2,$G #如果当前行不为第一行,则将保留空间的内容附加到当前模式空间中,用换行符隔开
h #将当前行复制到保留空间,此时保留空间原有的内容被覆盖
$!d #如果不是最后一行删除掉当前模式空间的全部内容,继续读取下一行
p #打印当前模式空间的内容
执行sed命令:sed –n –f sed.script sed.txt 结果如下:
This is a dog.
This is a cat.
This is a horse
也就是将原有的行反向输出。
/+++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
x Exchange the contents of the hold and pattern spaces.
交换保留空间(Hold space)和模式空间(Pattern space)的内容。
/ +++++++++++++++++++++++++++++++++++++++例子24+++++++++++++++++++++++++++++++++++++
sed.txt的内容为:
This is a dog.
This is a cat.
sed.script脚本的内容为:
1{ #选择第一行
s/dog/DOG/g #替换dog为DOG
h #将当前模式空间的内容复制到保留空间
d #删除当前模式空间的内容,继续读取下一行
}
2{ #选择第一行
G #读取保留空间的内容追加到当前模式空间的后面
}
p #打印当前模式空间的内容
执行sed命令:sed –n –f sed.script sed.txt结果为:
This is a cat.
This is a DOG.
/+++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
l List out the current line in a ‘‘visually unambiguous’’ form.
将当前行以可见的严格的形式列出来。
补充:严格来说这里的l(L的小写)是指以可见的严格的形式列出当前模式空间(Pattern space)中的内容。这里的可见的严格的形式是指:用C形式的转义符显示不可打印(non-printed)字符,同时模式空间的内容的末尾用一个“$”来表示。默认每行最多70个字符,可以通过l指令后面加具体数字指定,也可以通过-l选项指定每行显示的字符数。
/ +++++++++++++++++++++++++++++++++++++++例子25+++++++++++++++++++++++++++++++++++++
sed.txt的内容为:
This is the 1st line.
This is the 2nd line.
sed命令:sed -ne 'l 10' sed.txt 或者 sed -l 10 -ne 'l' sed.txt 结果都是相同的:
This is t/
he 1st/t /
line.$
This is t/
he 2nd/t /
line.$
l指令将1st和2nd后的制表符用/t显示,每个模式空间中行再以每行10个字符显示,用反斜杠作为换行标志,同时每个模式空间内容的末尾用$表示。
/+++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
n N Read/append the next line of input into the pattern space.
将当前行的下一行数据覆盖/追加进当前模式空间(Pattern space)的内容。
提示:其中n表示将模式空间中内容的下一行数据读取进来,这个时候模式空间中原有的内容将会被覆盖;N表示将模式空间中内容的下一行数据读取,然后追加到模式空间中原有的内容后,并用反斜杠隔开。
/ +++++++++++++++++++++++++++++++++++++++例子26+++++++++++++++++++++++++++++++++++++
sed.txt的内容为:
This is a dog.
This is a cat.
sed命令:
sed -ne ' #按Enter键换行
>1{ #选择第一行
>s/dog/DOG/g #替换dog为DOG
>n #读取第二行,覆盖当前行
>s/cat/CATT/g #将cat替换为CAT
>p #打印当前模式空间的内容
>}
>' sed.txt
运行上面的命令,结果为:
This is a CATT.
如果将上面的n换成N,结果为:
This is a DOG.
This is a CATT.
/+++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: