Basic grep -c Question

Hello,

I have a csv list which contains a reference and tariff code for every customer we have (~7.2 million) looking something like this:

customernumber,tariff
012345678910,T0001
012345678911,T0002
012345678912,T0001-A0001

I have a 2nd list which contains every unique tariff and tariff-add on combination on our system (~1000 entries) and it looks like this:

T0001
T0001-A0001
T0001-A0002
T0002
T0002-A0001
T0002-A0002

Using the unique tariff list I wanted to count the number of customers with each tariff/tariff-addon combination on the system. So I cam up with the following grep:

cat uniq-tariff-addon.csv | while read tariff
do 
echo $tariff `grep -c $tariff customer_tariff_list.csv` >> sub-tariffcount.csv; done

My problem being that it catches the customers with a tariff-addon in the count of just tariff only as follows:

customernumber,tariff
012345678910,T0001
012345678911,T0002
012345678912,T0001-A0001 (where A000X is an addon)

grep -c T0001 in the above file would return a count of 2
grep -c T0001-A0001 in the above file would return a count of 1

How can I get it to count T0001 and not catch T0001-A0001 as well?

Thanks in advance
Cludgie

Stop the match at the end-of-line (indicated by the $-sign following $tariff):

 while read tariff; do echo $tariff $(grep -c $tariff\$ customer_tariff_list.csv); done <uniq-tariff-addon.csv

There's options to grep as well that make it match whole words or entire lines only ... see man grep .

1 Like

Thanks for the quick response RudiC. I tried using grep -xc, but it still caught T0001-A0001 when T0001 was the criteria. :frowning:

I also tried your line, but it stops here:
T000001 0

Thanks
Cludgie

-x does not make sense for your requirement, it matches entire lines. Try -w.

1 Like

as a test I tried the following

grep -w T000001 customer_tariff_list.csv

012345678918,"T000001-A00220"

The above is the only entry for anyone with this old tariff on the system, but as you can see they have an addon, which means it shouldn't be caught by grep.

Any other thoughts?

Thanks
Cludgie

Is that add-on enclosed in double quotes (as indicated in your last post)?

It is, sorry I failed to mention that in the original post. My mistake

Try

while read tariff; do echo $tariff $(grep -c $tariff\" file1); done <file2
T0001 1
T0001-A0001 1
T0001-A0002 0
T0002 1
T0002-A0001 0
T0002-A0002 0
1 Like

Nailed it!!! Thanks very much RudiC.