Text editors will typically “helpfully” add a newline to the end of a text file upon saving it. There are cases when such a newline character is not desired. This newline character is typically not shown to the user in the editor and can thus be difficult to remove.
This is how it can be removed on Linux using truncate
frmo GNU coreutils:
truncate -s -1 foo.txt
This is remove the last byte of the file foo.txt
.
Note: it will remove the last byte regardless of it being a newline or not.
An alternative way is to use perl
if available:
perl -pi -e 'chomp if eof' foo.txt
This will only remove the trailing newline if such a newline exist.