FInd all string occurences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FInd all string occurences
# 1  
Old 12-02-2016
FInd all string occurences

Hi Folks -

I have a need to find a string occurrences in a list of *.txt files.

My code is as follows:

Code:
find . -name "*txt" -exec cat {} + | grep -ic " 0"

I was wondering a few things.

1. Is that an acceptable method to achieve my goal?
A. It works as expected but just curious
2. Is there a way to output the total # of found occurrences with a file name, then total everything up into 1 value?

Thank you all!
# 2  
Old 12-02-2016
Why run cat when you can run grep?

This will list counts from all files:

Code:
find . -name '*txt' -exec grep -icH " 0" '{}' '+'

This will add a TOTAL line to the end:

Code:
find . -name '*txt' -exec grep -icH " 0" '{}' '+' | awk -F: '{ T += $2 } 1; END { print "TOTAL:" T }'

Same thing, but excludes files with zero matches:
Code:
find . -name '*txt' -exec grep -icH " 0" '{}' '+' | awk -F: '{ T += $2 } $2+0; END { print "TOTAL:" T }'

grep -H forces grep to always print the related filename.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 12-02-2016
Coronaa -

Thank you so much! I will test now.

If I wanted to spool the results to a text file instead of print to screen, would I just simply do this:

Code:
find . -name '*txt' -exec grep -icH " 0" '{}' '+' | awk -F: '{ T += $2 } 1; END { echo "TOTAL:" T  >>results.txt }'

Thank you!
# 4  
Old 12-02-2016
No. There is no awk echo command. Try:
Code:
find . -name '*txt' -exec grep -icH " 0" '{}' '+' | awk -F: '{ T += $2 } 1; END { print "TOTAL:" T }' > results.txt

if you want to replace the contents of results.txt with the output from this run, or:
Code:
find . -name '*txt' -exec grep -icH " 0" '{}' '+' | awk -F: '{ T += $2 } 1; END { print "TOTAL:" T }' >> results.txt

if you want to append the output from this run to that file's previous contents.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-02-2016
Folks -

Thank you very much! It's working as expected!!
# 6  
Old 12-03-2016
Hi Guys -

I have one more request. I need to exclude or ignore a search string from my search.

The string is as follows:
Quote:
"AC_STAT" 0
I need to add it to the following code:

Code:
find . -name '*txt' -exec grep -icH " 0 " '{}' '+' | awk -F: '{ T += $2 } 1; END { print "TOTAL:" T }' >>TCP_FinSAP_20161203.txt

Thank you!
# 7  
Old 12-04-2016
Needs a slightly different approach - use awk in the first place instead of grep | awk:
Code:
find . -name "*.txt" -exec awk '{n=gsub (/ 0/, "&"); m=gsub (/"AC_STAT" 0/, "&"); CNT[FILENAME]+=n-m; TOT+=n-m} END {for (c in CNT) print c, CNT[c]; print "Total", TOT}' {} +

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