1748112 Members
3481 Online
108758 Solutions
New Discussion юеВ

How could I do this?

 
SOLVED
Go to solution
Rob Johnson_3
Regular Advisor

How could I do this?

I want to do a find in several directories for all files with a .cfg extension that don't contain several strings of text and return the names of those files. I'm in the brain storming mode here and looking for ideas. Oh yea, I'm terrible when it comes to scripting.

example strings to search for:
'string 1 with spaces'
'string 2 with spaces'
'string 3 with spaces'
'string 4 with spaces'
'string 5 with spaces'
'string 6 with spaces'
5 REPLIES 5
D Block 2
Respected Contributor
Solution

Re: How could I do this?

find in dirA and /foo/dirB files ending with ..cfg:


$ find /dirA /foo/dirB -name '*.cfg' -print


now find string: abc followed by any spaces

$ find /dirA -name '*.cfg' -print | \
egrep 'abc * |string2 *|string3 *'

now filter out anything I don't care for using egrep -v


$ find /dirA -name '*.cfg' -print | \
egrep 'abc * |string2 *|string3 *' \
egrep -v 'aa|bb|cc'






Golf is a Good Walk Spoiled, Mark Twain.
Vibhor Kumar Agarwal
Esteemed Contributor

Re: How could I do this?

Small modification:

egrep -vl "string1|string2" `find directories -name *.cfg -type f`

The -l option will list the filenames.
Vibhor Kumar Agarwal
Muthukumar_5
Honored Contributor

Re: How could I do this?

You can use find + xargs or find with -exec to do this easily as,

find -type f -name "*.cfg" | xargs grep -El 'string *|string1 *|string2 *'

or

find -type f -name "*.cfg" -exec grep -El 'string *|string1 *|string2 *' {} \+

hth.
Easy to suggest when don't know about the problem!
Arturo Galbiati
Esteemed Contributor

Re: How could I do this?

Hi,
cat>/tmp/string_to_search<
Nguyen Anh Tien
Honored Contributor

Re: How could I do this?

I like to do like this
find -type f -name "*.cfg" | xargs grep -El 'string *|string1 *|string2 *'

you can know more by
#man xargs
Some things like Muthukumar
HTH
HP is simple