Remove space in whole file -perl

Hi all,

I have a file which have say about 123,000 records, the records in it look like:

1294160401681,05-01-2011 00:00:01,68,Cjw,2,3333,7,1100,900,SUCCESS,200,68,localhost,
1294160406515,05-01-2011 00:00:06,68,Cjw,2,0207,7,1100,900,SUCCESS,200,68,localhost,
1294160410097,05-01-2011 00:00:10,68,Cjw,2,3333,7,1100,900,SUCCESS,200,68,localhost,
1294160413815,05-01-2011 00:00:13,68,Cjw,2,3333,7,1100,900,SUCCESS,200,68,localhost,
1294160417389,05-01-2011 00:00:17,68,Cjw,2,3333,7,1100,900,SUCCESS,200,68,localhost,

1294160421087,05-01-2011 00:00:21,68,Cjw,2,3333,7,1100,900,SUCCESS,200,68,localhost,
1294160425975,05-01-2011 00:00:25,68,Cjw,2,0208,7,1100,900,SUCCESS,200,68,localhost,
1294160430483,05-01-2011 00:00:30,68,Cjw,2,0207,7,1100,900,SUCCESS,200,68,localhost,

1294160435542,05-01-2011 00:00:35,68,Cjw,2,0207,7,1100,900,SUCCESS,200,68,localhost,
1294160439128,05-01-2011 00:00:39,68,Cjw,2,3333,7,1100,900,SUCCESS,200,68,localhost,

...
This is how it looks, if u notice there is a space or carriage return between some records and im planning to remove that. So the the records that i want it look like:

1294160401681,05-01-2011 00:00:01,68,Cjw,2,3333,7,1100,900,SUCCESS,200,68,localhost,
1294160406515,05-01-2011 00:00:06,68,Cjw,2,0207,7,1100,900,SUCCESS,200,68,localhost,
1294160410097,05-01-2011 00:00:10,68,Cjw,2,3333,7,1100,900,SUCCESS,200,68,localhost,
1294160413815,05-01-2011 00:00:13,68,Cjw,2,3333,7,1100,900,SUCCESS,200,68,localhost,
1294160417389,05-01-2011 00:00:17,68,Cjw,2,3333,7,1100,900,SUCCESS,200,68,localhost,
1294160421087,05-01-2011 00:00:21,68,Cjw,2,3333,7,1100,900,SUCCESS,200,68,localhost,
1294160425975,05-01-2011 00:00:25,68,Cjw,2,0208,7,1100,900,SUCCESS,200,68,localhost,
1294160430483,05-01-2011 00:00:30,68,Cjw,2,0207,7,1100,900,SUCCESS,200,68,localhost,
1294160435542,05-01-2011 00:00:35,68,Cjw,2,0207,7,1100,900,SUCCESS,200,68,localhost,
1294160439128,05-01-2011 00:00:39,68,Cjw,2,3333,7,1100,900,SUCCESS,200,68,localhost,

...
Here is the code which i have written.. but someone should help me in completing it... thanks in advance

open(file_tmp,"<","$chk_file");
while (my $line = <file_tmp>)
{
$line =~ s/\n//g;
open(out,">>",tmpfile);
print out $line;
close(out);
}
close(file_tmp);

Please correct me if im wrong.. thank you.

If you are not set on using Perl, this is a good way of removing empty lines:

awk NF file
1 Like

Hi Scrutinizer,

Thank you for your help. It is very useful for me.

using Perl:-

perl -wlane 'print if @F;' infile.txt
or
perl -wlne 's/^\s*$//g or print' infile.txt