1

I have a program that always adds a newline character to a file/stream. I want to remove this last linefeed.

However, on the command line this seams very difficult. When a stream ends with an empty line, this works perfectly:

printf $'line 1\nline 2\n\n' | sed '${/^$/d;}'

However, when there is only one linefeed at the end, sed doesn't do a thing:

printf $'line 1\nline 2\n' | sed '${/^$/d;}'

So how to remove this last newline character...

It should work on the command line of macOS. I think it's save to assume the line always will end with a newline character, but if there is a solution that will test for this new line that would be preferable. And also the solution doesn't need to use sed....

doekman
  • 18,750
  • 20
  • 65
  • 86

0 Answers0