Wednesday, April 26, 2006

How to strip extra lines in a file?

Problem :
Need a script to remove all the blank lines from a file.

Solution:

Save the following as strip.pl and use perl to run the script.

#############################################################
###################### Bipin C Nair #########################
##################### http://www.bipin.co.nr ################
#################### Written on 2003-12-23 ##################

$linecount = 1;
if ( $ARGV[0] )

{
$filename = $ARGV[0];
}
else
{
print "Specify file to strip of extra lines"; exit;
}
open(INFILE,"<$filename") die "can't write to your output file";

@mapfile = ;
close ( INFILE);
open(OUTFILE,">$filename") die "can't write to your output file";
foreach $line (@mapfile)
{
if ( $line =~ m/^\n/)
{
$line =~ s/^\n//;
}
print OUTFILE "$line";
$line = "";
$linecount = $linecount + 1;
}
print "Converted $linecount lines";

close(OUTFILE);
print "The file is stripped of extra line breaks, enjoy!!\n";exit;


© Copyright 2006. Bipin C Nair. All rights reserved.

0 Comments:

Post a Comment

<< Home