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
(sed) parsing insert statement column that crosses multiple lines jjordan Shell Programming and Scripting 3 10-09-2007 01:23 AM
unix command to insert double quotes berlin_germany Shell Programming and Scripting 2 01-17-2007 01:07 PM
Script does not execute Insert Statement Amruta Pitkar Shell Programming and Scripting 4 08-25-2006 12:14 AM
sql insert command abey Shell Programming and Scripting 2 05-19-2006 12:56 PM
Insert TAB in echo statement sunils27 Shell Programming and Scripting 5 08-26-2005 04:36 AM

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 05-10-2002
nattynatty nattynatty is offline
Registered User
  
 

Join Date: Apr 2002
Posts: 31
Unhappy awk command for INSERT statement

Hi,

I sometimes bulk upload data in oracle. The problem is that I sometimes get an INSERT statemnt like this:
INSERT INTO ALL_USER_HOTSPOT_DETAILS (USR_LOGIN,USR_LASTNAME,USR_FIRSTNAME,USR_EMAIL,
PROPERTYNR)
VALUES ('SABRDAG','D'AGOS','SABRINA','sabrina_d'agos@sheraton.com',70)

I would like to run an awk or sed command that would take care of the single quotes in the INSERT statement.
So the above string should be

INSERT INTO ALL_USER_HOTSPOT_DETAILS (USR_LOGIN,USR_LASTNAME,USR_FIRSTNAME,USR_EMAIL,PROPERTYNR)
VALUES ('SABRDAG','D''AGOS','SABRINA','sabrina_d''agos@sheraton.com',70);

I have tried awk but couldnt make it work. Any help would be appreciated.

Thanx
  #2 (permalink)  
Old 05-10-2002
auswipe's Avatar
auswipe auswipe is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Wide Awake Wylie, Texas
Posts: 535
Wow. That is a tough one.

I came up with a Perl solution, but would like to see a more elegant sed/awk solution as well.


Code:
#!/usr/bin/perl

# Auswipe - 10 May 2002
# Auswipe sez "No Guarantees!"
# Auswipe also sez "Test and Double Test! Auswipe is not responsible if anything gets franked!"

# Convert tic into double tic for contractions
# and single tic in names

open(QUERY, "query.txt") || die "$!";
open(NEWQUERY, ">query2.txt") || die "$!";

while ($inputLine = <QUERY>) {
  while ($inputLine =~ m/(\w+)'(\w+)/) {
    my $fore = $1;
    my $aft  = $2;
    $inputLine =~ s/$fore'$aft/$fore"$aft/g;
  };
  print NEWQUERY "$inputLine";
};

Sample usage:


Code:
FreeBSD:joeuser:/home/joeuser/sample $ ll
total 8
-rwxr-xr-x   1 joeuser  joeuser   519 May 10 10:35 perltic
-rw-r--r--   1 joeuser  joeuser   168 May 10 10:31 query.txt
FreeBSD:joeuser:/home/joeuser/sample $ ./perltic
FreeBSD:joeuser:/home/joeuser/sample $ ll
total 10
-rwxr-xr-x   1 joeuser  joeuser   519 May 10 10:35 perltic
-rw-r--r--   1 joeuser  joeuser   168 May 10 10:31 query.txt
-rw-r--r--   1 joeuser  joeuser   168 May 10 10:38 query2.txt
FreeBSD:joeuser:/home/joeuser/sample $ cat query2.txt
NSERT INTO ALL_USER_HOTSPOT_DETAILS (USR_LOGIN,USR_LASTNAME,USR_FIRSTNAME,USR_EMAIL,
PROPERTYNR)
VALUES ('SABRDAG','D"AGOS','SABRINA','sabrina_d"agos@sheraton.com',70)
FreeBSD:joeuser:/home/joeuser/sample $

Two stars. Joe Bob says "Check it out"
  #3 (permalink)  
Old 05-10-2002
nattynatty nattynatty is offline
Registered User
  
 

Join Date: Apr 2002
Posts: 31
Hi Auswipe,

I havent yet tested your code but the quotes in D''AGOS is to be two single quotes and not one double quote. The sample usage has one double quotes.

Natty
  #4 (permalink)  
Old 05-10-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
I'm not sure that I understand exactly was is going on here. But I think that you want any single quote surrounded by letters to become two single quotes. If that is correct (and it does seem crazy), try this...

sed "s/\([a-zA-Z]\)\'\([a-zA-Z]\)/\1\'\'\2/g"
  #5 (permalink)  
Old 05-10-2002
auswipe's Avatar
auswipe auswipe is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Wide Awake Wylie, Texas
Posts: 535
Quote:
Originally posted by Perderabo
I'm not sure that I understand exactly was is going on here. But I think that you want any single quote surrounded by letters to become two single quotes. If that is correct (and it does seem crazy), try this...

sed "s/\([a-zA-Z]\)\'\([a-zA-Z]\)/\1\'\'\2/g"
That's what I was doing wrong with my sed statement. I was attempting to reference the atoms that I pulled with $1 and $2 like I did in the Perl code. I needed to use \1 and \2. D'oh.

Replacing the single tic with two single tics isn't crazy at all. After the insert takes place, the two single tics are converted into a single tic. It's the same for MS SQL Server as well.

The Perl code above can be fixed by replacing double quote with two single quotes back to back, but the sed expression is much cleaner.
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 08:36 PM.


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