BULK STRING REPLACEMENT ON YOUR SERVER

WEDNESDAY, MARCH 3 2004 @ 08:56 PM

This is not directly related to Flash, but maybe useful to somebody. I don't know how many of you use self-managed servers, as I don't know how many of you use Linux as your server platform. But if you happen to do both, and your Linux skills are still intermediate, maybe this can be of help to you. However, if you are a Linux shark, maybe you can show us a better way to accomplish this.

So, this afternoon I needed to change some info in in 200+ "legacy" XML files in one of my client's Linux servers. Given the size of the files, the number of occurrences of the information to be changed, and some encoding issues, attempting to download all those files and process them locally was out of question, as it was changing the information manually. My only option was to find a way to make the replacement in the server.

After half and hour of looking around and examining man files, I came up with this little script that will replace a text string, on every file inside a directory:

find /path/to/directory/ -type f -exec \
sh -c 'gawk "$0" "$1" > "$1.tmp"; mv "$1.tmp" "$1"' \
'{gsub("old_string","new_string"); print $0}' '{}' \;


//1<\/div> will just allow us to loop through the list of files in the selected directory. The trick is actually being done by the combination of
//1<\/div> and
//1<\/div>, GNU versions of the
//1<\/div> and
//1<\/div> commands which allow us to match string patterns in files, and replace strings, respectively. If you want to exclude child directories from the string replacement to, you can specify how deep the script should go, using the
//1<\/div> option for
//1<\/div>:

find /path/to/directory/ -type f -maxdepth 1 -exec \
sh -c 'gawk "$0" "$1" > "$1.tmp"; mv "$1.tmp" "$1"' \
'{gsub("old_string","new_string"); print $0}' '{}' \;


Voila! It only took 30 minutes of research + testing. I wonder how much would have taken to make the modifications otherwise....

Archived under: Miscelanea. | Permalink | google | del.icio.us Is it delicious? | digg Do you digg it?