Two things about your script: 1. you should probably chomp your input lines; 2. it would probably be better to open your output file only once, i.e. before entering the for loop.
Update: and you don't need an empty else block.
Comment on Re: printing the entire row with particular given last column criteria