06-24-2009
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
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Hello,
I am writing a shell script and I need to find a way to
count the number of whitespaces in a string.
Eg:
NAME="Bob Hope"
I am looking for a way to count the number of whitespaces in this string. So a command that would take this string and return 1.
Or take
"First Middle Last"... (3 Replies)
Discussion started by: kevin80
3 Replies
2. Shell Programming and Scripting
Hi all,
I have a flat file of 1000 rows. I want to check the length of the 5th column. The one having the longest length , I want to set it as DEFINED PARAMETER.
So later I can check others with that particular number only.
Any ideas ?? (2 Replies)
Discussion started by: ganesh123
2 Replies
3. Shell Programming and Scripting
Hi,
Let me explain the situation.
There are many files in a directory and its sub-directories that conatin the string pattern "pa". I want to replace all such instances with the pattern "pranavagarwal"
doing a
grep "pa" `ls`
does give me all the instances of the occurence of that... (3 Replies)
Discussion started by: pranavagarwal
3 Replies
4. Linux
Hi All,
I have a quary regarding grep command in linux.
I have a file which contains
56677
56677
+56677
+56677
56677
56677
56677
I want to extract total count of "56677"
When I hit the following command
#cat filename | grep -w -c '56677'
the result comes 7. Its counting... (3 Replies)
Discussion started by: maddy
3 Replies
5. Shell Programming and Scripting
Hi,
There is a variable f_name, it store some file names.
Value of f_name=a.sql b.sql c.sql.......
like this.
want to count how many file name the var f_name stores.
Without using loop is there any command to count that. (5 Replies)
Discussion started by: Dip
5 Replies
6. Shell Programming and Scripting
I have requirement to prepare script which will grep for latest outofmemory message from the logs. I have used following command to grep the string from the logs,this script is not effective when logs are not getting updated as it will grep for old message.
f=catalina.out
var=`tail -10 $f |... (17 Replies)
Discussion started by: coolguyamy
17 Replies
7. UNIX for Dummies Questions & Answers
Hi All,
I hv several files which have hundreds of lines each for example>XYZ.abc01
NNNTCGGTNNNNNCCACACACMYACACACCCACACCCACSCARCAC
I'd like to exculde the first line beginning with ">" and then for the rest of the lines get a count for each string element. So for the above example I would like... (8 Replies)
Discussion started by: pawannoel
8 Replies
8. UNIX for Dummies Questions & Answers
Hello,
I have a string like this 0:1:2:0:2:2:4:0:0:0:-200:500......
what i want is to break down how many different characters are there and their count. For example for above string it should display
0 - 5 times
1 - 1 times
2 - 3 times
4 - 1 times
.
.
.
I am stuck in writing... (8 Replies)
Discussion started by: exit86
8 Replies
9. Shell Programming and Scripting
I have a list of URLs and I want to be able to count the number of instances of addresses ending in a certain TLD and output and sort it like so.
5 bdcc.com
48 zrtzr.com
49 rvo.com
Input is as so
ync.org
sduzj.edu
sduzj.edu
sduzj.edu
sduzj.edu
sduzj.edu
sduzj.edu
sduzj.edu... (1 Reply)
Discussion started by: Pjstaab
1 Replies
10. Shell Programming and Scripting
I have been working on this for a little while and have been unable to come to a solution. Any help would be appreciated. I am working on a UNIX workstation and have a 30-40 meg text file that I am working with. In my real file there is hundreds of Jobs. Example of input file;
misc logging data... (1 Reply)
Discussion started by: ny_evan
1 Replies