On Wed, Mar 08, 2006 at 06:54:50PM +0800, Peter N Lewis wrote:
At 10:29 +0000 8/3/06, Prof A Olowofoyeku (The African Chief) wrote:
For example, if I want to change all occurrences of "Type foo = Object (bar)" to "Type Tfoo = Class (Tbar)" in all ".pas" and ".pp" files in a directory tree, is there an easy way to do it, using standard GNU tools? I could of course write a program to do it, but I figured there could be an easier way. Thanks.
There are many ways to do it. I do this regularly using BBEdit and it's trivial, but you can do it using perl -i -p -e 's/whatever/whateverelse/', in combination with either zsh's ** to get all directories, or find -exec.
find . -name "*.pas" -or -name "*.pp" -exec perl -i -p -e 's/Type (\w+) = Object ((\w+))/Type T\1 = Class (T\2)/' '{}' ';'
find . -name "*.pas" -or -name "*.pp" -print
will list all the .pas or .pp files
find . -name "*.pas" -or -name "*.pp" -exec /bin/echo '{}' ';'
will execute teh command between -exec and ';', substituting the filename for '{}'.
perl -i -p -e 'perl code' filename
will execute the perl code for each line in filename, doing whatever it says, and then automatically printing the result, and then replace the file without making a backup.
Or something like that, I'd hate to try to get all the quoting exactly right as it passes through the shell, find, possibly another shell, and then grep...
And be weary of any version control files getting caught up by the find (SVN files add extensions, so they should be ok in this case).
Heaven help you if you don't have good backups.
I don't know about the heaven thing, but perl can do backups on the fly:
perl -i.old -p -e 'perl code' filename
Emil
I am really not responsible for the consequences of such a replace!
Enjoy, Peter.