shell variable in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell variable in awk
# 8  
Old 08-20-2007
Hi ghostdog,

As far i know there is no design issue.For your understanding let me explain.

I have One input file SOURCEFILE,keyfields=1|2|3,delimiter=|
I have three lookupfiles JOINFILE1,JOINFILE2,JOINFILE3 which are having the keyfields 3,4,5 and delimiters :,;,| respectively

based on the above files,i have to make one script file.
the sample script file shows like this
SCRIPTFILE
-------------
update table crn_ras_disc_dtl a set a.a5=SOURCEFILE.2,a.a1=JOINFILE1.3,a.a2=JOINFILE3.5,a.a3=JOINFILE2.3,a.a4=JOINFILE1.2;

For each lookup file
Join source file and the lookupfile with the keyfields and the respective delimiters
Append the Outpufield(s) by referring the SCRIPTFILE and its field (Append $3 of JOINFILE1 with the at the last of the input file) at the last of input file.
Change the sample script file and change the position
[ Assume SOURCEFILE has 20 fields. Eg: update table crn_ras_disc_dtl a set a.a5=SOURCEFILE.2,a.a1=JOINFILE1.3,a.a2=JOINFILE3.5,a.a3=JOINFILE2.3,a.a4=JOINFILE1.2;
to be replaced as update table crn_ras_disc_dtl a set a.a5=SOURCEFILE.2,a.a1=SOURCEFILE.21,a.a2=JOINFILE3.5,a.a3=JOINFILE2.3,a.a4=JOINFILE1.2;
]
done

Run the SCRIPTFILE by referring the LAST JOIN and its position.

Please suggest me.
# 9  
Old 08-20-2007
cskumar to be honest, I didn't understand very well your last explanation. However, have you thinked about inserting all the informations in database's tables and do the joins inside the db? As I said before I didn't get the point of your problem very well and maybe this approach isn't possible but... I wished to share my thought Smilie
# 10  
Old 08-20-2007
hi
Quote:
Originally Posted by cskumar
Hi All,

Output
---------
update table crn_ras_disc_dtl a set a.a5=10,a.a1=KUMAR,a.a2=23,a.a3=MALE,a.a4=US;
update table crn_ras_disc_dtl a set a.a5=20,a.a1=RAJ,a.a2=24,a.a3=MALE,a.a4=AU;
some observations:

1) i think in all sql-dialects you must enclose a string in some kind of quotes
2) the result of executing such a sql-file will be the same as executing only the last update, perhaps you want to insert the data
3) if you want to insert data most database systems have tool to load text-files efficiently, e.g oracle has sqlldr

mfg guenter
# 11  
Old 08-20-2007
Quote:
Originally Posted by summer_cherry
Hi, try this one( i use solaris)

Code:
while read line
do
cat file2 | nawk  'BEGIN{FS="|"}
{
l="'"$line"'"
gsub(/\$1/,$1,l)
gsub(/\$5/,$5,l)
gsub(/\$4/,$4,l)
gsub(/\$3/,$3,l)
gsub(/\$2/,$2,l)
print l
}'
done < file1

probably shouldn't say this but, cat is useless here.
# 12  
Old 08-20-2007
Java

cskumar , can you please elaborate more on the question please ????

your last explanation with sample confused a lot.......Smilie
# 13  
Old 08-20-2007
Quote:
Originally Posted by cskumar
Hi All,

How can i use the file for printing in awk

file1
----------
update table crn_ras_disc_dtl a set a.a5=$1,a.a1=$2,a.a2=$3,a.a3=$4,a.a4=$5;

file2
--------
10|KUMAR|23|MALE|US
20|RAJ|24|MALE|AU

Output
---------
update table crn_ras_disc_dtl a set a.a5=10,a.a1=KUMAR,a.a2=23,a.a3=MALE,a.a4=US;
update table crn_ras_disc_dtl a set a.a5=20,a.a1=RAJ,a.a2=24,a.a3=MALE,a.a4=AU;
Perhaps this.

Code:
[/tmp]$ cat file1
update table crn_ras_disc_dtl a set a.a5=$1,a.a1=$2,a.a2=$3,a.a3=$4,a.a4=$5;
[vivarkey@/tmp]$ cat file2
10|KUMAR|23|MALE|US
20|RAJ|24|MALE|AU
[/tmp]$ cat try.sh
#! /bin/sh

FINAL="$(<file1)"

while read line
do
    set -- ${line//|/ }
    eval echo $FINAL
done < file2
[/tmp]$ ./try.sh
update table crn_ras_disc_dtl a set a.a5=10,a.a1=KUMAR,a.a2=23,a.a3=MALE,a.a4=US
update table crn_ras_disc_dtl a set a.a5=20,a.a1=RAJ,a.a2=24,a.a3=MALE,a.a4=AU
[/tmp]$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Shell variable expansion in awk

I want to split one file input.tab into two separate ones, odd lines to input_reads1.txt, even lines to input_reads2.txt for a serial of files with similar name pattern. Also I want to "match" input/output file names to keep consistency of file name: CSEL_02.0_input.tab CSEL_03.4_input.tab... (2 Replies)
Discussion started by: yifangt
2 Replies

3. Shell Programming and Scripting

Awk: How to get an awk variable out to the shell, using system() ?

I am reasonably capable with awk and its quirks, but not with shell weirdness. This has to be Bourne Shell for portability reasons. I have an awk program that is working just fine; it handles multiple input streams and produces several reports, based on the request (-v Variables). In addition... (3 Replies)
Discussion started by: DerekAsirvadem
3 Replies

4. Shell Programming and Scripting

How to use shell variable in awk?

How do you use a shell variable in awk? I am using Solaris 10 and don't have GNU products installed. File (transportation.txt) contents: car boat airplane snowmobile bicycle sled This awk statment works (prints from the car line down to bicycle awk '/car/,/bicycle/'... (8 Replies)
Discussion started by: thibodc
8 Replies

5. Shell Programming and Scripting

use shell variable in awk command

Trying to do something like this ls -lrt | awk '$9=="test5"' -rw-r--r-- 1 lrmq db2iadm1 381 Sep 20 21:56 test5 But now, I need to give a variable in place of test5. For example let's define x as test5 x=test5 ls -lrt | awk '$9=="$x"' This doesn't seem to be working. It doesn't take the... (4 Replies)
Discussion started by: blazer789
4 Replies

6. Shell Programming and Scripting

Shell variable in awk

I have 3 files and each contain m*n matrix. m = number of rows (horizontal lines) n = number of columns (entries in a particular line) What I wish to find is the sum of the 2nd number in the last row. Ex file1.dat 2 5 8 8 4 6 7 8 3 8 3 7 file2.dat 3 4 1 4 8 4 0 3 4 7 3 7 ... (3 Replies)
Discussion started by: dynamics
3 Replies

7. Shell Programming and Scripting

How to change awk and shell variable value?

In awk script, #!/bin/sh awk 'BEGIN{i=0;}{i=i+5;}END{print i}' in.txt vr=0; vr=$i; echo "$vr" How can i assign that value of i in $vr(variable) of shell script? (7 Replies)
Discussion started by: cola
7 Replies

8. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

9. Shell Programming and Scripting

Return Awk Variable to Shell

I'm a bit stuck in getting variable from awk to shell. I tried searching but most of them showing to assign to shell variable via.. VAR=`echo $line | awk -F: '{print $1}'` which is correct ofcourse My problem is multiple assignments of variable like this one. The above solution will give... (10 Replies)
Discussion started by: ryandegreat25
10 Replies

10. Shell Programming and Scripting

Shell variable with awk

line_no=6 echo 'Phone,' `awk 'NR==$line_no{print;exit}' <filename>` what is the error in this.. it says.. awk: Field $() is not correct. The input line number is 1. The file is <filename>. The source line number is 1. i want to print the data in the $line_no line of a certain... (2 Replies)
Discussion started by: St.Fartatric
2 Replies
Login or Register to Ask a Question