Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How to remove newline characters at the end of file

by ashokpj (Hermit)
on Feb 21, 2012 at 09:34 UTC ( [id://955256]=perlquestion: print w/replies, xml ) Need Help??

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

In my file there are multiple new line char at EOF. I want to remove only new line char at end of file.

Line 1….\n Line2….\n \n Line3 …\n Line4….\n \n \n

Expected output

Line 1….\n Line2….\n \n Line3 …\n Line4….\n

Thanks in advance

Replies are listed 'Best First'.
Re: How to remove newline characters at the end of file
by Eliya (Vicar) on Feb 21, 2012 at 11:01 UTC

    Truncating just the superfluous newlines should work, too.  This would avoid rewriting the entire file, and/or reading it into memory.

    open my $fh, "+<", $fname or die $!; my $pos = (-s $fh) - 1; my $s; do { seek $fh, $pos--, 0; read $fh, $s, 1; } while $s eq "\n"; truncate $fh, $pos + 3;

    (On Windows you'd need to truncate at $pos + 4, or else you'd have just a trailing \r, not a complete \r\n.)

    Update: ww remarked via /msg that on Windows ActiveState Perl < 5.14, truncate only works on closed files (according to his tests), but that as of 5.14(.2) it does behave as expected.   I don't have Windows here to verify, so I can only pass this on as is.   Thanks anyway!

Re: How to remove newline characters at the end of file
by GrandFather (Saint) on Feb 21, 2012 at 09:55 UTC

    The first part of the trick is to 'slurp' the file. Set the special variable $/ to undef to read the entire file in one hit:

    use strict; use warnings; my $file = <<FILE; 1 2 3 FILE open my $fIn, '<', \$file; my $str = do {local $/; <$fIn>}; close $fIn; print "1-->\n$str<--1\n"; $str =~ s/\n+\Z//; print "2-->\n$str<--2\n";

    Prints:

    1--> 1 2 3 <--1 2--> 1 2 3<--2
    True laziness is hard work
Re: How to remove newline characters at the end of file
by moritz (Cardinal) on Feb 21, 2012 at 09:47 UTC

    What have you tried so far?

    When you read an empty line, you can't know yet whether to print it. So you need something along the lines of

    my $newlines = 0; while (<$file>) { if (/^$/) { $newlines++; } else { print "\n" x $newlines; print; $newlines = 0; } }

    (untestested).

    update: closed regex, ww++.

Re: How to remove newline characters at the end of file
by trizen (Hermit) on Feb 21, 2012 at 13:15 UTC
    use Tie::File; foreach my $filename (grep { -f } @ARGV) { print "** Processing $filename\n"; tie my @file, 'Tie::File', $filename or die "Unable to tie: $!\n"; pop @file while $file[-1] eq q{}; untie @file or die "Unable to untie: $!\n"; print "** Done...\n\n"; }
    You can specify multiple files as command line arguments.
Re: How to remove newline characters at the end of file
by JavaFan (Canon) on Feb 21, 2012 at 10:08 UTC
    A oneliner:
    perl -ne'$b.=$_;/./||next;print$b;$b=""' your-file
      Brilliant !!
Re: How to remove newline characters at the end of file
by planetscape (Chancellor) on Feb 22, 2012 at 00:47 UTC
Re: How to remove newline characters at the end of file
by Mandrake (Chaplain) on Feb 21, 2012 at 23:29 UTC
    A shorter command line version.
    perl -0pe '~s/\n+$/\n/' your-file
    cheers

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-25 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found