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
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
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
2. Shell Programming and Scripting
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
3. UNIX for Dummies Questions & Answers
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
4. Shell Programming and Scripting
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
5. Shell Programming and Scripting
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
6. Shell Programming and Scripting
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
7. Shell Programming and Scripting
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
8. Shell Programming and Scripting
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
9. Shell Programming and Scripting
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
10. UNIX for Beginners Questions & Answers
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
LEARN ABOUT REDHAT
mac2unix
dos2unix(1) General Commands Manual dos2unix(1)
NAME
dos2unix - DOS/MAC to UNIX text file format converter
SYNOPSYS
dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...]
Options:
[-hkqV] [--help] [--keepdate] [--quiet] [--version]
DESCRIPTION
This manual page documents dos2unix, the program that converts plain text files in DOS/MAC format to UNIX format.
OPTIONS
The following options are available:
-h --help
Print online help.
-k --keepdate
Keep the date stamp of output file same as input file.
-q --quiet
Quiet mode. Suppress all warning and messages.
-V --version
Prints version information.
-c --convmode convmode
Sets conversion mode. Simulates dos2unix under SunOS.
-o --oldfile file ...
Old file mode. Convert the file and write output to it. The program default to run in this mode. Wildcard names may be used.
-n --newfile infile outfile ...
New file mode. Convert the infile and write output to outfile. File names must be given in pairs and wildcard names should NOT be
used or you WILL lost your files.
EXAMPLES
Get input from stdin and write output to stdout.
dos2unix
Convert and replace a.txt. Convert and replace b.txt.
dos2unix a.txt b.txt
dos2unix -o a.txt b.txt
Convert and replace a.txt in ASCII conversion mode. Convert and replace b.txt in ISO conversion mode. Convert c.txt from Mac to Unix
ascii format.
dos2unix a.txt -c iso b.txt
dos2unix -c ascii a.txt -c iso b.txt
dos2unix -c mac a.txt b.txt
Convert and replace a.txt while keeping original date stamp.
dos2unix -k a.txt
dos2unix -k -o a.txt
Convert a.txt and write to e.txt.
dos2unix -n a.txt e.txt
Convert a.txt and write to e.txt, keep date stamp of e.txt same as a.txt.
dos2unix -k -n a.txt e.txt
Convert and replace a.txt. Convert b.txt and write to e.txt.
dos2unix a.txt -n b.txt e.txt
dos2unix -o a.txt -n b.txt e.txt
Convert c.txt and write to e.txt. Convert and replace a.txt. Convert and replace b.txt. Convert d.txt and write to f.txt.
dos2unix -n c.txt e.txt -o a.txt b.txt -n d.txt f.txt
DIAGNOSTICS
BUGS
The program does not work properly under MSDOS in stdio processing mode. If you know why is that so, please tell me.
AUTHORS
Benjamin Lin - <blin@socs.uts.edu.au>
Bernd Johannes Wuebben (mac2unix mode) <wuebben@kde.org>
MISCELLANY
Tested environment:
Linux 1.2.0 with GNU C 2.5.8
SunOS 4.1.3 with GNU C 2.6.3
MS-DOS 6.20 with Borland C++ 4.02
Suggestions and bug reports are welcome.
SEE ALSO
unix2dos(1) mac2unix(1)
1995.03.31 dos2unix v3.0 dos2unix(1)