[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: multiline M regexp extension not working in gnu sed 4.2.1?
From: |
Ralf Wildenhues |
Subject: |
Re: multiline M regexp extension not working in gnu sed 4.2.1? |
Date: |
Mon, 19 Jul 2010 21:54:35 +0200 |
User-agent: |
Mutt/1.5.20 (2010-04-22) |
Hello Ryan,
* Ryan B wrote on Sun, Jul 18, 2010 at 08:06:46PM CEST:
> hi all! i'm trying to use gnu sed's M extension for multiline regex
> pattern matching, but i can't get it to work. i'm using gnu sed 4.2.1
> on ubuntu lucid. as a data point, other gnu extensions like I (case
> insensitive) work happily.
There is a misunderstanding here. The M modifier does not change the
line-oriented operation mode of sed. In order to to multi-line
matching, you first need to get more than one line into pattern space,
e.g., with the N command. Then, you can match inner newlines with \n
(portably to Posix sed even):
$ (echo a; echo b) | sed 'N; s,a\nb,X,'
X
$
Now, the (GNU sed-specific) M modifier allows to match an inner newline even
with ^ or $ (which otherwise only match at the beginning or the end of the
pattern space:
$ (echo a; echo b) | sed 'N; s,a$,X,'
a
b
$ (echo a; echo b) | sed 'N; s,a$,X,M'
X
b
Of course, if your input file consists of more than two lines, and your
multi-line matching is supposed to happen at each line, not just at even
line numbers, take care to not discard both lines you slurped in:
$ (echo a; echo a; echo a; echo a) | sed 'N; s,a$,X,M'
X
a
X
a
$ (echo a; echo a; echo a; echo a) | sed -n '
:do
$p
N
s,a$,X,M
P
D
bdo'
X
X
X
a
Hope that helps.
Cheers,
Ralf