Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to chomp the last new line character from the file

by jojojo (Initiate)
on Nov 24, 2011 at 09:33 UTC ( [id://939828]=perlquestion: print w/replies, xml ) Need Help??

jojojo has asked for the wisdom of the Perl Monks concerning the following question:

I want to remove the last new line character in a file which is being generated by another code. I have to use this file as an input to some other script and the last newline character is creating problems for me. Is there any way I can do it?

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

    A little Tie::File fun:

    perl -MTie::File -e 'tie @a, q{Tie::File}, shift; chomp $a[-1]' file.t +xt

    Dave

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.
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>
Re: How to chomp the last new line character from the file
by jmcnamara (Monsignor) on Nov 24, 2011 at 11:33 UTC

    Here is one way:

    perl -i -pe 'chomp if eof' file1 file2 ...

    --
    John.

Re: How to chomp the last new line character from the file
by Tux (Canon) on Nov 24, 2011 at 13:04 UTC

    In the spirit of TIMTOWTDI ...

    $ perl -pe'eof&&chomp' file | nextprocess $ perl -0pe's/\n\Z//' file | nextprocess

    Enjoy, Have FUN! H.Merijn
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.

      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

Re: How to chomp the last new line character from the file
by sundialsvc4 (Abbot) on Nov 28, 2011 at 13:13 UTC

    1. Verify that the file exists and that it is writable by you.
    2. Seek to the end of the file, obtaining its length in bytes.   Exit if zero.
    3. 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.)
    4. Truncate the file at length - 1.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://939828]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found