Sponsored Content
Top Forums Shell Programming and Scripting awk command for INSERT statement Post 21096 by auswipe on Friday 10th of May 2002 11:41:08 AM
Old 05-10-2002
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"
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to convert unix command into Awk statement

Hi all, How can i use the below unix command in AWK . Can any one please suggest me how i can use. sed -e "s/which first.sh/which \$0/g" $shell > $shell.sal where $0=current program name(say current.sh) $shell=second.sh (1 Reply)
Discussion started by: krishna_gnv
1 Replies

2. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

3. Shell Programming and Scripting

How to insert numbers to a in between statement

Hi Guys, I want to create a shell script that will give me the output below. I want to insert the numbers from the input file to my url addresses below. And from the numbers below, I want to separate the last digit with a period (i.e. from 222222222222 to 22222222222.2). Appreciate any help.... (14 Replies)
Discussion started by: pinpe
14 Replies

4. Shell Programming and Scripting

awk command to insert a tag in XML

Hi All, I need a help on inserting a XML tag. Actual input <var> <nam>abcd</nam> <a1>.</a1> </var> if tag <a1>.</a1> is getting missed in XML like below <var> <nam>abcd</nam> </var> i need to insert wherever it is missed after <nam> tag and before </var> tag. Could anyone... (3 Replies)
Discussion started by: mohanalakshmi
3 Replies

5. Shell Programming and Scripting

Command in inside awk statement

Hello can you please help me with below script which is meant to delete clients from multiple netbackup policies I want to run a command insdie awk statement apparelnlty this script is not working for me for i in $( cat clients_list) do bppllist -byclient $i | awk... (6 Replies)
Discussion started by: Sara_84
6 Replies

6. Shell Programming and Scripting

How to add printf statement in awk command?

hi all i need to add the prinf statement in awk command for the converted comma separated output.... below is my code : Code Credits :RudiC awk -F, 'NF==2 {next} {ITM=$1 AMT=$2+0 CNT=$3+0 TOTA+=$2 ... (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

7. Shell Programming and Scripting

Insert field values in a record using awk command

Hi Friends, Below is my input file with "|" (pipe) as filed delimiter: My Input File: HDR|F1|F2||||F6|F7 I want to inser values in the record for field 4 and field 5. Expected output HDR|F1|F2||F4|F5|F6|F7 I am able to append the string to the end of the record, but not in between the... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

8. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

9. UNIX for Beginners Questions & Answers

Using df -g command with awk to get SQL statement

Hi Gurus... good day; currently I trying to run the df -g command with awk to get to convert in SQL statement, but I have some errors; df -g | awk '{print "This is the FileSystem: " $NF, " This is LV: "$1, "This is SIZE: "$2, "This is FREE: " $3, "This is the USED% "$4}' This on AIX... (3 Replies)
Discussion started by: wcastibl
3 Replies

10. Shell Programming and Scripting

Single quotes insert statement using awk

Hi, Need help, using awk command to insert statement awk -v q="'" '{ print "db2 connect to repolab > /dev/null; " "\n" "db2 -x \" select name from IBMPDQ.PROFILE where managed_database = " q $1"_"$3"__0" q "\"" } ' profile.txt | sh - | awk -v i="'" ' { print "db2 connect to repolab >... (1 Reply)
Discussion started by: Mathew_paul
1 Replies
INSERT(7)							   SQL Commands 							 INSERT(7)

NAME
INSERT - create new rows in a table SYNOPSIS
INSERT INTO table [ ( column [, ...] ) ] { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) | SELECT query } INPUTS table The name (optionally schema-qualified) of an existing table. column The name of a column in table. DEFAULT VALUES All columns will be filled by null values or by values specified when the table was created using DEFAULT clauses. expression A valid expression or value to assign to column. DEFAULT This column will be filled in by the column DEFAULT clause, or NULL if a default is not available. query A valid query. Refer to the SELECT statement for a further description of valid arguments. OUTPUTS INSERT oid 1 Message returned if only one row was inserted. oid is the numeric OID of the inserted row. INSERT 0 # Message returned if more than one rows were inserted. # is the number of rows inserted. DESCRIPTION
INSERT allows one to insert new rows into a table. One can insert a single row at a time or several rows as a result of a query. The col- umns in the target list may be listed in any order. Each column not present in the target list will be inserted using a default value, either a declared DEFAULT value or NULL. PostgreSQL will reject the new column if a NULL is inserted into a column declared NOT NULL. If the expression for each column is not of the correct data type, automatic type coercion will be attempted. You must have insert privilege to a table in order to append to it, as well as select privilege on any table specified in a WHERE clause. USAGE
Insert a single row into table films: INSERT INTO films VALUES ('UA502','Bananas',105,'1971-07-13','Comedy',INTERVAL '82 minute'); In this second example the last column len is omitted and therefore it will have the default value of NULL: INSERT INTO films (code, title, did, date_prod, kind) VALUES ('T_601', 'Yojimbo', 106, DATE '1961-06-16', 'Drama'); In the third example, we use the DEFAULT values for the date columns rather than specifying an entry. INSERT INTO films VALUES ('UA502','Bananas',105,DEFAULT,'Comedy',INTERVAL '82 minute'); INSERT INTO films (code, title, did, date_prod, kind) VALUES ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama'); Insert a single row into table distributors; note that only column name is specified, so the omitted column did will be assigned its default value: INSERT INTO distributors (name) VALUES ('British Lion'); Insert several rows into table films from table tmp: INSERT INTO films SELECT * FROM tmp; Insert into arrays (refer to the PostgreSQL User's Guide for further information about arrays): -- Create an empty 3x3 gameboard for noughts-and-crosses -- (all of these queries create the same board attribute) INSERT INTO tictactoe (game, board[1:3][1:3]) VALUES (1,'{{"","",""},{},{"",""}}'); INSERT INTO tictactoe (game, board[3][3]) VALUES (2,'{}'); INSERT INTO tictactoe (game, board) VALUES (3,'{{,,},{,,},{,,}}'); COMPATIBILITY
SQL92 INSERT is fully compatible with SQL92. Possible limitations in features of the query clause are documented for SELECT [select(7)]. SQL - Language Statements 2002-11-22 INSERT(7)
All times are GMT -4. The time now is 12:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy