sed-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

What is the use case of the /M regex modifier in an address range?


From: Arkadiusz Drabczyk
Subject: What is the use case of the /M regex modifier in an address range?
Date: Tue, 4 Feb 2025 23:52:49 +0100

In the manual it says:

> M
> 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.

I understand why it would be very useful with s command:

$ seq 5 | sed -En ':a;N;$!ba; s,^,added to every line ,gMp'
added to every line 1
added to every line 2
added to every line 3
added to every line 4
added to every line 5

vs:

$ seq 5 | sed -En ':a;N;$!ba; s,^,added to every line ,gp'
added to every line 1
2
3
4
5

But why would it be useful in address? Other commands operate on the
entire address space, for example there is no difference with p:

$ seq 5 | sed -En ':a;N;$!ba; /^[0-9]/ p'
1
2
3
4
5
$ seq 5 | sed -En ':a;N;$!ba; /^[0-9]/M p'
1
2
3
4
5

Or d:

$ seq 5 | sed -En ':a;N;$!ba; /^[0-9]/M d'
$ seq 5 | sed -En ':a;N;$!ba; /^[0-9]/ d'
$

-- 
Arkadiusz Drabczyk <arkadiusz@drabczyk.org>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]