sed学习笔记(二)--地址范围
时间:2007-12-19 来源:kinwin
sed maintains two data buffers: the active pattern space, and the auxiliary hold space. Both are initially empty. sed operates by performing the following cycle on each lines of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed. When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.Then the next cycle starts for the next input line. Unless special commands (like `D') are used, the pattern space is deleted between two cycles. The hold space, on the other hand, keeps its data between cycles (see commands `h', `H', `x', `g', `G' to move data between both buffers). |
number 具体的行号 |
first~step GNU扩展 只打印奇数行
|
sed -n '1~2p' |
$ 最后一行 |
/regexp/ 匹配该正则表达式的行 |
\%regexp%(%可为其他符号) 匹配正则表达式的行 |
/regexp/I \%regexp%I GNU扩展,忽略大小写 |
/regexp/M \%regexp%M The M modifier to regular-expression matching is a GNU sed extension which causes ^ and $ to match respectively (in addition to the normal behavior) the empty string after a newline, and the empty string before a newline. There are special character sequences (\` and \') which always match the beginning or the end of the buffer. M stands for multi-line. |
addr1,addr2 addr1和addr2之间的部分。若addr2<addr1,则只取addr1 |
一些GNU特殊的
0,/regexp/ 类似于1,/regexp/,但在范围上有所不同。 0,/regexp/会从第一行开始匹配regexp;而 1,/regexp/会从第二行开始匹配regexp
|
$ cat kin |
addr,+N addr,addr+N |
addr,~N ~N代表N的倍数 |