1827788 Members
2670 Online
109969 Solutions
New Discussion

Script Urgent !!

 
SOLVED
Go to solution
KapilRaj
Honored Contributor

Script Urgent !!

Gentlemen [ oh ladies too ;) ]

I have two files

cat file1

host1-80 10
host2-80 15

cat file2

host1-80 30
host2-80 20

BAsically they are the number of hits to an apps server and the files are created on a daily basis. I want to print the diffrence of second column ( file2 - file1 )

i.e.,

cat result

host1-80 20
host2-80 5

Can anybody help with the simplest code. I can use Korn shell, perl, awk, sed .... anything.

Thanks in advance,

Kaps
Nothing is impossible
10 REPLIES 10
Richard Fogle
Occasional Contributor

Re: Script Urgent !!

Try this, did some perl:

#!/usr/contrib/bin/perl -w

my $file1 = "/tmp/foo1.txt";
my $file2 = "/tmp/foo2.txt";

open FILE, "< ".$file1 || die "Can't open file: $@";
@file1 = ;
close (FILE);

open FILE, "< ".$file2 || die "Can't open file: $@";
@file2 = ;
close (FILE);

foreach my $line1 (@file1) {
chomp($line1);
my @array1 = split(/ /, $line1);
foreach my $line2 (@file2) {
chomp($line2);
my @array2 = split(/ /, $line2);
if ($array1[0] eq $array2[0]) {
print "Difference for ".$array2[0].": ".($array2[1] - $array1[1])."\n";
}
}
}
Yang Qin_1
Honored Contributor

Re: Script Urgent !!

Hi, Kaps I assume you only have two hosts in each data file:

#!/usr/bin/ksh

host1var1=`grep "host1-80" file1|cut -d" " -f2`
host2var1=`grep "host2-80" file1|cut -d" " -f2`
host1var2=`grep "host1-80" file2|cut -d" " -f2`
host2var2=`grep "host2-80" file2|cut -d" " -f2`

host1rslt=$(($host1var2-$host1var1))
host2rslt=$(($host2var2-$host2var1))

echo "host1-80 " $host1rslt
echo "host2-80 " $host2rslt

exit


Yang
Jonathan Fife
Honored Contributor
Solution

Re: Script Urgent !!

cat file1 file2 | awk '{if (c[$1] == 0) {c[$1]=-$2; next;}
c[$1]=c[$1] + $2} END {for(f in c) printf("%s %d\n",f,c[f])}' | sort
Decay is inherent in all compounded things. Strive on with diligence
Jannik
Honored Contributor

Re: Script Urgent !!

#!/usr/bin/ksh

SERVERS="host1-80 host2-80"
RESULT=0

for i in $SERVERS
do
RESULT=0
HITFILE1=$(cat file1 | grep $i | awk '{print $2}')
HITFILE2=$(cat file2 | grep $i | awk '{print $2}')
if [ $HITFILE1 -gt $HITFILE2 ]
then
echo $HITFILE1 $HITFILE2
RESULT=`echo $HITFILE1-$HITFILE2|bc`
else
echo $HITFILE2 $HITFILE1
RESULT=`echo $HITFILE2-$HITFILE1|bc`
fi
echo $RESULT
done
jaton
Michal Toth
Regular Advisor

Re: Script Urgent !!

stupid expensive script, yet simple:

#!/bin/sh

cat file1 | while read host value1
do
value2=`grep $host file2|awk '{print $2}'`
echo $host `expr $value2 - $value1`
done
Hein van den Heuvel
Honored Contributor

Re: Script Urgent !!

#perl -ne '($h,$n)=split; if ($old=$o{$h}) {print "$h : ", $n - $old, "\n"} else {$o{$h}=$n}' file1.txt file2.txt
host1-80 : 20
host2-80 : 5


- read both files.
- split line into 'host name' and 'new count'
- if old count in array
--- then subtract and print
--- else stick new value into array.


Cheers,
Hein.
Richard Fogle
Occasional Contributor

Re: Script Urgent !!

I think Jonathan Fife wins this one with the awk one-liner, I just wanted to use perl in mine. Getting sick of nothing but shell utils.
Sean Dale
Trusted Contributor

Re: Script Urgent !!

I was going to post perl, too. I agree, the awk one liner is impressive.
Live life everyday
KapilRaj
Honored Contributor

Re: Script Urgent !!

Michael , I never thought of it. It was so simple.
Richard Fogle , Thanks It does work.
Yang, Thanks it works as long as the file has just 2 lines.
Jonathan Fife , I wish I could give you 20 points !! Fantastic !! Wow ! I wish I could script like that !
Jannik , Thanks .. it is again hard coded .. yet I appreciate your time spent on my request.
Hein van den Heuvel, Wow ! Perl is always perl ! Wonderfull !

Thank you very much guys ...

Regards,

Kaps
Nothing is impossible
KapilRaj
Honored Contributor

Re: Script Urgent !!

Thanks to all once again. Thanks for your time. Points are awarded.
Nothing is impossible