viernes, 15 de agosto de 2008

Sed tip: How to edit multiple files on several directories in less than a second

This is a response to:
http://tinyurl.com/6rfv8v

Recently a friend had to edit a huge amount of files (239 in total) scattered in a bunch of directories and subdirectories.

He published an easy way to do, very explicit. Everybody can do it. So, it is a cool solution for a random Joe.

But, he says... I have no time to learn sed or awk to see how to do that.

So, here is the solution with sed:
find . -type f | while read file; do sed -i 's/kiss mario/kiss maria/g' $file; done

This can be useful to change variable names to something more meaningful for example.

The awk version is a bit more complicated and need extra storage:
find . -type f | while read file; do awk '{gsub("kiss mario","kiss maria")}9' $file > $file.$$; mv $file.$$ $file; done

Have fun,
LJ

PD Graciously adapted from:
http://objectmix.com/awk/26496-find-replace-word-multiple-files-find-awk.html