I have few files in a directory containing below pattern:
Simulator tool completed simulation at 20:07:18 on 09/28/18.
The situation of the simulation: STATUS PASSED
Now I want to count the number of files which contains both of strings completed simulation
& STATUS PASSED
anywhere in the file.
This command is working to search for one string STATUS PASSED
and count file numbers:
find /directory_path/*.txt -type f -exec grep -l "STATUS PASSED" {} + | wc -l
Sed is also giving 0 as a result:
find /directory_path/*.txt -type f -exec sed -e '/STATUS PASSED/!d' -e '/completed simulation/!d' {} + | wc -l
Any help/suggestion will be much appriciated!
The command
find /directory_path/*.txt
just lists all txt files in/directory_path/
not including subdirectories of/directory_path
If you ensure no special characters in the filenames
I don't have AIX to test it, but it should be POSIX compliant.
I'm printing the matching file names in case that's useful in some other context but piping that to wc will fail if the file names contain newlines - if that's the case just print 1 or anything else from awk.
Since
find /directory_path/*.txt -type f
is the same as justls /directory_path/*.txt
if all of the ".txt"s are files, though, it sounds like all you actually need is (using GNU awk fornextfile
):or with any awk:
Those will work no matter what characters are in your file names.
Using
grep
and standard utils:grep -m1
stops when it finds the first match. This saves time if it's a big file. If the list of matches is large,sort -t: -k1
would be better thansort
.