
I often need to create sql queries or updates based on data from flat files. For example, given the text file emails.txt that contain email addresses, I want to issue multiple select statements to look up the first and last name of the person identified by the email address.
Assume the emails.txt's content is:
friend1@friendlyemaildotcom friend2@friendlieremaildotnet relative@familyemaildotnet
And the database table, friends, looks like:
+-----------------------------------------------+ | email | first_name | last_name | +-----------------------------------------------+ | friend1@friendlyemaildotcom | joe | friend | | friend2@friendlieremaildotnet | mae | buddy | | relative@familyemaildotnet | aunt | marge | +-----------------------------------------------+
The following command line call with xargs will produce a separate sql select statement for each line in emails.txt:
$awk '{print $1;}' emails.txt | xargs -i echo "select first_name, last_name from friends where email='{}';"
The result can then be piped into mysql:
$awk '{print $1;}' emails.txt | xargs -i echo "select first_name, last_name from friends where email='{}';" | mysql -u me -p mystuff
© 2010–2011 Crumpeta Consulting LLC.