文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>sed学习笔记(二)--地址范围

sed学习笔记(二)--地址范围

时间:2007-12-19  来源:kinwin

sed是如何工作的?

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
1
2
1
kinwin@ustc-king:~
$ sed -n '0,/1/p' kin
1
kinwin@ustc-king:~
$ sed -n '1,/1/p' kin
1
2
1


addr,+N     addr,addr+N


addr,~N ~N代表N的倍数

Appending the ! character to the end of an address specification negates the sense of the match. That is, if the ! character follows an address range, then only lines which do not match the address range will be selected. This also works for singleton addresses, and, perhaps perversely, for the null address.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载