delete blank or enter at the end of file

December 6, 2012 at 00:25:45
Specs: Windows 7
I have one file in which at the end of file there is one blank line i.e. one enter after last line.
have one script which reads that file but because of that blank line its getting mess up in result so while reading i want to ignore that blank line or before reading can we delete that blank line so further result will be good?
Is anybody's having solution?

See More: delete blank or enter at the end of file

Report •

December 6, 2012 at 09:19:34
# this sed one-liner deletes the last line of the file. Does not check whether it's blank or not:

sed '$d' my.txt > mynew.txt

Report •

December 10, 2012 at 02:28:56
thanks for your reply, but is it possible to delete in same file? I dont want to create mynew.txt file for deletion.

Report •

December 10, 2012 at 08:56:47
The GNU sed version that runs on most Linux systems has an edit in place flag -i that works on the same file:

sed -i '$d' my.txt

But this won't work on legacy unix systems like AIX, Solaris, HP-UX, etc although GNU versions for these systems are available at

If your system has perl, this one-liner should work:

perl -i -ne 'print unless eof' my.txt

Report •
Related Solutions

Ask Question