←back to thread

131 points tomschafer | 1 comments | | HN request time: 0.214s | source
1. sriram_malhar ◴[] No.42152307[source]
You get all this and more with a direct perl one liner. Without the interactivity. I'd argue that if it is a lot of files, interactivity would be a pain. Also, since the original is preserved as a .bak file, one can be fearless about trying

   #change xxx to yyy in all html
   bash> perl -pi.bak -e 's/xxx/yyy/g' *.html

   #change xxx10 (say) to yyy10 in all html
   bash> perl -pi.bak -e 's/xxx(\d+)/yyy$1/g' *.html

   # Change x4 to yyyy, where the number of y's equals to the number after x.
   bash> perl -pi.bak -e 's/x(\d+)/"y" x $1/ge' *.
The last example shows the /e operator, which evaluates an expression and uses the result as substitute, instead of a simple string.

And finally, to exclude files, one can use a subshell. For example, suppose you want to change all html, but exclude undesirable.html..

   perl -pi.bak -e 's/x/y/g'  $(ls *.html | egrep -v undesirable)