Re: How to chomp the last new line character from the file
by davido (Cardinal) on Nov 24, 2011 at 10:13 UTC
|
perl -MTie::File -e 'tie @a, q{Tie::File}, shift; chomp $a[-1]' file.t
+xt
| [reply] [Watch: Dir/Any] [d/l] |
Re: How to chomp the last new line character from the file
by choroba (Cardinal) on Nov 24, 2011 at 09:54 UTC
|
Try chomping the last line. Or if you are sure the last character in the file is to be deleted, you can truncate the file to $size - 1. | [reply] [Watch: Dir/Any] [d/l] |
Re: How to chomp the last new line character from the file
by rovf (Priest) on Nov 24, 2011 at 09:57 UTC
|
truncate should do the job.
Note that you can get the size of a file using the -s operator.
--
Ronald Fischer <ynnor@mm.st>
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: How to chomp the last new line character from the file
by jmcnamara (Monsignor) on Nov 24, 2011 at 11:33 UTC
|
perl -i -pe 'chomp if eof' file1 file2 ...
--
John.
| [reply] [Watch: Dir/Any] [d/l] |
Re: How to chomp the last new line character from the file
by Tux (Canon) on Nov 24, 2011 at 13:04 UTC
|
$ perl -pe'eof&&chomp' file | nextprocess
$ perl -0pe's/\n\Z//' file | nextprocess
Enjoy, Have FUN! H.Merijn
| [reply] [Watch: Dir/Any] [d/l] |
Re: How to chomp the last new line character from the file
by ansh batra (Friar) on Nov 24, 2011 at 10:29 UTC
|
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.
| [reply] [Watch: Dir/Any] [d/l] |
|
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
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: How to chomp the last new line character from the file
by sundialsvc4 (Abbot) on Nov 28, 2011 at 13:13 UTC
|
- Verify that the file exists and that it is writable by you.
-
Seek to the end of the file, obtaining its length in bytes. Exit if zero.
-
Seek one byte back from where you are. Read that byte. Exit if not a newline. (Assuming no double-bye character-set nonsense; otherwise, you must back up one character.)
-
Truncate the file at length - 1.
| [reply] [Watch: Dir/Any] |