0

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 .

Ahmad
  • 2,110
  • 5
  • 26
  • 36

2 Answers2

1

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 .
Lærne
  • 3,010
  • 1
  • 22
  • 33
  • first one not working and as I already mentioned second option is not available to me. Thanks anyway for your answer – Ahmad Nov 24 '15 at 10:57
  • Oops, didn't read the last line, sorry. Well read fedorqui's answer. – Lærne Nov 24 '15 at 10:59
1

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.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Hey fedorqui, looks like that filer "*.properties" doesn't look to be working. If I echo fname in the loop, I don't see properties file – Ahmad Nov 24 '15 at 11:26
  • 1
    @Ahmad I did not really follow what is your purpose. Since you use `find ... -exec grep`, you may want to say `grep -l` (that is, lowercase `L`) so that it just provides the name of the file as output. – fedorqui Nov 24 '15 at 11:29