agc*_*agc 4 sed regular-expression
对于 GNU sed
v4.2.2-7,info sed
说:
'/REGEXP/M'
'\%REGEXP%M'
The 'M' modifier to regular-expression matching is a GNU 'sed'
extension which directs GNU 'sed' to match the regular expression
in 'multi-line' mode. The modifier 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. In addition, the period
character does not match a new-line character in multi-line mode.
Run Code Online (Sandbox Code Playgroud)
没有给出例子。经过测试,这个/M
后缀实际上做了什么并不明显。它的行为似乎根本没有 /M
。
那么 的简单重要用法是/M
什么?其中“最简单”的意思是“你好世界”简单,不需要对其他程序有太多额外的知识,而“重要”意味着它应该用 '/M' 做一些值得注意的事情,如果缺少它就无法完成。
例如,一个实例:
seq 10 | sed -n '<code>;/<some regexp>/Mp'
Run Code Online (Sandbox Code Playgroud)
...的行为不同于:
seq 10 | sed -n '<code>;/<some regexp>/p'
Run Code Online (Sandbox Code Playgroud)
这相当于正m
则perl
表达式运算符中的标志,或者(?m)
在 perl regexps 或 PCRE 中使用(尽管gsed
的M
标志也会删除s
perl 标志,因为没有M
,sed
的.
匹配换行符,而使用perl
,则需要该s
标志.
来匹配新队)。
这些标志仅在模式空间包含多行时起作用,例如使用-z
, (读取 NUL 分隔的记录),或使用G
,N
或等命令向模式空间添加行时s
。
$ seq 3 | sed 'N;s/$/<foo>/g'
1
2<foo>
3
$ seq 3 | sed 'N;s/$/<foo>/Mg'
1<foo>
2<foo>
3
Run Code Online (Sandbox Code Playgroud)
之后N
,模式空间包含1<newline>2
. 没有M
,$
仅匹配模式空间的末尾,(在2之后);with M
,$
匹配该模式空间中第一行的末尾(在1之后,但在换行符之前)和模式空间的末尾(在2之后)。