[Solved] Messaging data into required report

Hello to all;

hope someone can assist me in getting the required output that my manager is expecting.

I have been able to generate this code which does the comparison of the files and creates the file called diff_fuss_file.txt

[dslgvol@rua ~]$ vi fussrpt.pl
#!/usr/bin/perl

#cd /tmp
#rm output.txt

# PERL MODULES USED
#use strict;
use warnings;
use DBI;
use DBD::mysql;

# CONFIG VARIABLES
my $DB='ops_filetransfer';
my $HOST='pele';
my $user='dslops';
my $pass='dslops72';
my $table='lsof_conf';

# my VARIABLES
my $myfile="/tmp/fuss_lsof_conf.txt";
my $mydiff_file="/tmp/diff_fuss__file.txt";

# PERL DBI CONNECT
my $dbh=DBI->connect( "DBI:mysql:database=$DB;host=$HOST;", $user, $pass) or die "$!\n";

# PREPARE\CONSTRUCT THE QUERY
my $sth=$dbh->prepare("SELECT id, t_client, t_filelocation, t_remotescript, t_destination, t_enabled, t_search, t_compression FROM $table") or die "$!\n";

# EXECUTE THE QUERY
$sth->execute();

#OPEN OUTPUT FILE AND WRITE EACH FETCHED ROW FROM THE DATABASE INTO IT WITH A NEW LINE RETURN AFTER EACH RECORD
open(FH, ">$myfile") or die "$!\n";
while (my @row=$sth->fetchrow_array()) {
        print FH join(",", @row)."\n";
}

#CLOSE OUTPUT FILE AND TERMINATE DB CONNECTION
close FH;
$sth->finish();
$dbh->disconnect();

$nmofchgs = `more /tmp/diff_fuss__file.txt | wc -l`;
print "Total number of rules changed or added: $nmofchgs\n";

open (FILE, '/tmp/diff_fuss__file.txt');

while (<FILE>) {
 chomp;
 ($rule, $client) = split("\,");
 print "   Client: $client\n";
 print "     Rule: $rule\n";
 print "\n";
}
close (FILE);

============================
Data is in a file called diff_fuss_file.txt

Data is csv and here's a sample:

51176,qsc,/ureports/fusqsc/stmt,/home/dslmain/qsc/scripts/copylsoftosftpwithmove.sh,qscsftp@sftp1: prod/qsc/dsi/xml,EXPORT,<where>7862<any>DSI<any>.xml,YES
120004,bns,dslmain@faunus:import/svc,/home/dslmain/bns/scripts/copylsoffromsftpnomove.sh,/ureports/exportbns/svc,IMPORTNOMOVE,<where>fndmut<any>.svc,NO
120006,gjv,/ureports/exportbns/extracts/sent,/home/dslmain/bns/scripts/copylsoftosftp.sh,bnssftp@sftp1: prod/bns/export/extracts,NO,<where><any><yesterday>,NO
120007,bns,dslmain@faunus:import/svc,/home/dslmain/bns/scripts/copylsoffromsftpnomove.sh,/ureports/exportbns/svc,IMPORTNOMOVE,<where>fndmut<any>.svc,NO
120008,fid,/ureports/ibfid/jot/rpts/nofiche,/home/dslmain/fid/scripts/copylsoftosftp.sh,fidsftp@sftp1: prod/fid/jot/rpts/nofiche,YES,<where><any><yesterday><any>,NO

My output looks like this:

Total number of rules changed or added: 5

Client: qsc
     Rule: 51176

   Client: bns
     Rule: 120004

   Client: gjv
     Rule: 120006

   Client: bns
     Rule: 120007

   Client: fid
     Rule: 120008

However my manager prefers this:

Rules rules changed for 5 clients: QSC, BNS x2, GJV, FID
The total number of Rules changed are: 5
Rules changed for QSC: 51176
Rules changed for BNS: 120004, 120007
Rules changed for GJV: 120006
Rules changed for FID: 120008

And finally he wants this saved in a file called fuss_audit_<insert run date>

Fairly new to Perl...thanks in advance for any and all suggestions\solutions.

Sincerely
Giuliano

Hi gvolpini,

Correct me if I'm wrong, but as I understand the problem is with the format of the output data.

This could be a solution, adapt it to your source. I assume that infile has the CSV content of your script, and next program prints it as you wish.

$ cat infile
51176,qsc,/ureports/fusqsc/stmt,/home/dslmain/qsc/scripts/copylsoftosftpwithmove.sh,qscsftp@sftp1: prod/qsc/dsi/xml,EXPORT,<where>7862<any>DSI<any>.xml,YES
120004,bns,dslmain@faunus:import/svc,/home/dslmain/bns/scripts/copylsoffromsftpnomove.sh,/ureports/exportbns/svc,IMPORTNOMOVE,<where>fndmut<any>.svc,NO
120006,gjv,/ureports/exportbns/extracts/sent,/home/dslmain/bns/scripts/copylsoftosftp.sh,bnssftp@sftp1: prod/bns/export/extracts,NO,<where><any><yesterday>,NO
120007,bns,dslmain@faunus:import/svc,/home/dslmain/bns/scripts/copylsoffromsftpnomove.sh,/ureports/exportbns/svc,IMPORTNOMOVE,<where>fndmut<any>.svc,NO
120008,fid,/ureports/ibfid/jot/rpts/nofiche,/home/dslmain/fid/scripts/copylsoftosftp.sh,fidsftp@sftp1: prod/fid/jot/rpts/nofiche,YES,<where><any><yesterday><any>,NO
$ cat script.pl
use warnings;
use strict;

my (%client_data, $num_clients);

while ( my $line = <> ) {
        my ($rule, $client) = split /,/, $line;
        ++$num_clients;
        push @{ $client_data{ uc $client } }, $rule;
}


printf qq[Rules changed for %d clients: %s\n], 
                $num_clients, 
                join qq[, ], 
                        map { 
                                @{ $client_data{ $_ } } > 1 ? 
                                        $_ . qq[ x] . scalar @{ $client_data{ $_ } } 
                                                : 
                                        $_ } 
                                keys %client_data;

printf qq[The total number of Rules changed are: %d\n], $num_clients;

for ( keys %client_data ) {
        printf qq[Rules changed for %s: %s\n], 
                $_, 
                join qq[, ], @{ $client_data{ $_ } };
}

$ perl script.pl infile
Rules changed for 5 clients: QSC, FID, GJV, BNS x2
The total number of Rules changed are: 5
Rules changed for QSC: 51176
Rules changed for FID: 120008
Rules changed for GJV: 120006
Rules changed for BNS: 120004, 120007

Regards,
Birei

Thanks birei...will give it a try !!

---------- Post updated at 11:00 AM ---------- Previous update was at 10:26 AM ----------

Worked like a charm..I just had to add the initial file open as I am not sending the source file as a parameter....thanks SO MUCH !!!

I new that this could only really be done with arrays however I have not yet reached that level of understanding of coding arrays.

Regards
Giuliano