jay johnston dot com
mootools php faith camera linux

linux

Awk One-liner for copying mysql tables



Thanks so much to Blake Dunlap for showing me some simple raw awk power when copying mysql tables from the backup machine became a hideous array of mv table.MYD (etc) and scp table.* and chmod table.* commands:


ls 
-1 table_one.* table_two.* | awk -F'{print "rsync -aP 
" $1 "." $2 " user@192.168.1.100:/mysql/mysql5/mydatabase/" 
$1 "_old."$2}' 
bash


(note that the line breaks should be removed as this IS a single line command

So what you're seeing here is
  1. We list out the tables we need moved from the backup machine using ls -1 table_one.* table_two.*
  2. That output is sent via a pipe | to awk
  3. awk is splitting on a "dot" . (as evidenced by the -F switch)
  4. The resulting splits are an array that is referenced by dollar 1 and dollar 2 $1 , $2
  5. Lastly, print causes an rsync command to execute when piped to bash: | bash


UPDATE
Knowledge is only good when one uses it, eh? So use it, I have!

Here is an example I just used to fix a migration boo-boo. I had accidentally migrated all the thumbs to <id>.jpg and migrated all the images to <id>t.jpg (in other words, I named them backwards). Poop. So, what I must accomplish is making all the .jpg images t.jpg images and vice-versa.

mkdir product_images_oops
;
cd product_images_oops;
mv ../product_images/*.jpg .;


There, now all my backwards-named images are in a temp directory. Let's awk them back into place:

ls 
*t.jpg awk -Ft '{print "mv " $1 "t" $2 "
 ../product_imgs/" $1 $2}' 
bash;
ls *.jpg awk -F'{print "mv " $1 "." $2 "
 ../product_imgs/" $1 "t." $2}' 
bash;


Man, sakes alive: awk rocks! Be sure to use awk examples like this often enough to make them second nature. I sure plan to.

(Once again, note that the line breaks should be removed as these ARE single line commands)

Last Updated: 2010-06-18 10:05:43

Creative Commons License Valid XHTML 1.0 Transitional Powered by PHP
Jay Johnston is a Zend Certified Engineer in PHP 5