Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: delete blank lines in a txt file

by Wookie (Beadle)
on Jul 20, 2001 at 20:22 UTC ( [id://98458]=note: print w/replies, xml ) Need Help??


in reply to delete blank lines in a txt file

Assuming the blank lines are just carriage returns - the following should work:
#!/usr/bin/perl -w use strict; open (IN,"blah.file")||die ("Failed to open file blah.file\n$!"); open (OUT,"blah.file.nospace")||die("Failed to open file blah.file.out + for writing\n$!"); my $count=0; while (<IN>) { chomp $_; if ($_=~m/^$/g) { $count++; } else { print OUT "$_\n"; } } close IN; close OUT; print 'Deleted '."$count".' blank lines'."\n"; exit (0);
If you think you may have white spaces on your empty line - then you can change the line that reads:
if ($_=~m/^$/g) {
to read:
if ($_=~m/^\s*$/g) {
Hope this helps :)
game(Wookie,opponent) eq 'Wookie' ? undef $problem : remove_limbs(arms,opponent);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2025-03-18 04:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (56 votes). Check out past polls.