views:

32

answers:

2

How would I replace one pattern with another for every file with extension .cc and .h recursively? Not sure if I need to prevent it from going into .svn directories.

first attempt

#!/bin/bash
for file in `find . -name '*.cc' -or -name  '*.h'`; do \
    sed -e s/$1/$2/g -i temp $file
done
A: 

You can search and replace using regular expressions through certain files in Eclipse project explorer, for example. I'm sure there are lots of tools that can do this, but Elipse comes to mind first.

To do this you need to invoke the "Search" dialog, place your pattern in the search field, specify extensions with a wildcard and press "Replace",

Igor Zinov'yev
+2  A: 

If your project is managed under linux platform, You can do sth like that inside the bash:

for file find . -name '*.cpp' -or -name  '*.h'; do \
     cat $file | sed s/search/replace/g > tmp
     mv tmp $file
done

Each svn file has '*-base' extension so all of them will be unchanged. This script will only affect to *h and *cc files.

kogut
doesn't work. What am I doing wrong? #!/bin/bash for file find . -name '*.cc' -or -name '*.h'; do \ sed -i s/$1/$2/g $file done
Neil G
You forgot about semicolon after $file and before 'done' command.Semicolon is needed because your command is in one line.This is proper version: #!/bin/bash for file find . -name '*.cc' -or -name '*.h'; do \ sed -i s/$1/$2/g $file; done
kogut