Categories

Albums

Shots

Pictures of things possibly worth looking at.
12 June 2012
1 images in album
2012_06_13/P6123751.jpg

Emblems

Powered By Jaws Project
Powered By Apache HTTP Server
Powered By MySQL
Supports RSS
Powered By RSS Lounge
















Search and Replace on all rows in a MySQL table using bash

While wrestling with a software upgrade to the LAMP application running this website, I wanted to test a fix for an issue I was experiencing. I would have thought this could be accomplished in straight MySQL, but a few searches including the official docs on the regexp and replace functions suggest it is not. When the best advice I could find was to do it in perl. Rather than bust out the old DBI routines, I decided to just use my shell.


[code='Bash']
#!/bin/bash
# migrate-maps.sh - Search and Replace on all rows in a MySQL table using bash
# I thought this would fix a bug but it does not
# see http://dev.jaws-project.com/ticket/1228

DBNAME=jaws26a_upgrade


COUNT=`mysql -Ne "SELECT max(id) from $DBNAME.jaws_url_maps;"`;

for i in `seq 1 $COUNT` ; do
OLD=`mysql -Ne "SELECT map from $DBNAME.jaws_url_maps WHERE id = $i;"`;
if [[ $OLD == "0" ]] ; then
continue;
fi
NEW='{site_url}/'$OLD;
mysql -Ne "UPDATE $DBNAME.jaws_url_maps SET map=\"$NEW\" WHERE id=$i;";

[/code]
Trackback URI: http://www.26a.net/index.php/trackback/18

Leave a Comment



Write the captcha code you are seeing.

Comment XML feeds: RSS | Atom