I am writing the below script to do a grep and count number of occurances between two tab delimited files.
I am trying to achieve..
1) Extract column 2 and column 3 from the S.txt file. Put it in a temp pattern file
2) Grep and count column 2 in D.txt file
3) Compare the counts between D.txt and S.txt files.
4) Abort if counts doesn't match
Example: APPLE occurs 4 times in D.txt and is a match in S.txt
#!/usr/bin/ksh
SUM_COUNT=`nawk '{if ($0 ~ /^S/) print $2,$3 >"S1.txt" }'` S.txt
for i in S1.txt
do
DETAIL_COUNT=`grep $i D.txt | wc -l`
if [ ${DETAIL_COUNT} -eq ${SUM_COUNT} ]
then
echo "Count between Detail and Summary matches"
else
echo "Count didn't match"
exit
fi
done
The script goes in a loop and never exits....I am not sure if this is the right way to code.
S.txt
S APPLES 4
S ORANGES 1
S PEARS 1
S PINEAPPLES 1
S TOMATOES 0
S PEPPERS 1
D.txt
D PINEAPPLES
D ORANGES
D PEARS
D APPLES
D APPLES
D APPLES
D APPLES
D PEPPERS
I am still in the learning phase and would appreciate any input on this..