Ahmad
Ahmad

Reputation: 2190

find and then grep and then iterate through list of files

I have following script to replace text.

grep -l -r "originaltext" . |  
while read fname  
do  
sed 's/originaltext/replacementText/g' $fname > tmp.tmp  
mv tmp.tmp $fname  
done

Now in the first statement of this script , I want to do something like this.

find . -name '*.properties' -exec grep "originaltext" {} \;

How do I do that?
I work on AIX, So --include-file wouldn't work .

Upvotes: 0

Views: 1046

Answers (2)

fedorqui
fedorqui

Reputation: 290025

In general, I prefer to use find to FIND files rather than grep. It looks obvious : )

Using process substitution you can feed the while loop with the result of find:

while IFS= read -r fname  
do  
  sed 's/originaltext/replacementText/g' $fname > tmp.tmp  
  mv tmp.tmp $fname  
done < <(find . -name '*.properties' -exec grep -l "originaltext" {} \;)

Note I use grep -l (big L) so that grep just returns the name of the file matching the pattern.

Upvotes: 1

L&#230;rne
L&#230;rne

Reputation: 3142

You could go the other way round and give the list of '*.properties' files to grep. For example

grep -l "originaltext" `find -name '*.properties'`

Oh, and if you're on a recent linux distribution, there is an option in grep to achieve that without having to create that long list of files as argument

grep -l "originaltext" --include='*.properties' -r .

Upvotes: 1

Related Questions