I have several records in a database and I want to modify the contents with a good line using awk. I want to grab the field in each record, extract some fields and then put the fields I extracted back into the database.
Here is what I have so far. I'm looking at the requirements field in the pc_t table and just grabbing the 1st, 3rd, and 6th fields from the 2nd line of that output, then putting them back into the darkstar database.
Code:
mysql darkstar -e "select requirements from pc_t where game_id=1;" | awk 'NR==2{print "mysql darkstar -e \"update pc_t set requirements = "$1"|"$3"|"$6" where game_id=1;\""}' | bash
which produces(without piping to bash)
Code:
mysql darkstar -e "update pc_t set requirements = 90Mhz|32MB|200MB where game_id=1;"
However, I need to put single quotes around 90Mhz|32MB|200MB. So I tried putting single quotes and escaping them.
Code:
mysql darkstar -e "select requirements from pc_t where game_id=1;" | awk 'NR==2{print "mysql darkstar -e \"update pc_t set requirements = \'"$1"|"$3"|"$6"\' where game_id=1;\""}' | bash
but the terminal gives me the next line as if there's a quote that is not closed. How can I go about doing this?
In the end I would do something like this, where the game_id is from 1 to 58.
Code:
for i in `seq 1 58`; do mysql darkstar -e "select requirements from pc_t where game_id=$i;" | awk 'NR==2{print "mysql darkstar -e \"update pc_t set requirements = "$1"|"$3"|"$6" where game_id=$i;\""}' | bash; done