The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 06-24-2009
scarletsupra scarletsupra is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 1
grep -c script counting string twice instead of once?

I tried this script to get a count of the occurrence of a string in files.
I have multiple files in one directory I will use this on.
All the filenames begin "invALL.06" The script works, except it counts twice for every one instance of 'Invoice Total'.
If there are 5 occurences of 'Invoice Total' in a file it returns 10.
I have tried it with grep -cn and grep -cx. No better.
I have tried the string "Last Page" same results.
I tested by searching for the word "Continued" in the files. It counts them correctly. Does it have to do with grep -c on more than a one word string?
Thanks.

Script
for i in invALL.06*
do

count=`grep -c 'Invoice Total' $i`

echo $i "has " $count "Invoice Totals"

done


Outputs:
invALL.0605 has 870 Invoice Totals
invALL.0606 has 936 Invoice Totals


The file it is searching looks like below. It only has "Invoice Total" at the end of each Invoice section.

&k2G&f504y4X(8U(s0p12.00h0s1b4102T&l6D&k10H&k2G&f504y4X(8U(s0p12.00h0s1b4102T&l6D&k10H& a0h60V

Invoice
000000 06/19/09 84888-00
25070 ZZ joe 1
06/19/09 14:40
Sell to me co name LLC (MD)
123 your street DR 777 my DRIVE
SUITE z
your town, VA 24551 irvington, MD 21061

sell to me
123 your street DR 555-555-1212 Net 30 Days 07/19/09
yourtown, VA 24551 sold to you, L john 06/19/09


order for
this customer.
hold for 6/17/09 delivery
1 ED51 1 0 1 SHT 2.68 2.68
15#
2 la4 5 0 5 each 8.00 90.00
040
3 EW4 5 0 5 sht 7.35 36.75
4MM
=============
Subtotal for Non-Taxable: 89.43

3 Lines Total Qty Shipped Total 11 Total 89.43
Fuel Surchrg 3.50
Invoice Total 92.93








Last Page
&k2G&f504y4X(8U(s0p12.00h0s1b4102T&l6D&k10H&k2G&f504y4X(8U(s0p12.00h0s1b4102T&l6D&k10H& k2G&f504y4X(8U(s0p12.00h0s1b4102T&l6D&k10H&k2G&f504y4X(8U(s0p12.00h0s1b4102T&l6D&k10H&a0 h60V

Invoice
000000 06/19/09 848700-01
25070 ZZ Ron 1
06/19/09 14:40
selltome isell, LLC (MD)
123 sellme 123 wesell DRIVE
SUITE z
pinetree, VA 24000 pinetree, az 12121

sellto me
123 myroad 222-555-1212 Net 30 Days 07/19/09
tree, VA 24000 soldfromme, jjones 06/19/09


for
this customer.
5 YMA 2 0 2 each 3.65 7.30
A24
=============
Subtotal for Non-Taxable: 7.30

1 Lines Total Qty Shipped Total 2 Total 7.30
Invoice Total 7.30











Last Page