The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
input text into file cannonfodder Shell Programming and Scripting 1 12-15-2007 10:37 AM
regex to delete multiple blank lines in a file? fedora Shell Programming and Scripting 6 10-11-2007 05:36 PM
Apply Regex to input read bonekrusher UNIX for Dummies Questions & Answers 3 09-23-2007 03:45 PM
sed, grep, awk, regex -- extracting a matched substring from a file/string ropers Shell Programming and Scripting 2 05-23-2006 02:56 PM
how to get input from file ajaya Shell Programming and Scripting 1 04-05-2006 03:02 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-02-2008
primal primal is offline
Registered User
  
 

Join Date: Oct 2001
Location: Toronto, Ontario, Canada
Posts: 34
Question SED + Regex + SQL Input file

Here's the problem...

I have a mysqldump file and I need to put single quotes around the date/time timestamp. So for example I have a line like:


Code:
INSERT INTO attachments VALUES (1,182,2004-08-06 09:24:04,'description'...

and I need it to become


Code:
INSERT INTO attachments VALUES (1,182,'2004-08-06 09:24:04','description'...

I tried
Code:
cat backup.sql | sed s/\,200/,\'200/ | sed s/..:..:../\&\'/ | less

and it did what I wanted, however it would also add a single quote infront of the first occurance of 200 so eventually it would insert
Code:
('2005,20041,2003-05-06 10:13:12',...

So then I attempted


Code:
cat backup.sql | sed s/.*\,.*\,/\&\'/ | sed s/..:..:../\&\'/ | less

but it seems to only add the single quote after the time. I can't figure out how to get the single quote before the date as well.

Anyone able to provide assistance?

Thanks!
Primal
  #2 (permalink)  
Old 04-02-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
Try this:


Code:
awk -F, '/INSERT INTO attachments VALUES/{$3="\047"$3"\047"}1' backup.sql

Regards
  #3 (permalink)  
Old 04-02-2008
unilover unilover is offline
Registered User
  
 

Join Date: Mar 2008
Location: Toronto, Canada
Posts: 66
Try:

Code:
sed "s/,\(200[0-9]-..-.. ..:..:..\)/,'\1'/" mysqldump

  #4 (permalink)  
Old 04-03-2008
primal primal is offline
Registered User
  
 

Join Date: Oct 2001
Location: Toronto, Ontario, Canada
Posts: 34
@unilover
When I run your command it returns:
Code:
\1 not defined in the RE

@Franklin52
When I run your command it places the single quotes around the dates but it removed the commas
Code:
...VALUES (1 182 '2004-...

I was able to get it with this
Code:
sed s/VALUES\ \([0-9]*\,[0-9]*\,/\&\'/ | sed s/..:..:../\&\'/

but then I realized that the timestamp also appears in other tables (different fields and more than once)

This is where I am right now

Code:
sed s/....-..-..\ ..:..:../\'&\'/g

The only problem with that is some timestamps already have single quotes around them and now after this line, some timestamps get 2 singles quotes surrounding them ''2004-08-06 09:24:04''

Is there anyway to do it so that it skips any timestamps that already have quotes?

I can do
Code:
s/,....-..-..\ ..:..:../\'&\'/g

but that would place the single quote ',2004-08-06 09:24:04' Is it possible to include the comma in the search and then place the single quote right after it?
  #5 (permalink)  
Old 04-03-2008
unilover unilover is offline
Registered User
  
 

Join Date: Mar 2008
Location: Toronto, Canada
Posts: 66
Maybe if you append the following to your command-line:

Code:
|sed -e "s/''2/'2/g" -e "s/'',/',/g"

  #6 (permalink)  
Old 04-03-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348

Code:
awk 'BEGIN{FS=OFS=","}/INSERT INTO attachmentsVALUES/{$3="\047"$3"\047"}1' backup.sql

Regards
  #7 (permalink)  
Old 04-03-2008
primal primal is offline
Registered User
  
 

Join Date: Oct 2001
Location: Toronto, Ontario, Canada
Posts: 34
@Franklin52
Can you explain your awk command? What does the 1 at the end mean?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0