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.

UPDATE
I've not kept my promise...though I did use awk today to make lower case files out of a list of annoyingly UPPER CASE files:

ls 
*.txt awk '{print "mv " $1 " " tolower($1)}' bash


UPDATE
Today's issue was a tough one. There were images named identically in pics/thumbs and pics/full. In order to import them using X-Cart's importer, all the images needed to be in a single folder. Copying in the fulls is easy (execute cp ../../pics/fulls/* ., from the destination folder).

Now, the thumbs are the problem. Using that same command would overwrite the identically named images.

AWK to the rescue! I've pulled in a few new goodies, split(), substr, and length. I'll try to find time to explain how exciting this one is, but for now, suffice it to say it worked great, even when some of these images had multiple periods in them.


ls 
-../../pics/thumbs/asp.jpg 
   
awk '{split($1,b,"/"); split(substr(b[5],length(b[5])-5),a,"."); 
   print "cp "$1" " substr(b[5],0,length(b[5])-6) ""a[1]"-t."a[2]}'


prints, for instance:
cp ../../pics/thumbs/Ae28-.02.3.rr.jpg Ae28-.02.3.rr-t.jpg
Pipe that to bash and you're ready to go!

And event though I said it earlier, be SURE to run this WITHOUT piping to bash to be sure you have it right. Then when things look good, THEN pipe it to bash so the commands are executed.

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


Last Updated: 2011-07-06 17:04:00
Creative Commons License Jay Johnston is a Zend Certified Engineer in PHP 5 LPI-ID: LPI000102450, Verfication Code: wmnnt56y2k  ... Jay Johnston is certified as LPIC-1 by the Linux Professional Institute