I can match newline by \n
:
echo "one
two" | sed 'N;s/\n/_/g'
In GNU sed
, I can use [^\n]
to match any character but newline:
echo "one
two" | sed 'N;s/[^\n]/_/g'
This is very handy, but it violates POSIX. Other sed
versions correctly answer __n______
Same thing with tab character, but there I can work around by using an actual tab character, preceeded by ctrl-v. But this doesn't work for newline:
echo "one
two" | sed 'N;s/[^
]/_/g'
gives me unbalanced brackets
.
Using [^[:cntrl:]]
only works while there are no other control characters I want to match.
So what's the correct way to match any character but newline in POSIX sed
?
sed 'H;1h;$! d;...
and then do operations on the whole text in one buffer. In those cases it may be useful to match anything but newline. How to do that elegantly? – Philippos May 01 '17 at 12:04.*
or.+
will not work? – George Vasiliou May 01 '17 at 12:09.
matches anything but NUL character, including newline. I'm preparing another question where this is needed with buffer example. – Philippos May 01 '17 at 12:25.
matches everything exceptnull
you could pre-process the file liketr '\n' '\0'
or even you could make it likesed '......' <(tr '\n' '\0' <file)
(just thoughts) – George Vasiliou May 01 '17 at 12:30sed
it is not possible to use\0
either. – Philippos May 01 '17 at 14:43N
in the first examples, that doesn't create a newline in the pattern space. Also: it is theN
that reads lines in pairs, not the regex. The regex will match all newlines. – Sep 23 '21 at 12:49