当使用数字方式的行寻址时,可以用行在文本流中的行位置来引用。sed编辑器会将文本流中的第一行编号为1,然后继续按顺序为接下来的行分配行号。
在命令中指定的地址可以是单个行号,或是用起始行号、逗号以及结尾行号指定的一定区间范围内的行。这里有个sed命令作用到指定行号的例子。
$ sed '2s/dog/cat/' data.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
$
sed编辑器只修改地址指定的第二行的文本。这里有另一个例子,这次使用了行地址区间。
$ sed '2,3s/dog/cat/' data.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog
$
如果想将命令作用到文本中从某行开始的所有行,可以用特殊地址——美元符。
$ sed '2,$s/dog/cat/' data.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
$
当并未明确知道文本中到底有多少行数据时,美元符用起来通常很方便。