As my old group friends knows, I know shell scripting but very new to perl scripting. Hence struggling now for the simple task. This should be done using perl. Any help for the below requirement would be greatful for me...
I've around 40 files, with the below layout:
file1.csv
C01;35047
C02;0
C03;0
C04;0
C05;1294
C06;0
C07;0
C08;0
Till now I've build the code as below:
my $report_dir = "$prereq_dir/REPORTS";
opendir DIR, $report_dir || die "Cannot opendir $report_dir $!";
while ( $filename = readdir(DIR) )
{
open(PAGE, $filename) || die "I can't open $filename";
Iam struggling to find the second field, count and totalling the values.
Any light on my dark way would be much appreciated...
I'm trying to decode the output: where is the count?
Or you just need to sum the values of the second field?
What should be the output from the sample input files above?
One more thing is that, I am writing a perl script and not a shell script. hence I cannot use the line "perl -F';' -lane'" (If Iam not wrong).
Please guide me...
Thanks for your answer radoulov
you always helped me. Still sorry for this questions again...
In the perl script I'm using "#!/usr/bin/perl" in the first line. Is it required to write perl in your first line perl -F';' -lane' ?
As you know I'm not an advanced perl programmer, could you please explain your code? i.e, what is the meaning of ' -lane' and where is the output file name etc...
Sorry, eventhough I'm wasting your valuable time, it would be very helpful for me..
But the problem is I'm not passing the files name in command line. I'm assigning these to variables inside the perl script. Same is for the output file.
No, if you need to add this logic to an existing code
and your program invokes the perl interpreter without any switches
you should leave it as it is and use something like this:
#!/usr/bin/perl
# you should always use these in a script
use warnings;
use strict;
# your existing code here
{
my $path = defined $ENV{'prereq_dir'}?$ENV{'prereq_dir'}.'/REPORTS/file':'./file';
my ($ext, $outfile, %data, @flds) = ('.csv', 'output.csv');
while (<$path*$ext>) {
open IN, $_ or warn "Error openning $_: $!\n" and next;
while (<IN>) {
@flds[1,2] = split ';';
$data{$flds[1]} += $flds[2];
}
close IN;
}
open OUT, '>', $outfile;
print OUT map {"$_;$data{$_}\n"} sort keys %data;
}
# other code if any
I'm not either and I'm sure there is a better way to write the above
I've restructured my code with your solution as below:
#!/usr/bin/perl
# you should always use these in a script
use warnings;
use strict;
# your existing code here
my $report_dir = "/export/home/PREREQUISITS/20080430/REPORTS";
opendir DIR, $report_dir or die "Cannot opendir $report_dir $!";
while ( my $filename = readdir(DIR) )
{
open( PAGE, $filename ) or die "I can't open $filename";
my $path = defined $report_dir?$report_dir.'/REPORTS/file':'./file';
my ($ext, $outfile, %data, @flds) = ('.csv', 'output.csv');
while (<$path*$ext>)
{
open IN, $_ or warn "Error openning $_: $!\n" and next;
while (<IN>)
{
@flds[1,2] = split ';';
$data{$flds[1]} += $flds[2];
}
close IN;
}
open OUT, '>', $outfile;
print OUT map {"$_;$data{$_}\n"} sort keys %data;
}
But this is throwing one warning while compiling, which I'm not being able to solve. Code should be error/warning free... Could you please have a look on this?
$ perl -c ganesh.pl
Name "main::PAGE" used only once: possible typo at ganesh.pl line 14.
ganesh.pl syntax OK
#!/usr/bin/perl
# you should always use these in a script
use warnings;
use strict;
my $report_dir = '/export/home/PREREQUISITS/20080430/REPORTS/';
my ($ext, $outfile, %data, @flds) = ('.csv', 'output.csv');
while (<$report_dir*$ext>) {
open IN, $_ or warn "Error openning $_: $!\n" and next;
while (<IN>) {
@flds[1,2] = split ';';
$data{$flds[1]} += $flds[2];
}
close IN;
}
open OUT, '>', $report_dir.$outfile or die "Error creating output file: $!\n";
print OUT map {"$_;$data{$_}\n"} sort keys %data;
A small problem with the code. If I run the below program one time, it is working perfectly. But if I run this program more than 2-3 times, the hash and array contents are keep on adding, eventhough I've initialized in the begining. Which are resulting big numbers in the output file.
Pls have a look and help me...
#!/usr/bin/perl
# you should always use these in a script
use warnings;
use strict;
my %data;
my @flds=();
my $report_dir = '/export/home/L86898/MYDATA/PREREQUISITS/20080430/REPORTS/';
my ($ext, $outfile) = ('.csv', 'Summary_by_TestScript.csv');
while (<$report_dir*$ext>) {
open IN, $_ or warn "Error openning $_: $!\n" and next;
while (<IN>) {
@flds[1,2] = split ';';
$data{$flds[1]} += $flds[2];
}
close IN;
}
open OUT, '>', $report_dir.$outfile or die "Error creating output file: $!\n";
print OUT map {"$_;$data{$_}\n"} sort keys %data;
#!/usr/bin/perl
# you should always use these in a script
use warnings;
use strict;
my %data;
my @flds=();
my $report_dir = '/export/home/L86898/MYDATA/PREREQUISITS/20080430/REPORTS/';
my ($ext, $outfile) = ('.csv', 'Summary_by_TestScript.csv');
while (<$report_dir*$ext>) {
next if $_ eq $report_dir.$outfile;
open IN, $_ or warn "Error openning $_: $!\n" and next;
while (<IN>) {
@flds[1,2] = split ';';
$data{$flds[1]} += $flds[2];
}
close IN;
}
open OUT, '>', $report_dir.$outfile or die "Error creating output file: $!\n";
print OUT map {"$_;$data{$_}\n"} sort keys %data;
Sorry for my stupid question. But I failed understand the answers from the perl study material regarding "map" function.
Could you please explain me what exactly the below code of line is doing using map() ? Are there any other ways to do the same thing without using map() ?
One of my friend has asked this question, but I's failed to answer him also failed to understand from perl book...