Does awk ever resolve params ?..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Does awk ever resolve params ?..
# 1  
Old 02-25-2009
Does awk ever resolve params ?..

Hi,
Does awk ever resolve params in the search pattern?..
The following awk doesnt know how to resolve ${tables[$j]}$ inside a loop.
Code:
k=`awk '/${tables[$j]}$/ ${graph[$i]}`

The search pattern has ${tables[$j]}$ and I am narrowing down my search with a $ at the end of string.
So...this leaves me with a question on if awk ever resolve the params in the search patterns?...I am sure it resolves in other places...like in this case ${graph[$i]}

Thx !
# 2  
Old 02-25-2009
Quote:
Originally Posted by anduzzi
Hi,
Does awk ever resolve params in the search pattern?..
The following awk doesnt know how to resolve ${tables[$j]}$ inside a loop.
Code:
k=`awk '/${tables[$j]}$/ ${graph[$i]}`

The search pattern has ${tables[$j]}$ and I am narrowing down my search with a $ at the end of string.
So...this leaves me with a question on if awk ever resolve the params in the search patterns?...I am sure it resolves in other places...like in this case ${graph[$i]}

Thx !
what are these: ${tables[$j]}" and "${graph[$i]}"?
Shell variables?
# 3  
Old 02-25-2009
Hi vgersh,
"${tables[$j]}" and "${graph[$i]}" are 2 arrays holding 2 distinct sets of elements like table list and graph list.
A simplified code snippet giving a better idea...

Code:
#! /bin/ksh

count1=`ls -lart P*.ksh | awk '{print $9}' | wc -l`
list1="`ls -lart P*.ksh | awk '{print $9}'`"
set -A graph $list1

count2=`wc -l read_table.dat | awk '{print $1}'`
list2="`cat read_table.dat`"
set -A tables $list2

j=0

while ((j<$count2))
do
  i=0
 while ((i<$count1))
   do
   k=`awk '/ETL_VW_SCHEMA/ && /[.]${tables[$j]}$/ || /EDW_DB_SCHEMA/ && /[.]${tables[$j]}$/' ${graph[$i]}`
       ((i=i+1))
   done
  ((j=j+1))
done

Awk in the inner most loop doesnt resolve ${tables[$j]} during the run time....but no issues with ${graph[$i]}.
Hope this gives a better idea on the problem...

Thx !
# 4  
Old 02-25-2009
Code:
k=`awk -v t="${tables[$j]}" '/ETL_VW_SCHEMA/ && $0 ~ ("[.]" t "$") / || /EDW_DB_SCHEMA/ && $0 ~ ("[.]" t "$")' ${graph[$i]}`

NOTE: as always, if on Solaris, use 'nawk' or '/usr/xpg4/bin/awk' instead of the old 'awk'.
# 5  
Old 02-25-2009
Quote:
Originally Posted by anduzzi
Hi vgersh,
"${tables[$j]}" and "${graph[$i]}" are 2 arrays holding 2 distinct sets of elements like table list and graph list.
A simplified code snippet giving a better idea...

Code:
#! /bin/ksh

count1=`ls -lart P*.ksh | awk '{print $9}' | wc -l`
list1="`ls -lart P*.ksh | awk '{print $9}'`"


Why are you using -l then removing all the details? You are calling 2 unnecessary external commands.

Code:
count1=`ls -art P*.ksh | wc -l`
list1=`ls -art P*.ksh`

Why are you using the -a option? Your pattern cannot match any dot files.

In fact you are using more than 2 UECs:

Code:
set -- P*.ksh
count1=$#
list1=`ls -rt P*.ksh`

Quote:
Code:
set -A graph $list1

count2=`wc -l read_table.dat | awk '{print $1}'`


There's no need for awk:

Code:
count2=$( wc -l < read_table.dat)

Quote:
Code:
list2="`cat read_table.dat`"
set -A tables $list2

j=0

while ((j<$count2))


I recommend using the standard syntax:

Code:
while [ $j -lt $count2 ]

Quote:
Code:
do
  i=0
 while ((i<$count1))
   do
   k=`awk '/ETL_VW_SCHEMA/ && /[.]${tables[$j]}$/ || /EDW_DB_SCHEMA/ && /[.]${tables[$j]}$/' ${graph[$i]}`
       ((i=i+1))


I recommend using the standard syntax:

Code:
i=$(( $i + 1 ))

Quote:
Code:
   done
  ((j=j+1))
done

Awk in the inner most loop doesnt resolve ${tables[$j]} during the run time....but no issues with ${graph[$i]}.

Variables are not expanded inside single quotes.

To pass a shell variable to awk, use the -v option:

Code:
awk -v x="${tables[$j]}" '...'

