in reply to How to chomp the last new line character from the file

I have to use this file as an input to some other script and the last newline character is creating problems for me
this means that you are using each line of file so you can check the last line of file
$count = `wc -l < $file`; if($x==$count); { chomp($line); }
where $x is the loop count
$line represent the current line
and there wont be any issue weather your last charater of last line is new line or not because chomp only removes the last character if it is a newline.

Replies are listed 'Best First'.
Re^2: How to chomp the last new line character from the file
by davido (Cardinal) on Nov 24, 2011 at 10:38 UTC

    You don't need ($x) when you have ($.). perlvar.

    But all that's too much fuss when you have eof. There's no need to keep track of what constitutes the last line number.

    You could do it as a one-liner like this:

    perl -pi.bak -e 'chomp if eof' file.txt

    Dave