Operating System - Linux
1829108 Members
13608 Online
109986 Solutions
New Discussion

Generate consolidated Report

 
Rahul_13
Advisor

Generate consolidated Report

Hi,

I have some reports like the one attached along with this thread. I need to write a script which will create a master report with the combined data from all the reports.

For instance in the attached report, I have

ID Attempts Success Failure

11 20 18 2

I need to add Attempts,Sucess, Failure of all the ID=11 from the different files and report to the master file. Similiarly, I need to report the other IDs also.

Also,I need to read the input file names from a configuration file.

Please help. Either PERL or UNIX script would do.

Thanks,
Rahul
3 REPLIES 3
Jean-Luc Oudart
Honored Contributor

Re: Generate consolidated Report

Hi
this assumes all filenames *.txt and id 2 digits only.
Adapt when required.

###########################################
#!/bin/sh

grep ^[0-9][0-9]" " *.txt | cut -d":" -f2 | sort | awk '{
if($1==oldid) {
attempts+=$2;
success+=$3;
failure+=$4;
}
else {
print oldid,attempts,success,failure;
oldid=$1;
attempts=$2;
success=$3;
failure=$4;
}
}
END{print oldid,attempts,success,failure;}'

Regards
Jean-Luc
fiat lux
Peter Nikitka
Honored Contributor

Re: Generate consolidated Report

Hi,
try the following:

awk 'NR==1 {nfield=NF;header=$0;next}
/^[0-9]/ && NF==nfield {ids[$1]=$1;for(i=2;i<=NF;i++) f[$1,i]+=$i }
END {print (header,"\n")
for (id in ids) {printf(id); for(i=2;i<=nfield;i++) printf("\t%s",f[id,i]); printf("\n")}}' file1 .... filen

This will ignore lines not having the identical number of fields of the first header.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Muthukumar_5
Honored Contributor

Re: Generate consolidated Report

You can try with PERL scripting as,

#!/usr/contrib/bin/perl
#master.pl
$i=0;
while (<>)
{
($id,$attempt,$success,$failure)=split;
$I_arr[$id]=$id;
$A_arr[$id]+=$attempt;
$S_arr[$id]+=$success;
$F_arr[$id]+=$failure;
}

foreach $var (@I_arr)
{
if ($var !~ /^$/)
{
printf "ID= $I_arr[$var] Attempt = $A_arr[$var] Success = $S_arr[$var] Failure = $F_arr[$var] \n";
}
}

# perl master.pl > master.pl

will make it.

hth.
Easy to suggest when don't know about the problem!