How can I escape the single quote symbol? 
Author Message
 How can I escape the single quote symbol?

Hi everyone,

I'm writing an awk script at work which generates a bunch of SQL commands
from a datafile.  Since Oracle likes its strings to be enclosed within
single quotes (' ') how can I escape these quotes within the printf command
so awk won't get confused.  for example, part of my awk script looks like :

printf" 'Test Config' );\n" ;

The script itself is generated successfully, except, the single quotes are
NOT output which leaves a problem when trying to run the generated file in
Oracle.

I've tried :

printf" \' Test Config \' );\n" ;

in a vain hope to escape the characters but to no avail.

Any help will be greatly appreciated.



Sun, 01 Dec 2002 03:00:00 GMT  
 How can I escape the single quote symbol?

Quote:

>Hi everyone,

>I'm writing an awk script at work which generates a bunch of SQL commands
>from a datafile.  Since Oracle likes its strings to be enclosed within
>single quotes (' ') how can I escape these quotes within the printf command
>so awk won't get confused.  for example, part of my awk script looks like :

>printf" 'Test Config' );\n" ;

>The script itself is generated successfully, except, the single quotes are
>NOT output which leaves a problem when trying to run the generated file in
>Oracle.

>I've tried :

>printf" \' Test Config \' );\n" ;

>in a vain hope to escape the characters but to no avail.

One easy method is to assign the single quote to a variable, and then
use that variable wherever you need it.

I personally like doing it this way:

echo a | awk -v sq="'" '{print sq $0 sq}'

outputs:

'a'

Alternatively, you could assign it in a BEGIN section:

echo a | awk 'BEGIN{sq=sprintf("%c",39)} {print sq $0 sq}'

which also outputs:

'a'

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 01 Dec 2002 03:00:00 GMT  
 How can I escape the single quote symbol?

% from a datafile.  Since Oracle likes its strings to be enclosed within
% single quotes (' ') how can I escape these quotes within the printf command
% so awk won't get confused.  for example, part of my awk script looks like :

You should understand that there is nothing special about single quotes
in awk. The problem is that you're putting your script on the command-line,
and single-quotes _are_ special to the shell. Here are some choices:

1. put the awk script in a file and include it with -f:
  awk -f bob.awk data in

If you have something like a shell script which has a single awk command,
you can make an executable awk script by putting this at the top:
 #!/usr/bin/awk -f
if you're on Solaris, use
 #!/usr/bin/nawk -f
or
 #!/usr/xpg4/awk -f

2. keep the script on the command-line, but{*filter*}around with quoting:
 awk 'BEGIN { printf "'"'"'Test Config'"'"');\n" }'
(it doesn't _have_ to be quite that bad, but it might as well be)
(also, I'd like to point out that I got that right the first time I
typed it)

3. use \ddd to represent single quotes:
 printf "\047Test Config\047);\n"
you can make this more readable by assigning it to a variable:
 BEGIN { qt = "\047" }
 printf qt "Test Config" qt ");\n"

If what you're doing is taking a file of data, turning it into a file
of sql statements, running the sql statements through sqlplus, then
deleting the intermediary file, you can make it a bit more seamless by
piping the output of awk straight into sqlplus. For instance, your
script could be something like this:
 #!/usr/bin/awk -f

 BEGIN { FS = ","
         cmd = "sqlplus u/p"
 }

 $1 == "I" { printf "insert into %s values ", $2 | cmd
    for (i = 3; i <= NF; i++)
      printf "%s'%s'", i == 3 ? "(" : ",", $i | cmd
    print ");" | cmd
    next
 }

 $1 == "U" { ... }
 ...
 END { close(cmd) }
--

Patrick TJ McPhee
East York  Canada



Mon, 02 Dec 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Escaping single quote?

2. entry level question: how to escape single quote?

3. single quote escaping

4. Escaping single quotes

5. phpmailer and escaped single quotes in mail body

6. single/double quote escape interpolation

7. replace single quote to double quote with an example

8. single-quoted string conversion to triple-quoted string

9. fgetcsv and escaped quotes

10. Variables escaping quotes

11. Using quotes in a print command without escaping

12. MySQL + SQL Statements + Quote escaping

 

 
Powered by phpBB® Forum Software