Perl report problem...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl report problem...
# 1  
Old 07-02-2008
Java Perl report problem...

Hi All,

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


file2.csv
C01;197874
C02;20
C03;9
C04;0
C05;0
C06;0
C07;406
C08;0


and so on up to 40 files.

I've to read all the 40 files and for the non-zero second column I need the count and total values in to a summary file 'summary_report.csv'.

Example Final Report should be as below:

C01;35047
C02;20001
C03;3002
C04;32224
C05;1294
C06;23
C07;8474
C08;737




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...
# 2  
Old 07-02-2008
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?

Trying to guess:

Code:
perl -F';' -lane'
  $_{$F[0]} += $F[1];
  END { 
    $, = ";";
	for (sort keys %_) {
      print $_, $_{$_}
  }
}' "$prereq_dir"/REPORTS/*.csv

Or:


Code:
perl -F';' -lane'
  $_{$F[0]} += $F[1];
  END {
    print map {"$_;$_{$_}\n"} sort keys %_ 
	}'  "$prereq_dir"/REPORTS/*.csv

If you want to exclude 0 values:

Code:
perl -F';' -lane'
  $_{$F[0]} += $F[1];
  END { 
    $, = ";";
	for (sort keys %_) {
      print $_, $_{$_} if $_{$_}     
  }
}'  "$prereq_dir"/REPORTS/*.csv


Last edited by radoulov; 07-02-2008 at 01:07 PM..
# 3  
Old 07-02-2008
Java

Sorry for leading you towards misunderstanding...
Please consider the below 3 sample files:

file1.csv
C01;10
C02;0
C03;0
C04;0
C05;20
C06;0
C07;0
C08;0

file2.csv
C01;50
C02;20
C03;90
C04;0
C05;0
C06;0
C07;40
C08;0


file3.csv
C01;0
C02;0
C03;0
C04;10
C05;30
C06;80
C07;40
C08;60

Then the resultant output file should be as follows:

output.csv
C01;60
C02;20
C03;90
C04;10
C05;50
C06;80
C07;80
C08;60

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...
# 4  
Old 07-02-2008
So:

Code:
zsh-4.3.4% head *csv
==> file1.csv <==
C01;10
C02;0
C03;0
C04;0
C05;20
C06;0
C07;0
C08;0

==> file2.csv <==
C01;50
C02;20
C03;90
C04;0
C05;0
C06;0
C07;40
C08;0

==> file3.csv <==
C01;0
C02;0
C03;0
C04;10
C05;30
C06;80
C07;40
C08;60

Code:
zsh-4.3.4% perl -F';' -lane'
  $_{$F[0]} += $F[1];
  END {
    print map {"$_;$_{$_}\n"} sort keys %_
}' *csv
C01;60
C02;20
C03;90
C04;10
C05;50
C06;80
C07;80
C08;60

Quote:
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).
[...]
?

The command above is a perl script, not a shell script ...

Perhaps you prefer something like this:

Code:
zsh-4.3.4% cat perl_script
#!/usr/bin/perl -lanF;

$_{$F[0]} += $F[1];

END {
      print map {"$_;$_{$_}\n"} sort keys %_;
}
zsh-4.3.4% ./perl_script *csv
C01;60
C02;20
C03;90
C04;10
C05;50
C06;80
C07;80
C08;60


Last edited by radoulov; 07-02-2008 at 01:17 PM..
# 5  
Old 07-02-2008
Thanks for your answer radoulov Smilie
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..

Thanks again / Mysore Ganapati.
# 6  
Old 07-02-2008
another way
Code:
while (<>) {
    ($f1,$f2) = split(/[;]/, $_, -1);
    $a{$f1} += $f2;
}
foreach $i (keys %a) {
    print $i,":",$a{$i},"\n";
}

output:
Code:
# ./test.pl file1 file2 file3
C04:10
C03:90
C05:50
C02:20
C07:80
C06:80
C01:60
C08:60

# 7  
Old 07-02-2008
Thanks a lot ghostdog74,

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.

So, could you please suggest me for this...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Report generation using perl script

Hi, I have a perl script to read the log file and create a report from it. I have the script file and log file in a different directories. Now i have pipe the log file data to the perl script to create the report (HMTL file). I am using the below command this isn't working tail -f... (4 Replies)
Discussion started by: vel4ever
4 Replies

2. Shell Programming and Scripting

Help in modifying existing Perl Script to produce report of dupes

Hello, I have a large amount of data with the following structure: Word=Transliterated word I have written a Perl Script (reproduced below) which goes through the full file and identifies all dupes on the right hand side. It creates successfully a new file with two headers: Singletons and Dupes.... (5 Replies)
Discussion started by: gimley
5 Replies

3. Web Development

problem with exporting vairable from one perl cgi to another perl cgi script while redirecting.

Can anyone tell me how to export a variable from one perl CGI script to another perl cgi script when using a redirect. Upon running the login.pl the user is prompted to enter user name and password. Upon entering the correct credentials (admin/admin) the user is redirected to welcome page. My... (3 Replies)
Discussion started by: Arun_Linux
3 Replies

4. Shell Programming and Scripting

Disk report generation problem

Hello everyone, I have a list of inputs as below. My logic is to get the particular powerdisk which matches for ASM disk which means take the major & minor number of each asm disk and matches with powerdisk info then get the particular powerdisk $ ls -l /dev/asm_* ---> ASM disk info... (11 Replies)
Discussion started by: kannan84
11 Replies

5. Shell Programming and Scripting

perl script for generates a report for backup times

Hi all, we do have daily oracle database backups and i need to generate report of each database start point and end point we are using netapp snapshot(filer level) for backups. all backups logs will be come in one directory /u01/app/oracle/backup/hostname/log/* here... (7 Replies)
Discussion started by: prakashdba2010
7 Replies

6. Programming

Problem with perl ~ tr///

I am trying to run the following script which is a file format converter. The frame variable in the input file has a file of 3,2,1 which needs to be 0,1,2 respectively i.e. 3 =0 etc. I have included the tr/// function into the script to do this, however it does not seem to be working input its... (2 Replies)
Discussion started by: fordie
2 Replies

7. Shell Programming and Scripting

problem with perl

hi, i have a script that coverts the file time in epoch time.but the problem is perl is not working inside the k-shell ---------------------------------------------------------------- #!/bin/ksh echo "Enter the file" read n perl -e 'print ((stat("n")))'... (6 Replies)
Discussion started by: ali560045
6 Replies

8. Shell Programming and Scripting

Perl problem

I have been recently given a PERL script to develop, but the main problem is that the perl version that I have to use is old, also I cant download modules from CPAN. Perl version 5.0005 I didnt realise this untill I had the script ready to be tested, so there are a few modules that I have... (6 Replies)
Discussion started by: meevagh
6 Replies

9. Shell Programming and Scripting

Problem getting data to a report file.

Hi all, I'm trying in vain to workout how I can generate a report from a months worth of files that get created every day. There is one file per day and each daily file contain the output from a df -v command. With the following section of code ... for xdffile in $1$2/df?? do ... (4 Replies)
Discussion started by: Cameron
4 Replies
Login or Register to Ask a Question