![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 08:19 AM |
| option of grep for counting exact word ?? | maddy | Linux | 3 | 06-17-2008 08: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 09:34 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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
Last edited by methyl; 06-24-2009 at 06:04 PM.. Reason: Typo |
| Sponsored Links | ||
|
|