# 6  
Old 02-25-2009
Hi Johnson/Vgersh,
Thank you very much for the suggestions and the pointers..
But my shell errors out with the awk syntax....here are some quick details on my OS and the actual error itself...I tried switching my shell to ksh and run it but still errors out the same way as mentioned below.

Code:
$ awk -v t="B1_STAT_HIST" '/ETL_VW_SCHEMA/ && $0 ~ ("[.]" t "$")  || /EDW_DB_SCHEMA/ && $0 ~ ("[.]" t "$")' PDEWG300_B1_STAT_HIST.ksh
awk: syntax error near line 1
awk: bailing out near line 1

$ uname -a
SunOS xspetl01 5.8 Generic_117350-36 sun4u sparc SUNW,Sun-Fire-480R

Appreciate your suggestions !
Thx !
# 7  
Old 02-25-2009
Oh sorry....my bad !
nawk works perfect in this case.

Thank you so much for your time..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cannot resolve $variable in awk

My script ---------- for i in `cat n`;do export k=`echo "CSN: "$i` //combining CSN: and value from n echo "$k" awk ''{print "CSN: "$0;}'{_=29}_&&_--' file1|tail -1 >> file2 done In the above script i cannot able to resolve $k in awk command file n contains ------------ 0000 1111 2222... (2 Replies)
Discussion started by: Mohana29_1988
2 Replies

2. Shell Programming and Scripting

Cannot resolve $var in awk

My script ---------- for i in `cat n`;do export k=`echo "CSN: "$i` //combining CSN: and value from n echo "$k" awk ''{print "CSN: "$0;}'{_=29}_&&_--' file1|tail -1 >> file2 done In the above script i cannot able to resolve $k in awk command file n contains ------------ 0000 1111... (0 Replies)
Discussion started by: Mohana29_1988
0 Replies

3. Shell Programming and Scripting

The awk subtraction giving exponential values.Please help resolve it

Hi friends, I have a file list1 which has these 2 columns like 616449 0 434453 1 2151083 0 2226536 0 2132382 0 2136814 0 I have to put the result of col1 -col2 into another file list2 linewise. e.g. It gives the below result if use the below code: awk '{ print $1 - $2 }' list1 >... (2 Replies)
Discussion started by: kunwar
2 Replies

4. Shell Programming and Scripting

Check params for number

I have 2 and three params, both I should make sure thay numbers at one single line insted of checking for each one . Example I wroote the following way.. checking for 2 and three seperately but I shud be able to do it at on statement echo $2 | egrep '^+$' >/dev/null 2>&1 if ; then echo... (2 Replies)
Discussion started by: raopatwari
2 Replies

5. Shell Programming and Scripting

Pass params with Udev

Hello! I'm sorry if this is the false Forum, didn't really knew where to put it... My question: I have serveral USB-Sticks and wrote several Udev-Rules for theme, each Sticks needs to do something else, but all are using the same script (they have common tasks to do) and only some parts are... (2 Replies)
Discussion started by: al0x
2 Replies

6. UNIX for Dummies Questions & Answers

Can any good awk'er resolve this issue?

awk -F^ '{ if ((($1 != "M") && ($5 != "2")) || (($1 != "S") && ($5 != "7"))) print $0}' welcome > welcome1 The "&&" and "||" in the above command is not working with awk. When I run the above command, the same content of welcome is copied to welcome1 without any difference. Your reply is... (12 Replies)
Discussion started by: karthickrn
12 Replies

7. Shell Programming and Scripting

script takes params

i want to write a shell script that can be run as ./deployPortal.sh -version 5.1.2 -portlet -exportall how can i do that? version param is required. bu the others are optional. in first step i only want to read 5.1.2, is portlet selected ? and is exportall selected ?? can you... (2 Replies)
Discussion started by: keromotti
2 Replies

8. Shell Programming and Scripting

sed & awk--get section of file based 2 params

I need to get a section of a file based on 2 params. I want the part of the file between param 1 & 2. I have tried a bunch of ways and just can't seem to get it right. Can someone please help me out.....its much appreciated. Here is what I have found that looks like what I want....but doesn't... (12 Replies)
Discussion started by: Andy Cook
12 Replies

9. UNIX for Dummies Questions & Answers

evaluating params

Hi all, I ve a script like.... TBL=employee sql=`cat abhi.sql` \\ abhi.sql contains ------- select a from $TBL echo $TBL echo $sql SQL=`echo $sql` echo $SQL now i want SQL as select a from employee and as select a from $TBL How can I achieve this? Help appriciated (3 Replies)
Discussion started by: abzi
3 Replies

10. UNIX for Advanced & Expert Users

Change params for time zone

Hi! How ican change parameters for daylight, i mean, i want change the daylight on december first week. I've tried with /usr/lib/tztab but result unsuccessful. (7 Replies)
Discussion started by: agustincm
7 Replies
Login or Register to Ask a Question