Find and Grep

Is it possible with find and Grep to search files under a directory and display only files that have multiple occurrence of a string (In AIX)? Anybody has an example code? If not what are the other options?

Thanks in advance.

Hi, let me make sure I understand your question?
You just want to find files that have the word aix in them?

String I want to find is "not found".. If "not found" string found twice in a file give me the file name.. What I meant was OS is AIX.

Not sure this works in AIX, but try

awk 'FNR==1 {c=0} /not found/ {++c} c==2 {print FILENAME; ++c}' *
Cd {directory path}
Ls -ltr >> test.txt
Cat test.txt | awk -F" " '{print $9}| while read fl
Count=`Grep  "not found" ${fl} | wc -l`
If [ ${Count}  -eq  2   ]
Then
Echo ${fl} having "not found" string ${Count} times
fi
done

---------- Post updated at 06:23 PM ---------- Previous update was at 06:23 PM ----------

The above script will help you

Commands in UNIX are case-sensitive, you can't just capitalize random things. Also, awk is missing a quote.

Also, that can be simplified a lot...
1) awk does not need cat's help to read a file.
2) awk's separator defaults to space, -F" " is redundant.
3) grep can count all by itself, via -c .
4) You don't need ls and awk to loop through a directory.
5) You especially don't need to ls -l -t then just throw away everything -l -t does for you.

ls -r -l -t | awk '{ print $9 }' | ... simplifies to ls -r | ... See man ls to learn what these options do.

6) If test.txt already existed, you'd be adding to it instead of replacing it, ending up with more filenames than you expected. Use > instead of >> to overwrite.

7) You don't need a temporary file. You can pipe into a loop as easily as anything else -- in fact you do so already, but didn't join the two.

8) find is often better than ls -r. You can tell it to find only files, for example (which is what -type f means.)

9) You don't need to run grep 12 times for 12 files. It's quite capable of counting more than one file.

Since "echo a b c d e | xargs cat" is equivalent to "cat a b c d e", we can feed find's list of files into xargs grep to grep many files at once.

It will print lines like filename:2 which we can read directly, telling the shell to split on : (The -H forces grep to always print the filename)

10) You only check for two and exactly two matches. You'd miss a file which had three. Try -ge for "greater than or equal to".

find /path/to/folder -type f | xargs grep -Hc "not found" | while IFS=":" read FILE C
do
        [ "$C" -ge 2 ] && echo "$FILE contains 'not found' $C times"
done

I don't think -H works that way in AIX it has something to do with the -r and -R options and symbolic links.

We could "trick" grep into printing the filename by giving it /dev/null first:

find /path/to/folder -type f | xargs grep -c "not found" /dev/null | while IFS=":" read FILE C
do
        [ "$C" -ge 2 ] && echo "$FILE contains 'not found' $C times"
done
1 Like

Another one:

find /path/to/folder -type f -exec awk 'FNR==1 {c=0} /not found/ && ++c==2 {print FILENAME}' {} +

I think only GNU awk could accelerate with {print FILENAME; nextfile}