Perl report problem...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl report problem...
# 8  
Old 07-02-2008
Quote:
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'* ?
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:

Code:
#!/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

Quote:
As you know I'm not an advanced perl programmer ...
I'm not either and I'm sure there is a better way to write the above Smilie

Quote:
[...]could you please explain your code? i.e, what is the meaning of *' -lane'* and where is the output file name etc...
See perldoc perlrun

Last edited by radoulov; 07-03-2008 at 02:42 AM..
# 9  
Old 07-03-2008
Radoulov,

You are shining with your prompt answers and suggestions.
Thanks again for your valuable time and help.

Cheers~Smilie~ / Mysore Ganapati...
# 10  
Old 07-03-2008
Hi Radoulov,

I've restructured my code with your solution as below:

Code:
#!/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?

Code:
$ perl -c ganesh.pl
Name "main::PAGE" used only once: possible typo at ganesh.pl line 14.
ganesh.pl syntax OK


Last edited by radoulov; 07-03-2008 at 07:37 AM.. Reason: added code tags
# 11  
Old 07-03-2008
Try changing it like this:

Code:
#!/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;

# 12  
Old 07-03-2008
It worked.. Smilie

Thanks a lot radoulovSmilie
# 13  
Old 07-03-2008
Hi,

Are you able to read the contents of the files?

Sorry you already got the solution right? No problem i wait for your next query Smilie

Last edited by Shreedhar Naik; 07-03-2008 at 10:16 AM.. Reason: .
# 14  
Old 07-04-2008
Hi Radoulov,

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...



Code:
#!/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;


Last edited by radoulov; 07-04-2008 at 05:17 AM.. Reason: add code tags
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