Sed find and replace in folder |
| Saturday, 23 February 2008 | ||||
|
Here's a handy one-liner to help you replace strings globally throughout all the files in a specified folder. This procedure is also called mass find and replace, or global replace. You can even use it on windows if you're willing to spent a few minutes installing cygwin , a windows port of the gnu/linux most used utilities (including the bash shell and even X11) The code below will replace every occurence of 'ugly' with 'beautiful'. Linux example: find /home/bruno/old-friends -type f -exec sed -i 's/ugly/beautiful/g' {} ;
Cygwin example (you have to use unix-style paths): find /cygdrive/c/xampp/joomla/ -type f -exec sed -i 's/ugly/beautiful/g' {} ;
/cygdrive/c/ stands for the root of your C windows drive letterAlso note that Cygwin requires you to use the forward slash delimiter instead of the windows backslash delimiter. The bash shell allows TAB completion for paths, just type the first few letters and hit TAB to auto-complete the path. If you want to filter, let's say, php files, you can add -name '*.php' to command-line arguments, just after the search path. find /cygdrive/c/xampp/joomla/ -type f -name '*.php' -exec sed -i 's/ugly/beautiful/g' {} ;
Sometimes you may need a more powerful regex engine, so here's how to use PERL to perform a global substitution: $ find . -type f -name '*.php' -exec perl -pi -e 's/struct\[(.+?)\]/struct(\1)/g' {} ;
The above command will search for .php files in the current(working) folder and replace struct[some text] with struct(sometext). This command uses the \1 backreference syntax to specify that the match found in the first set of paranthesis should be included in our replace pattern.
Related articles(beta):
Thanks for the info, Hi, If you're having trouble trying to replace strings that contain slashes or backslashes, you can always use pipe '|' as a string delimiter Write Comment |
||||
| Last Updated ( Wednesday, 07 July 2010 ) | ||||
Joomla stuff
Newsletter
Auto tags
find and sed
sed find
sed replace
find sed
sed search replace
sed search and replace
sed find and replace
cygwin sed escape
Sed global search and replace
sed replace semicolon
linux find and sed
cygwin search and replace
exec sed
sed directory
find sed exec
search and replace sed
cygwin sed replace
find and replace with sed
linux sed find
cygwin sed
find et sed
sed in cygwin







