FInd all string occurences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FInd all string occurences
# 8  
Old 12-04-2016
The -exec awk can be run several times; then the "Total" does not work as intended.
So the pipe must be there...
This User Gave Thanks to MadeInGermany For This Post:
# 9  
Old 12-04-2016
Thanks for pointing that out. Try
Code:
find . -name "*.txt" -exec awk '{CNT[FILENAME]+= gsub (/ 0/, "&") - gsub (/"AC_STAT 0/, "&")} END {for (c in CNT) print c, CNT[c]}' {} + | awk '{TOT += $2} 1; END { print "Total", TOT}'

# 10  
Old 12-04-2016
Thank you, RUdi!

Which portion is my original search string though?

This portion?

Code:
(/ 0/

# 11  
Old 12-04-2016
Yes. You can also use " 0".
# 12  
Old 12-04-2016
Thank you!

So:

Code:
find . -name "*.txt" -exec awk '{CNT[FILENAME]+= gsub (/" 0"/, "&") - gsub (/"AC_STAT 0/, "&")} END {for (c in CNT) print c, CNT[c]}' {} + | awk '{TOT += $2} 1; END { print "Total", TOT}'

Or is it:

Code:
find . -name "*.txt" -exec awk '{CNT[FILENAME]+= gsub (" 0", "&") - gsub (/"AC_STAT 0/, "&")} END {for (c in CNT) print c, CNT[c]}' {} + | awk '{TOT += $2} 1; END { print "Total", TOT}'

# 13  
Old 12-04-2016
The second option. For the future: how about doing some tests and finding out by yourself?
# 14  
Old 12-04-2016
If your grep supports the P flag for Perl comparability regex then you can just make this little adjustment:

Code:
find . -name '*txt' -exec grep -PicH '(?<!"AC_STAT") 0 ' '{}' '+' | awk -F: '{ T += $2 } 1; END { print "TOTAL:" T }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find string in file and find the all records by string

Hello I would like to get know how to do this: I got a big file (about 1GB) and I need to find a string (for instance by grep ) and then find all records in this file based on a string. Thanks for advice. Martin (12 Replies)
Discussion started by: mape
12 Replies

2. UNIX for Advanced & Expert Users

couting occurences of a character inside a string and assigning it to a variable

echo "hello123" | tr -dc '' | wc -c using this command i can count the no of times a number from 0-9 occurs in the string "hello123" but how do i save this result inside a variable? if i do x= echo "hello123" | tr -dc '' | wc -c that does not work...plz suggest..thanks (3 Replies)
Discussion started by: arindamlive
3 Replies

3. Shell Programming and Scripting

Count occurences of a numeric string falling in a range

Dear all, I have numerous dat files (1.dat, 2.dat...) containing 500 numeric values each. I would like to count them, based on their range and obtain a histogram or a counter. INPUT: 1.dat 1.3 2.16 0.34 ...... 2.dat 1.54 0.94 3.13 ..... ... (3 Replies)
Discussion started by: chen.xiao.po
3 Replies

4. Shell Programming and Scripting

Count occurences of string

Hi, Please help me in finding the number of occurences of the string. Example: Apple, green, blue, Apple, Orange, green, blue are the strings can be even in the next line. The o/p should look as: Word Count ----- ----- Apple 2 green 2 Orange 1 blue 2 Thanks (2 Replies)
Discussion started by: acc888
2 Replies

5. Shell Programming and Scripting

delete last character in all occurences of string

Hello all, I have a file containing the following p1 q1 p2 q2 p1 p2 p3 pr1 pr2 pr1 pr2 pa1 pa2 I want to remove the last character from all strings that start with 'p' and end with '1'. In general, I do not know what is between the first part of the string and the last part of the string.... (4 Replies)
Discussion started by: bigfoot
4 Replies

6. Shell Programming and Scripting

sed to match only exact string only in all occurences

Dear Friends, Anybody knows how to match exact lines only in multilinear. Input file: apple orange orange apple apple orange Desired output: fruit orange apple fruit i used the command (1 Reply)
Discussion started by: vasanth.vadalur
1 Replies

7. Web Development

How to find all occurences of word?

Hi, For example lets consider i have word like this:cell I have some text that is stored in table. These are few sentences. TRAP also regulates translation of trpE by promoting formation of an cell. In addition initiation of pabA, trpP and ycbK by directly blocking cells. I... (0 Replies)
Discussion started by: vanitham
0 Replies

8. Shell Programming and Scripting

Need to replace all occurences of a search string using sed

All, Here is what I am searching for using sed. 1 00640000106798 I want to replace that with the following. 8 0064B0000106798 I can do this easy enough from the command line using sed but I need to put the search string in a file and then execute the sed command within a... (2 Replies)
Discussion started by: mjs3221
2 Replies

9. Shell Programming and Scripting

number of occurences of a string

hi, I have a file where i need to count the occurences of a string ex) 'welcome to unix forum'. can anybody help me out (12 Replies)
Discussion started by: siddu_chittari
12 Replies

10. UNIX for Advanced & Expert Users

How to count no of occurences of a character in a string in UNIX

i have a string like echo "a|b|c" . i want to count the | symbols in this string . how to do this .plz tell the command (11 Replies)
Discussion started by: kamesh83
11 Replies
Login or Register to Ask a Question