Passing KSH variable to AWK with two loops


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Passing KSH variable to AWK with two loops
# 1  
Old 06-18-2009
Passing KSH variable to AWK with two loops

Hi, I need some help to figure out why an outer for loop KSH variable does not decode in AWK but inner for loop does. Below is my code,
If I hard code variable 'SUBSEQ' in AWK it works but if I try to pass the SUBSEQ from KSH, it does not and when I pass the variable 'NAM' from KSH it works: I have all the file below. I need help so code gives output if I uncomment the line
#SUBSEQ = "'$sub'"; and comment out the hardcode
SUBSEQ = "0633-009_001200008|225065338468009";

Not following why the if block does not evaluate SUBSEQ if ( arr[j,1] == SUBSEQ && && arr[j,2] == NAM)

#! /bin/ksh


# read subject and sequence
set -A subseq_array $(<subseq.txt)


# read the applicable QVALS Order for the study ( can be from define)

set -A qnam_array $(<qnam.txt)


for sub in ${subseq_array[@]}
do
for n in ${qnam_array[@]}
do

#print $sub ;

`awk -F ':' 'BEGIN { }
{

{
#SUBSEQ = "'$sub'";
SUBSEQ = "0633-009_001200008|225065338468009";
NAM = "'$n'";
NULL = " |";
}



for(i=1;i<=NF;i++)
{
arr[NR,i]=$i;
}
}

END{


for(j=1;j<=NR;j++)
{

if ( arr[j,1] == SUBSEQ && arr[j,2] == NAM) {
{printf("%s|",arr[j,3]);}
exit; }
}



for(j=1;j<=NR;j++)
{


if ( arr[j,1] == SUBSEQ && arr[j,2] != NAM) {
{printf("%s",NULL);}
exit; }

}






}' supp_q.txt >> out1.txt`

done
print $sub >> out1.txt
break;
done



The files:
subseq.txt
=========

0633-009_001200008|225065338468009
0633-009_001200008|225065338468010
0633-009_001200009|225065338468008
0633-009_001200018|225065338468009
0633-009_001200018|225065338468011

qnam.txt
=======

CMBRTRM
CMCLAS1
CMCLAS2
CMCLAS3
CMCLSCD1
CMCLSCD2
CMCLSCD3
CMROUTE

source file that I am reading from
supp_q.txt
==========

0633-009_001200008|225065338468009:CMBRTRM:albuterol
0633-009_001200008|225065338468009:CMCLAS1:respiratory system
0633-009_001200008|225065338468009:CMCLAS2:drugs for obstructive airway diseases
0633-009_001200008|225065338468009:CMCLAS3:adrenergics for systemic use
0633-009_001200008|225065338468009:CMCLSCD1:R
0633-009_001200008|225065338468009:CMCLSCD2:R03
0633-009_001200008|225065338468009:CMCLSCD3:R03C
0633-009_001200008|225065338468010:CMCLSCD2:R03
0633-009_001200008|225065338468010:CMCLSCD3:R03C
0633-009_001200009|225065338468008:CMBRTRM:albuterol
0633-009_001200009|225065338468008:CMCLAS1:respiratory system
0633-009_001200009|225065338468008:CMCLAS2:drugs for obstructive airway diseases
0633-009_001200009|225065338468008:CMCLSCD3:R03C
0633-009_001200018|225065338468009:CMCLAS2:drugs for obstructive airway diseases
0633-009_001200018|225065338468011:CMROUTE:RESPIR

initial output file that I am writing to from AWK:
============
CMBRTRM|CMCLAS1|CMCLAS2|CMCLAS3|CMCLSCD1|CMCLSCD2|CMCLSCD3|CMROUTE|USUBJID|CMSEQ|

what the output needs to be:
========================

CMBRTRM|CMCLAS1|CMCLAS2|CMCLAS3|CMCLSCD1|CMCLSCD2|CMCLSCD3|CMROUTE|USUBJID|CMSEQ|
albuterol|respiratory system|drugs for obstructive airway diseases|adrenergics for systemic use|R|R03|R03C| |0633-009_001200008|225065338468009
# 2  
Old 06-18-2009
Hello!

Per forum rules, and the benefit of all users, please search the forums before posting a question.

This question has been asked and answered numerous times.

You can easily search the forums using our internal Google search engine or our advanced internal search engine. You can also search our huge UNIX and Linux database by user generated tags or search the database for unanswered questions and provide an answer.

Thank you.

The UNIX and Linux Forums

Last edited by vgersh99; 06-18-2009 at 06:24 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing output parameter(Oracle) to variable in ksh Script

Hello, I have researched and tried many way to pass OUT parameter to be stored in variable in KSH Script.Still not success, please help. Here is my Store Procedure. create procedure testout3(v_in varchar2,v_out OUT integer) as begin v_out := 1; end; Here is my testvout.ksh #!/bin/ksh... (1 Reply)
Discussion started by: palita2601
1 Replies

2. Shell Programming and Scripting

awk with passing variable

I have file called in in.txt contains with the below lines I want to display the lines between the value which I would be passing. one two three four five ten six seven eight Expected output if I have passed one and ten two three four five (8 Replies)
Discussion started by: mychbears
8 Replies

3. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

4. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

6. Shell Programming and Scripting

Passing variable to awk

Hi, I'm new with this stuff, but I hope you can help me. This is what I'm trying to do: for id in $var; do awk '{if ($1 == $id) print $2}' merg_data.dat > neigh.tmp done I need that for every "id", awk search the first column of the file merg_data.dat which contains "id" and... (3 Replies)
Discussion started by: matteo86
3 Replies

7. Shell Programming and Scripting

Passing value of variable in KSH

Here is a sample script demonstrating the issue x() { echo "foo" a=1 } ## the value of $a is 1 a=0 x echo $a ## the value of $a stays 0 a=0 x|sed "s/foo/bar/" echo $a Result: foo 1 (1 Reply)
Discussion started by: pbmarvin56
1 Replies

8. UNIX for Dummies Questions & Answers

Passing a Shell Variable to awk

Hello, I have a file with 4 columns. An arbitrary example is shown below: a Tp 10 xyz b Tq 8 abc c Tp 99 pqr d Tp 44 rst e Tr 98 efg Based on the values in col 2 and col 3, I will execute another program. I have been running this:... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

9. Shell Programming and Scripting

passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working): if ];then searchstr=$1 lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr' else echo "usage: $0 <search string>" fi I tried several options. Is it... (3 Replies)
Discussion started by: rein
3 Replies

10. Shell Programming and Scripting

Passing FORM(HTML) variable to ksh

I am currently able to use the $QUERY_STRING variable and simply cut out what I need to be assigned as variables within the shell script. However, I've been able to use the "name" value assigned within the FORM(HTML) as a variable when I use perl. Why is it that ksh doesn't read the "name" in as... (1 Reply)
Discussion started by: douknownam
1 Replies
Login or Register to Ask a Question