O 04/09/21 ás 12:12, ropers escribiu:
> However, I can't get the newline to work right in OpenBSD's sed. It
> does work in GNU sed.
>
> man sed has this:
>
>> The escape sequence \n matches a newline character embedded in the
>> pattern space. You can't, however, use a literal newline character
>> in an address or in the substitute command.
>
> Now I *thought* \n was NOT considered a literal newline, and that I
> should thus be able to get this to work, but I'm a little stumped.
>
> Googled information suggests that the opposite of what's described in
> the man page may be true: You CAN use a literal newline, but you
> can't use \n. But I'm not sure if the googled information is correct
> and I still haven't figured this one out.
>
> In trying to get this to work, I took a FILE that contains a bunch of
> abc's, sometimes several to a line, sometimes not, and I did
> this[0]:
>
> $ sed s/abc/abc^M/g FILE > DAFTFILE
>
> Here ^M *was supposed to be* a literal newline, i.e. I pressed ^V
> followed by the return key, but readers more alert than me can
> doubtlessly already see the problem: Apparently ^V[enter] yields a
> literal carriage return, not a literal newline (^J). That's the case
> on Linux as well, and I don't know why.
>
> One interesting observation, if someone does get this to work, is
> that this :!sed s/abc/abc\n/g % | grep -c abc is only shorter than
> this :!cat % | tr '\n' ' ' | awk '{print gsub(/abc/, "")}' if the
> abc string is short.
To use newlines with sed I use tr and a char I know does not appear on
the text, like '|' or '`'. I just tested
:!sed s/abc/abc€/g % | tr '€' '\n' | grep -c abc
and it worked fine.
No comments:
Post a Comment