LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-25-2012, 05:39 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
How would I go about doing this...


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

Last edited by trist007; 02-25-2012 at 05:40 PM.
 
Old 02-25-2012, 11:08 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Reformatting the code for legibility ...

Original
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
Trying for single quotes
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
Analysis ...

In bash, anything (everything!) after the opening single quote is replicated verbatim up to the closing single quote so when the bash running the script (as opposed to the bash at the end of the pipeline) parses the awk command it
  1. Finds an opening single quote in awk 'NR==2
  2. Finds a closing single quote in = \'"$1
  3. Finds an opening single quote in \""}'
It would be possible to fix the quoting but multiple parsing passes like this are notoriously difficult to write and subsequently to maintain so how about (not tested):
Code:
values=$( mysql darkstar -e "select requirements from pc_t where game_id=1;" \
  | awk 'NR==2{print $1"|"$3"|"$6}' )
mysql darkstar -e "update pc_t set requirements = '$values' where game_id=1;"
 
Old 02-26-2012, 09:37 AM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
I see now, ok great fantastic.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration