Insert query with shell variable with AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert query with shell variable with AWK
# 1  
Old 10-15-2012
Question Insert query with shell variable with AWK

Hi,
I'm a first timer with Unix so pardon my ignorance.
I'm trying to read a comma separated file from the same folder where the script is and insert the value in a DB2 table.
I'm using AWK for the same. I'm getting `)' not expected error.
I'm not sure but for me it doesn't look like detailed error message. Smilie

The input file looks like this:

Code:
value1,value2,value3

The script is like this

Code:
awk '{
        nbr=split($2,arr,",");
        for(ix=1;ix<=nbr;ix++) 
        product_code=`echo 'print arr[ix]'` 
        db2 "INSERT INTO TABLE_NAME (PRODUCT_CODE) values (\'${product_code}\') 
where NOT EXISTS 
(select * from PRICING_PRODUCT_CODES where PRODUCT_CODE = \'${product_code}\');"
    }' CODES.txt

Moderator's Comments:
Mod Comment edit by bakunin: Please!! - view this code tag video for how to use code tags when posting code and data. Using all sorts of fancy fonts is no good replacement for this.

Last edited by bakunin; 10-15-2012 at 11:02 AM..
# 2  
Old 10-15-2012
Try:
Code:
#gawk '{print "db2 \"INSERT INTO TABLE_NAME (PRODUCT_CODE) values ("$1") etc..\""}'  RS=',|\n' infile                               
db2 "INSERT INTO TABLE_NAME (PRODUCT_CODE) values (value1) etc.."
db2 "INSERT INTO TABLE_NAME (PRODUCT_CODE) values (value2) etc.."
db2 "INSERT INTO TABLE_NAME (PRODUCT_CODE) values (value3) etc.."
db2 "INSERT INTO TABLE_NAME (PRODUCT_CODE) values (value4) etc.."
db2 "INSERT INTO TABLE_NAME (PRODUCT_CODE) values (value5) etc.."
db2 "INSERT INTO TABLE_NAME (PRODUCT_CODE) values (value6) etc.."


Last edited by Klashxx; 10-15-2012 at 11:13 AM.. Reason: Gawk
# 3  
Old 10-15-2012
Tried but with no luck

I changed the query to this

Code:
awk '{print "db2 \"INSERT INTO TABLE_NAME (PRODUCT_CODE) values ("$1",) where NOT EXISTS (select * from TABLE_NAME where PRODUCT_CODE=("$1")\""}'  RS=',' PRICING_PRODUCT_CODES.txt

And got the below error

Code:
 The error context is
        {print "db2 \"INSERT INTO PRICING_PRODUCT_CODES (PRODUCT_CODE, NETWORK_NAME, ACTIVE) values >>>  ("$1",some <<< 
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
 awk: 0602-512 The string ,somevalue cannot contain a newline character. The source line is 1.
    awk: 0602-540 There is a missing ) character.

# 4  
Old 10-15-2012
Try the gawk version.
# 5  
Old 10-15-2012
Data Newbie to Unix

I tried replacing 'awk' with 'gawk' and I get,
Code:
gawk:  not found.

Could you please tell me what is wrong with the script I had written the first time.
Code:
awk '{         nbr=split($2,arr,",");         for(ix=1;ix<=nbr;ix++)          product_code=`echo 'print arr[ix]'`          db2 "INSERT INTO TABLE_NAME (PRODUCT_CODE) values (\'${product_code}\')  where NOT EXISTS  (select * from TABLE_NAME where PRODUCT_CODE = \'${product_code}\');"     }' CODES.txt

Thanks in advance!Smilie

Last edited by Kabira Speaking; 10-15-2012 at 11:34 AM.. Reason: Code block is missing
# 6  
Old 10-15-2012
Should be something like this:
Code:
awk '{         
nbr=split($0,arr,",")
for(ix=1;ix<=nbr;ix++){
   print "db2 \"INSERT INTO TABLE_NAME (PRODUCT_CODE) values ("arr[ix]") " 
   print "where NOT EXISTS  (select * from TABLE_NAME where PRODUCT_CODE = "arr[ix]");\"" 
   } infile

# 7  
Old 10-15-2012
Data Back to Square One

Thanks Klashxx for instant replies.

I changed the script to look like this.

Code:
awk '{         
        nbr=split($0,arr,",")
        for(ix=1;ix<=nbr;ix++){
        print "db2 \"INSERT INTO PRICING_PRODUCT_CODES (PRODUCT_CODE, NETWORK_NAME, ACTIVE) values (\'"arr[ix]"\',\'some value\',1) " 
        print "where NOT EXISTS (select * from TABLE_NAME where PRODUCT_CODE = \'"arr[ix]"\');\"" 
    }' PRICING_PRODUCT_CODES.txt

And I got this error
Code:
0403-057 Syntax error at line 33 : `)' is not expected.

Basically, I want insert queries like this

Code:
Insert into table(col1,col2) values('valuefromfile1','staticvalue1') where ....;
Insert into table(col1,col2) values('valuefromfile2','staticvalue2') where ....;


Last edited by Kabira Speaking; 10-15-2012 at 12:05 PM.. Reason: Forgot to thank Klashxx
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing value of variable to a query within shell script

I have a script in which i connect to database to run a query and get the result of the query to a temp file. This works fine , now what i want is there is flat file which contains the value to be used in the query. I want to read this file line by line and then run the query for each value in that... (7 Replies)
Discussion started by: gpk_newbie
7 Replies

2. Shell Programming and Scripting

[awk] - how to insert an external variable

I want to incorporate the variable in the for statement as a column of my processed file. In the INCORRECT example below, it is $i which corresponds to the i in my for loop: for i in x86_64 i686; do awk '{ print $1" "$4" "$5" "$i }'awk $file-$i > processed-$i.log doneThanks! (3 Replies)
Discussion started by: graysky
3 Replies

3. UNIX for Dummies Questions & Answers

insert variable into awk

I'm trying to insert a filename into awk. filename="12345.wmv" I have tried this: awk '/$filename/{print $0}' infile and this: awk -v fn=$filename '/$fn/{print $0}' infile How do I insert the variable into awk? (1 Reply)
Discussion started by: locoroco
1 Replies

4. Shell Programming and Scripting

Using a shell script variable in a mysql query using 'LIKE'

Hello. I am writing a simple script that reads a text file and removes records from a mysql database. The items in the text file are of the format: firstname.middle.lastXXX, where XXX is a 3 digit number. The table has an email field that will match the firstname.middle.last. So, I thought I... (1 Reply)
Discussion started by: bricoleur
1 Replies

5. Shell Programming and Scripting

Forming an insert query using awk

Hi, I'm trying to form an insert sql query using shell programming. I have table named company with four columns 'company name', 'company id', 'company code' and 'last change id' I have to read the company name, company code and last change id from a file delimited by | which has around 10... (4 Replies)
Discussion started by: rakesh_s
4 Replies

6. Shell Programming and Scripting

How to use a variable in insert query?

My script contains as follows, VALUE=`sqlplus un/pwd <<EOF > OB.txt set pagesize 0 feedback off verify off heading off echo off select max(1) from table1; exit; EOF` insert into table2 values(1, 'The max value is $value',...); i need the value of VALUE to be inserted after 'The max... (2 Replies)
Discussion started by: savithavijay
2 Replies

7. Shell Programming and Scripting

Insert external variable in a AWK pattern

Dear all, ¿How can i insert a variable in a AWK pattern? I have almost succeeded in solving a puzzle with AWK but now i want to make a script. Let me explain. cat file.txt | awk 'BEGIN {RS="\\n\\n"} /tux/ { print "\n"$0 }' I know that this command makes right what i want to do, but mi... (8 Replies)
Discussion started by: antuan
8 Replies

8. Shell Programming and Scripting

Passing a variable from shell script to mysql query?

I heard this was possible but from my research I haven't been able to figure it out yet. Seems it should be simple enough. Basically from a high level view I'm trying to accomplish... . $X='grep foo blah.log' then 'mysql command SELECT foo FROM bar WHERE ' . $X or something like that. ... (2 Replies)
Discussion started by: kero
2 Replies

9. Shell Programming and Scripting

Setting variable for query using iSql / Korn Shell

Hi All- First time using iSql. I have 4 query files - some have more than 1 line of sql statements After a bit of research it appears I can just use the -i command and specify the input file. Questions: Does it matter that there are multiple queries in each file? Do I need to have... (3 Replies)
Discussion started by: Cailet
3 Replies

10. Shell Programming and Scripting

awk problem when attributing query to a variable

Hello, I'm trying to make a script which reads the PID of a process using awk. I've read the thread in this forum where it is explained. However, the problem i'm having is when attributing that PID value to a variable. I'm using AIX 5.3. The command is the following: :/home/user1>ps -ef |... (2 Replies)
Discussion started by: mambo
2 Replies
Login or Register to Ask a Question