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

P(小写) Print the current pattern space.

打印当前模式空间(Pattern space)中的全部内容。

P(大写) Print up to the first embedded newline of the current pattern space.

打印当前模式空间(Pattern space)中内容的第一行数据。
提示:之前说过模式空间是可以存在多行数据的,p(小写)和P(大写)作用不同在于,前者打印全部,而后者只打印第一行。
/ +++++++++++++++++++++++++++++++++++++++例子27+++++++++++++++++++++++++++++++++++++

sed.txt的内容为:

This is a dog.

This is a cat.
sed命令:

sed -ne ' #按Enter键换行

>1{ #选择第一行

>N #将第二行追加到当前行

>P #大写P,打印当前模式空间的第一行

>}

>' sed.txt

执行以后显示结果为:

This is a dog.
将以上命令中的大写P换成小写p 执行后显示结果为:

This is a dog.

This is a cat.

/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

s/regexp/replacement/

Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes /1 through /9 to refer to the corresponding matching sub-expressions in the regexp.

尝试对模式空间中的内容进行正则表达式“regexp”的匹配,如果匹配成功,将会用“replacement”来代替匹配的部分;“replacement”可以包含特殊字符“&”,“&”用来代替匹配“regexp”的模式空间的内容,而/1、/2、/3……/9表示“regexp”的子表达式的匹配内容。

提示:1、2、3……等数据第几个子表达式的意思。
/ +++++++++++++++++++++++++++++++++++++++例子28+++++++++++++++++++++++++++++++++++++

sed.txt的内容为:
It is my dog,and your dog is there.
sed命令:sed -ne 's//(.*/)/(dog/)/(.*/)/(dog//(.*/)//1horse/3bird/5/g;p' sed.txt
执行结果:

It is my horse,and your bird is there.
其中/1代表第一个/(.*/)匹配的内容,这里匹配:It is my ;/3代表第二个/(.*/)匹配的内容,这里匹配的是:,and your ;/5代表第三个/(.*/)匹配的内容,这里匹配的是:is there.。

/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

w filename

Write the current pattern space to filename.

将当前模式空间(Pattern space)的全部内容写入到文件“filename”中

W filename

Write the first line of the current pattern space to filename.

将当前模式空间(Pattern space)内容中的第一行写入到“filename”中。
/ +++++++++++++++++++++++++++++++++++++++例子29+++++++++++++++++++++++++++++++++++++

sed.txt的内容为:

sed.txt的内容为:

This is a dog.

This is a cat.
sed命令:
sed -ne ' #按Enter键换行

>1{ #选择第一行

>N #将下一行追加到当前模式空间,并用换行符隔开

>w sed.write #将当前模式空间中的内容全部写入sed.write中

>}

>' sed.txt

执行以上命令sed.write的内容为:

This is a dog.

This is a cat.

如果将以上命令中的w换成W执行以后,sed.write的内容为:

This is a dog.

/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

y/source/dest/

Transliterate the characters in the pattern space which appear in source to the corresponding character in dest.

用另一种字母体系将模式空间(Patter space)内出现的“source”字符替换成对应的“dest”字符。
提示:这个命令说白了就是转换数据中的字元,例如指令:y/abc.../xyz.../针对这个指令 /abc.../xyz.../(x、y、z、a、b、c 代表某些字元)为y的参数,其中 abc... 与 xyz... 的字元个数必须相同;sed 执行转换时,将 模式空间内数据中的 a 字元转换成 x 字元 、b 字元转换成 y 字元 、c 字元转换成 z 字元……
/ +++++++++++++++++++++++++++++++++++++++例子30+++++++++++++++++++++++++++++++++++++

sed.txt的内容为:

sed.txt的内容为:

This is a dog.

This is a cat.
sed命令为:

sed -ne ' #按Enter键换行

>y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/ #将小写字母换成大写字母

>p #打印当前模式空间的内容

>' sed.txt

执行以后结果为:

THIS IS A DOG.

THIS IS A CAT.

注意那两个元字符集个数要一样,而且对应才能互相转换。

/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值