accessing a shell variable in an awk print statement 
Author Message
 accessing a shell variable in an awk print statement

I have a shell program that creates another shell script using output
from 3 awk lines. The first program then runs the constructed script.
The only problem is I need to access a shell variable (in this case
$dbf ) to print the correct line to execute. However awk will not access
this variable. Is there anyway to pass parameters to awk without using
the scriptfile (-f) option ?  My code is below...

cd $rawdir
foreach i (u*)
 exec dbflst $i/$i"log.dbf" > $i/$i"log.dbflst"&
 wait
 cd $i/
 echo \#\!\/bin\/csh > cat_files.csh
 awk '$5 != "" {printf"cat %s.ldr %s.ldr > %sa.ldr;\n",$4,$5,$5}' <
$i"log.dbflst" >>  cat_files.csh
 awk '$5 != "" {printf"rm %s.ldr; rm %s.ldr;\n",$4,$5}' <
$i"log.dbflst" >> cat_files.csh
 awk '$5 != "" {printf"exec flineupdate %s %s %s %s
%sa&;\n",$dbf,$1,$4,$5,$5}'
< $i"log.dbflst" >>  cat_files.csh
 cat *.catlog* > $i.cat_log
 mv $logpath$project.masterlog $logpath$project.tmplog
 cat $logpath$project.tmplog $i.cat_log > $logpath$project.masterlog
 rm  $i.catlog*
 chmod u+rx cat_files.csh
 exec cat_files.csh
 wait
 echo "removing temporary script cat_files.csh"
 rm cat_files.csh
end

cat_files.csh code produced is below...

#!/bin/csh
cat u232203.ldr u232204.ldr  >  u232204a.ldr;
cat u232206.ldr u232207.ldr  >  u232207a.ldr;
rm u232203.ldr; rm u232204.ldr;
rm u232206.ldr; rm u232207.ldr;
exec flineupdate "?"  2  u232203 u232204 u232204a
exec flineupdate "?"  5  u232206 u232207 u232207a

if $dbf is "norl3000.dbf" it should be where the "?"s are above.

Anyone know how I can plop a value in there ?

thanks...

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Mon, 01 Jul 2002 03:00:00 GMT  
 accessing a shell variable in an awk print statement

% this variable. Is there anyway to pass parameters to awk without using
% the scriptfile (-f) option ?

?? There are even more ways to pass parameters to awk without using -f than
there are using -f.

If you're in a shell script, you can use a HERE document (OK, this uses -f,
so sue me)
 awk -f - file1 file2 file <<HERE
 BEGIN { print "$i is \$i" }
 HERE

If the variable is ane environment variable (as opposed to a mere shell
variable), you can use the ENV array:
 awk 'BEGIN { print ENV[i], "is $i" }'

You can always assign a value to an awk variable on the command-line:
 awk -v dollari=$i 'BEGIN { print dollari, "is $i" }'

Finally, you can{*filter*}around with quotes so that $i is expanded by
the shell:
 awk 'BEGIN { print "'$i' is $i" }'
--

Patrick TJ McPhee
East York  Canada



Tue, 02 Jul 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. how to print a shell variable in awk states

2. awk variables --> shell variables

3. AWK-Script to set a shell variable (Win32) ?

4. Passing command line variables to AWK in shell script

5. setting shell variables with results from awk

6. Exporting awk variables to the UNIX shell

7. help for newbie: way to get the value of an awk variable in a shell script

8. how to use a shell variable in awk for pattern matching

9. Shell variables in AWK

10. Quoting Swamp: Saving an awk command in a shell variable

11. How to pass shell variable to awk?

 

 
Powered by phpBB® Forum Software