Checking the last line and deleting

Hi everyone,

I have a file. I have to search whether the last line is empty(blank line) or not. if it is a blank line, I have to delete it. I dont want to move it to a temp file and again to original file after deleting the last line because I am doing some more modification in that file. Just I want to check whether the last line is blank or not and delete it if it is blank. Pls help.

sh-2.05b$ cat 1.txt
1
2
3
4

sh-2.05b$ wc -l < 1.txt
      5
sh-2.05b$ vi 1.txt 2> /dev/null <<EOF
> G
> :g:^ *$:d
> :wq
> EOF
sh-2.05b$ cat 1.txt
1
2
3
4
sh-2.05b$ wc -l < 1.txt
      4
sh-2.05b$ 

If you are doing "more changes" in this file you may already run a sed script.

sed '$ {
            /^[<spc><tab>]*$/d
         }'

will only affect the last line and delete a line with only whitespace. Enter a literal tab and space character instead of <tab> and <spc>.

bakunin

perl -pi -e "if(eof) { /^\s*$/ ; chomp ; }" filename

This will remove the last line of file only if that line is blank. It will directly edit the file without having to make a temp copy of the file.
Hope this helps