0

I'm on macOS 12.6 and have an issue with the multiline commands in sed.

From this script (taken from the manual): seq 6 | sed -n 'N;l;D' I should get this output:

1\n2$
2\n3$
3\n4$
4\n5$
5\n6$

But this is what I get:

1$
2$
2$
3$
3$
4$
4$
5$
5$
6$

I can't figure out why the newline isn't printed unambiguously (that's what the "l" command does)

Thanks in advance!

HatLess
  • 10,622
  • 5
  • 14
  • 32
ranemirusG
  • 143
  • 2
  • 16
  • 1
    I can't reproduce _with GNU sed v4.8_, I am getting the first output. Are you sure that you are really running the command you are running? What sed version and operating system are you using? – KamilCuk Oct 13 '22 at 16:26
  • @KamilCuk I'm on macOS 12.6 therefor I can't tell exactly the version (or maybe yes, but not with `sed --version` as I've just read [here](https://stackoverflow.com/questions/37639496/how-can-i-check-the-version-of-sed-in-os-x)). The date in the man page is June 10, 2020. – ranemirusG Oct 13 '22 at 17:50
  • 1
    I believe macos sed is based on freebsd sed. If you look at the [manual page](https://www.freebsd.org/cgi/man.cgi?query=sed&apropos=0&sektion=0&manpath=FreeBSD+13.1-RELEASE+and+Ports&arch=default&format=html) there is no reference to newline, so I would guess it is shown as is. – potong Oct 13 '22 at 22:57
  • 1
    You can use gsed on `macos` with homebrew https://brew.sh/ – HatLess Oct 14 '22 at 00:03
  • @HatLess I ended up by doing this and adding `alias sed="gsed"` in my .zshrc – ranemirusG Oct 14 '22 at 16:32

1 Answers1

1

why the newline isn't printed unambiguously

Because you are using BSD sed, not GNU sed, the newline is printed "ambiguously".

https://github.com/chimera-linux/bsdsed/blob/master/process.c#L627 -> prints a $+newline on newline. (The check above when the line is longer than terminal width.)

I should get this output:

l command is not standardized. Every sed can do what it wants with it. GUN sed prints it that way, BSD sed prints it the other, you can't expect.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111