How can I delete a newline if it is the last character in a file?
awk, linux, perl, sed, shell
Solution
perl -pe 'chomp if eof' filename >filename2
or, to edit the file in place:
perl -pi -e 'chomp if eof' filename
[Editor's note: `-pi -e` was originally `-pie`, but, as noted by several commenters and explained by @hvd, the latter doesn't work.]
This was described as a 'perl blasphemy' on the awk website I saw.
But, in a test, it worked.
Problem
I have some files that I'd like to delete the last newline if it is the last character in a file. `od -c` shows me that the command I run does write the file with a trailing new line: ``` 0013600 n t > \n ``` I've tried a few tricks with sed but the best I could think of isn't doing the trick: ``` sed -e '$s/\(.*\)\n$/\1/' abc ``` Any ideas how to do this?