The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
80 chars limit for params broli SUN Solaris 2 12-19-2008 09:05 AM
Solaris 10 kernel Params, Set/Get? NewSolarisAdmin SUN Solaris 8 12-12-2008 05:40 PM
script takes params keromotti Shell Programming and Scripting 2 06-06-2008 08:15 AM
evaluating params abzi UNIX for Dummies Questions & Answers 3 11-22-2005 11:40 PM
Change params for time zone agustincm UNIX for Advanced & Expert Users 7 04-05-2005 05:40 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-25-2009
anduzzi anduzzi is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 48
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 (permalink)  
Old 02-25-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by anduzzi View Post
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 (permalink)  
Old 02-25-2009
anduzzi anduzzi is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 48
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 (permalink)  
Old 02-25-2009
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,365
Quote:
Originally Posted by anduzzi View Post
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]}" '...'

  #5 (permalink)  
Old 02-25-2009
anduzzi anduzzi is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 48
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 !
  #6 (permalink)  
Old 02-25-2009
anduzzi anduzzi is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 48
Oh sorry....my bad !
nawk works perfect in this case.

Thank you so much for your time..
  #7 (permalink)  
Old 02-25-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

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'.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0