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

Addresses

Sed commands can be given with no addresses, in which case the command will be executed for all input lines; with one address, in which case the

command will only be executed for input lines which match that address; or with two

addresses, in which case the command will be executed for

all input lines which match the inclusive range of lines starting from the first address and continuing to the second address. Three things to

note about address ranges: the syntax is addr1,addr2 (i.e., the addresses are separated by a comma); the line which addr1 matched will always be

accepted, even if addr2 selects an earlier line; and if addr2 is a regexp, it will not be tested against the line that addr1 matched.

Sed 命令可以不用给定地址(通过行号或者正则表达式选定),这时候命令将会对所有的行进行处理;如果有一个地址,指定将会作用于该地址选择的每一行输入;如果有两个地址,命令将会作用于从地址1到地址2的所有行。关于地址范围的三个提示:1.地址表达语法为addr1,assr2(地址之间用逗号隔开);2.所有匹配第一个地址的数据行都会被处理,尽管第二个地址比第一个地址先匹配;3.如果第二个地址是一个正则表达式,它将不会对第一个地址匹配的行进行测试。

补充:关于指定两个地址的情况,命令将会对由addr1和addr2匹配而组成的数据行进行操作,其中组行是这样划分的,首先由addr1匹配,匹配的数据行作为第一组的开始,第二个地址addr2要选择后续的行来匹配,选择到的行作为第一组的尾行,如果没有在后续行找到与第二个地址addr2匹配的行,那么第二个地址将会指向文件末尾,以上就是第一组数据行;接着addr1又开始从第一组数据行后匹配数据行作为下一组的行首,然后addr2匹配数据行为行尾,如此循环,直到将整个文件处理完毕。
After the address (or address-range), and before the command, a ! may be inserted, which specifies that the command shall only be executed if

the address (or address-range) does not match.

在地址后面,和在命令之前之间肯会插入一个感叹号(!),它将会明确指出,只有那些不匹配地址(或者范围地址)的行才会被命令处理。

The following address types are supported:

Sed同时还支持以下地址类型

number Match only the specified line number.

选择由number明确指定的行。

first~step

Match every step’th line starting with line first. For example, ‘‘sed -n 1~2p’’ will print all the odd-numbered lines in the input stream, and the address 2~5 will match every fifth line, starting with the second. (This is an extension.)

选择由first指定的行开始(包括first指定的行),每隔step的行。比如:“sed –n 1~2p”将会从输入流中取出奇数行来打印,地址“2~5”将从第2行开始匹配,同时下来每5行选择匹配一行。

$ Match the last line.

选择最后一行。

/regexp/

Match lines matching the regular expression regexp.

选择匹配正则表达式的行。

/cregexpc

Match lines matching the regular expression regexp. The c may be any character.

选择匹配正则表达式的行,其中c可以是任何字符。(说白了就是用c来指定正则表达式,平时我们都用斜杠,就是c默认的是斜杠,你可以指定其他字符)
GNU sed also supports some special 2-address forms:
GNU sed同时还支持特殊的两个地址(2-address)的形式:

0,addr2

Start out in "matched first address" state, until addr2 is found. This is similar to 1,addr2, except that if addr2 matches the very first line of input the 0,addr2 form will be at the end of its range, whereas the 1,addr2 form will still be at the beginning of its range.

开始时就处于第一个地址已经匹配了的状态,(对应的命令就会一开始就处理行)直到找到第二个地址。这一点很像1,addr2,但是如果addr2匹配了输入的第一行,“0,addr2”的行范围就会在第一行结束,但是“1,addr2”的范围才刚刚开始。

解释:不管是“0,addr2”还是“1,addr2” 行的范围都只有一个,而且都是一开始就默认已经处于该范围之中了,直到匹配add2,则在执行与之对应的代码后退出该地址范围;他们的不同是“1,addr2”的addr2是从第二行开始去检查, “0,addr2” 的addr2是从第一行开始检查。

addr1,+N

Will match addr1 and the N lines following addr1.

将会选择匹配了addr1和它后面的N行作为地址范围。

addr1,~N

Will match addr1 and the lines following addr1 until the next line whose input line number is a multiple of N.
将会选择匹配了addr1和它后边的行,直到输入的下一行的行号是N的倍数。
解释:例如:sed –ne ‘/2/,~3p’ textfile 将会选择含有2的行作为地址范围的开始,直到行号为3的倍数比如行号为3、6、9、12的行(包括此行)。假设行号为4的行包含2那么,这个行将作为开始,然后到取第五行,到了第六行,6为3的倍数,作为地址范围的结束,那么这个地址范围为第4,5,6行;第六行之后再去匹配/2/重复以上,可能就会得到多个地址范围。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值