SED + Regex + SQL Input file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED + Regex + SQL Input file
# 1  
Old 04-02-2008
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  
Old 04-02-2008
Try this:

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

Regards
# 3  
Old 04-02-2008
Try:
Code:
sed "s/,\(200[0-9]-..-.. ..:..:..\)/,'\1'/" mysqldump

# 4  
Old 04-03-2008
@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  
Old 04-03-2008
Maybe if you append the following to your command-line:
Code:
|sed -e "s/''2/'2/g" -e "s/'',/',/g"

# 6  
Old 04-03-2008
Code:
awk 'BEGIN{FS=OFS=","}/INSERT INTO attachmentsVALUES/{$3="\047"$3"\047"}1' backup.sql

Regards
# 7  
Old 04-03-2008
@Franklin52
Can you explain your awk command? What does the 1 at the end mean?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regarding file input to SQL from command line

Hi friends, Need your help again to get a best approach for the below scenario. I am previously having one shell script which accepts request_id/s as the command line argument. single req_id arg= 1111 Multiple arg= 1111,2222,3333 which gets passed to the embedded sql inside to... (9 Replies)
Discussion started by: Showdown
9 Replies

2. Shell Programming and Scripting

Read input file and used it to SQL query

Hi All, Seeking for your assistance to read each line $1 and $2 of input file and used it to query. Ex. file1.txt(number range) 9064500000 9064599999 9064600000 9064699999 9064700000 9064799999 Database name: ranges_log a_no message 9064500001 test 9064700000 ... (7 Replies)
Discussion started by: znesotomayor
7 Replies

3. Shell Programming and Scripting

Need help to run sql query from a script..which takes input from a file

I need to run sql script from shell script which takes the input from a file and contents of file will be like : 12345 34567 78657 and query will be like : select seq_nbr from bus_event where event_nbr='12345'; select seq_nbr from bus_event where event_nbr='34567'; select seq_nbr... (1 Reply)
Discussion started by: rkrish
1 Replies

4. UNIX for Dummies Questions & Answers

SQL Script to use variable value from input file

Here is the requirement, When I run the "run file KSH (sql)", it should substitute '${pCW_Bgn_DT}' with 201120 and '${pCW_End_DT}' with 201124 Input File ---------- $ cat prevwk.dat 201124 20110711 run file KSH (sql) ------------------ In this file, I want to use the variables... (1 Reply)
Discussion started by: shanrice
1 Replies

5. Shell Programming and Scripting

sed parse a lign into a new sql file

Hi everyone, another question while using sed. my sed statement should parse every line in a file and store all "i" variable item a a new file. any wrong arguments here? Thanks a million. task_name => name, object_type => 'TABLE', attr1 => 'TestR3', attr2 => '$i', for i... (4 Replies)
Discussion started by: sundaygeek
4 Replies

6. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

7. Shell Programming and Scripting

How to use sql data file in unix csv file as input to an sql query from shell

Hi , I used the below script to get the sql data into csv file using unix scripting. I m getting the output into an output file but the output file is not displayed in a separe columns . #!/bin/ksh export FILE_PATH=/maav/home/xyz/abc/ rm $FILE_PATH/sample.csv sqlplus -s... (2 Replies)
Discussion started by: Nareshp
2 Replies

8. Shell Programming and Scripting

how to use data in unix text file as input to an sql query from shell

Hi, I have data in my text file something like this. adams robert ahmed gibbs I want to use this data line by line as input to an sql query which i run by connecting to an oracle database from shell. If you have code for similar scenario , please ehlp. I want the output of the sql query... (7 Replies)
Discussion started by: rdhanek
7 Replies

9. Shell Programming and Scripting

isql input file with multiple sql statements

I've got: isql -U $USERID -S $SERVER -D $DATABASE -i inputfile.sql -o outputfile.txt in inputfile I have: go sql#1 go sql#2 go sql#3 go I also tried without "go" and with";" instead which did not work SQL statements will work if I paste them directly into the script and use EOF ... (0 Replies)
Discussion started by: Cailet
0 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question