The UNIX and Linux Forums  


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



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Counting string of a variable Dip Shell Programming and Scripting 5 10-22-2008 09:19 AM
option of grep for counting exact word ?? maddy Linux 3 06-17-2008 09:55 AM
Need shell/sed script for grep+string replacement pranavagarwal Shell Programming and Scripting 3 11-15-2007 06:35 AM
Counting the max length of string ganesh123 Shell Programming and Scripting 2 02-23-2007 04:27 PM
Counting patterns in a shell string kevin80 UNIX for Dummies Questions & Answers 3 06-03-2003 10:34 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #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
  #2 (permalink)  
Old 06-24-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,800

Code:
for i in invALL.06* 
do 
   grep -c '^Invoice Total' $i  | read count
   echo "$i  has  $count Invoice Totals" 
done

Try that.
  #3 (permalink)  
Old 06-24-2009
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
This looks like a formatted print file complete with printer control codes.
We may need to convert the control characters to text before running a textual search. Also, we usually process an open ended list of files in "while" loop which also stops the script breaking if filenames contain space characters.
Try


Code:
ls -1d invALL.06* | while read FILENAME
do
       # sed -n l (character ell) converts control characters to text 
       count=`sed -n l "${FILENAME}" | grep -c 'Invoice Total'` 
       echo "${FILENAME} has ${count} Invoice Totals" 
done

I note that jim mcnamara has confined the search to lines starting "Invoice Total" which may already have fixed the problem! I may be delving too deep.

Last edited by methyl; 06-24-2009 at 07:04 PM.. Reason: Typo
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0