Reputation: 716
i have to grep one each line from a file and count the occurences in another file . the contents of two template files are:
template_service:
CO_SERVICE_SVCID268435463;Main Service
CO_FT_NON_BESTOWED_SVCID268435464;Bestowed Service
CO_SFT_NON_BESTOWED_SVCID268435465;Non Bestowed Service
template1(made out of first file itself) :
CO_SERVICE_SVCID268435463
CO_FT_NON_BESTOWED_SVCID268435464
CO_SFT_NON_BESTOWED_SVCID268435465
which contains search string(ie each different line). and there is another file(text3.txt) with 1,00,000+ lines which contains either of the three above mentioned strings(the content of template_service may also vary,it is not fixed).
this is the code i have written:
file="/somepath/etc/template_service.txt"
while IFS= read -r line
do
echo "$line" | cut -d';' -f1 >>template1.txt
echo "$line" | cut -d';' -f2 >>template2.txt
grep -c -f template1.txt text3.txt >>final_count.txt <--problematic statement showing incorrect count
done <"$file"
This should count the occurance of each line from file one in text3.txt and print the result in final_count.txt This is showing the output as :
60000
120000
180000
But the output should be :
60000
60000
60000
Why is it adding the previous sum as well?
Upvotes: 2
Views: 4138
Reputation: 1857
You always append to the templateX.txt
files (you use >>
).
So the second time grep is called it uses the first two lines from template_service.txt, and all three for the third time.
Instead try using >
to write the templateX.txt
files.
Upvotes: 1
Reputation: 123708
Why is it adding the previous sum as well?
Because of this:
echo "$line" | cut -d';' -f1 >>template1.txt
Changing it to:
echo "$line" | cut -d';' -f1 >template1.txt
should fix the issue. Using >>
causes the line to be appended to the file causing the previous count to be added to the result.
Upvotes: 2