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

-l N, --line-length=N

specify the desired line-wrap length for the ‘l’ command

也就是说如果后面的command中如果有l(是英文L的小写),就可以通过这里的-l选项来指定每一行的显示长度,默认是70个字符,如果超过,将会通过跳脱字符“/”来换行。例如 sed -n -l 10 '1,2l' textfile 将第一和第二行按每行10个字符显示,包括转义字符。
注意:该选项必须要与后面的 –e 指定的指令l一起用。
/ +++++++++++++++++++++++++++++++++++++++例子6++++++++++++++++++++++++++++++++++++++
sed.txt的内容如下:

This is the 1st line.

This is the 2nd line.

This is the 3rd line.
sed命令:sed –n –l 10 –e ‘l’ sed.txt 执行后显示:

This is t/

he 1st li/

ne.$

This is t/
he 2nd li/

ne.$

This is t/

he 3rd li/

ne.$
l命令是指用可见的形式打印当前行,$表示行的末尾,而且通过反斜杠换行,如果行中有制表符将会显示/t 上面的命令就是每行显示10个字符。

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

--posix

disable all GNU extensions.

禁用所有的GNU扩展。
补充:GNU sed没有早期专门版本的很多限制,如行长度限制,可以轻松处理任意长度的行。
-r, --regexp-extended

use extended regular expressions in the script.

在脚本中使用扩展正则表达式。
解释:标准的正则表达式由特殊字符“.” 、“^”、“$”、“/”、“*”、“/{n.m}/”、“[]”组成,扩展的正则表达式由特殊字符“+”、“?”、“|”、“()”组成。
/ +++++++++++++++++++++++++++++++++++++++例子7++++++++++++++++++++++++++++++++++++++
sed.txt的内容如下:

This is the 1st line.

this is the 2nd line.

This is the 3rd line.
1.sed命令(不带-r选项):sed -ne 's/T.?/**/p' sed.txt 执行后没有任何显示;

2.sed命令(带-r选项):sed –n –r –e 's/T.?/**/p' sed.txt 执行以后显示:

**is is the 1st line.

**is is the 3rd line.
它匹配了以大写字母T开头接着是一个任意字符的两个字符,然后通过s命令替换为“**”。

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

-s, --separate

consider files as separate rather than as a single continuous long stream.

将输入文件视为各个独立的文件而不是一个长的连续输入。

解释:意思是如果[input-file]选项如果指定有多个文件的话,默认的sed将会视为一个单独的流,然后对这个流进行操作,而如果加了-s选项了以后,多个文件将会视为单独的流,后面的操作将会对每一个单独的流进行同样的操作,比如 sed –n -s –e ‘1p’ textfile1 textfile2 将会同时显示textfile1和textfile2的第二行,也就是加起来一共输出两行,但是如果是sed –n –e ‘1p’ textfile1 textfile2 这样的话由于textfile2将会加到textfile1中作为一个流来处理,这样只会出现一行也就是合并以后的流的第二行。
/ +++++++++++++++++++++++++++++++++++++++例子7++++++++++++++++++++++++++++++++++++++
sed1.txt的内容:

This is the 1st line.

This is the 2nd line.
sed2.txt的内容:
This is the 3rd line.

This is the last line.
1.sed命令:sed –n –s –e ‘4p’ sed1.txt sed2.txt 不显示任何行,因为-s强调了将多个文件视为独立的流;

2.Sed命令:sed –n –e ‘4p’ sed1.txt sed2.txt 执行后显示:

This is the last line.

可见不加-s多个文件会和起来作为一个流;比如这里的sed1.txt和sed2.txt 合起来的流为:

This is the 1st line.

This is the 2nd line.

This is the 3rd line.

This is the last line.

存在第四行了,所以’4p’ 就会显示最后一行了。

/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-u, --unbuffered

load minimal amounts of data from the input files and flush the output buffers

从输入文件装载最少的数据,并且刷新输出缓冲区,也就是说尽量少缓冲input和output 要随时更新。
解释:用一个例子来理解,比如:我开启一个shell终端,在这个终端输入tail -n 2 -f textfile | sed -n -u -e 'p' 不要关闭,然后又开启另一个shell终端,然后我通过 echo 'This is the 17th line' >>textfile 此时你会发现,第一个shell那里马上会刷新出现这一行。也就是说-u保证更新的数据随时得到sed的处理。
/ +++++++++++++++++++++++++++++++++++++++例子8++++++++++++++++++++++++++++++++++++++

sed.txt的内容为:

This is the 1st line.

sed命令为:tail –n 2 –f sed.txt | sed –n –u –e ‘p’

打开一个shell终端,输入以上sed命令不要关闭;

打开另一个shell终端,输入echo ‘This is the 2nd line.’ >>sed.txt此时你会发现第一个终端就会刷新出这一行,-u起到了随时刷新输出缓冲区的作用,没有-u选项的话就不会有这个功能的。

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值