I have a file that does not have an EOL at the end of the last line. I noticed this when my bash script that reads from it was not processing the last line.
I read from it like this:
while read LINE
do
...
done <thefile
The file is supplied to me, and so there is nothing I can do to it before it lands on my system (Linux – OpenSuSE 10.2). I do run dos2unix on the file, but that does not resolve the missing EOL.
I have seen a couple of solutions involving vi and ed, but they are a bit clunky and I was hoping there is a neater solution, maybe using sed, that I can use from within my bash script?
Oddly, when I vi the file and do a :set list, I can see a “$” at the end of the last line. I was expecting that to be missing as I thought that “$” represented \n. Or maybe there is a difference between newline and end-of-line?
sed -i -e '$a\' "path"will add a newline at EOF only if it doesn’t already have one (explanation after the jump).If you want to process a file which might have no newline at EOF, you have to check whether the
readoutput variable exists after the loop:Not very elegant, but at least you don’t have to modify the input before processing it.