Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing variables to awk

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0

Hello, in my shell script, I've created some variables.
Then I envoke awk in the script and would like to use those variables. What's the proper way to do this??

Thanks.
 
If you have shell variables v1 and v2, use

awk -v va=$v1 -v vb=$v2 ...

and refer to va and vb in your awk program. CaKiwi
 
Thanks for the reply CaKiwi,
this works great. However, I have a loop going where shell variable va1 changes but the awk variable va stays the same.

ex

ls -l | while read LINE
do
va1=`echo $LINE | awk '{print $NF}'`

awk -v va=$va1 '{ print va }'

done

in this example the output is

myscript.sh
myscript.sh
myscript.sh... for as many files are in the current directory.

I'd like the awk variable va to be dynamic so that awk prints all the files in the directory. Thanks for any help.
 
Thanks for the reply CaKiwi,
this works great. However, I have a loop going where shell variable va1 changes but the awk variable va stays the same.

ex

ls -l | while read LINE
do
va1=`echo $LINE | awk '{print $NF}'`

awk -v va=$va1 '{ print va }'

done

in this example the output is

myscript.sh
myscript.sh
myscript.sh... for as many files are in the current directory.

I'd like the awk variable va to be dynamic so that awk prints all the files in the directory. Thanks for any help.
 
I don't understand this. What's the purpose of this script?

you're looping through ALL the files - getting the name of the file.

what the purpose of the SECOND AWK? what EXACTLY are you trying to do? Give us the desired output for a given directory listing, pls!


vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
I assume your script is just a test of the syntax as it has no useful function, as vlad pointed out. I don't understand exactly why it is repeating the same value but it is caused by the fact that the second awk has no input. Try

awk -v va=$va1 'BEGIN{ print va }'

or

echo xx | awk -v va=$va1 '{ print va }' CaKiwi
 
Basically, the second awk is only used to print some nicely formatted information about each file...

output:

File Name: myscript.sh
Owner: Jack Frost
Permissions: User: rwx Group: rw Other: rw
File type: Shell Script
File Size: 2336Kb
etc... for all files.


so, I'm looping through each file long listing and grabbing some elements and putting them into a variable for the second awk to use for final output..
 
Would it not be easier to feed the output of ls directly into your awk and pull the information out by the positional parameters, avoiding the while loop altogether?

ls -l | awk '{usr=substr($0,2,3); grp= . . .

(Forgive me if the user protections don't start in column two. I don't have a Unix box handy right now and may be misremembering the ls -l output.)
If you need to do some processing on them that awk is not good for, then pass your variables as the input to the awk:

ls -l | while read LINE
do
v1=...
v2=...
echo v1 v2 | awk '{printf(&quot;Yada %s yada %s ...\n&quot;, $1, $2)}'
done

or even better since it only calls awk once,

ls -l | while read LINE
do
v1=...
v2=...
echo v1 v2
done | awk '{printf...

Last, you can also pass awk variables after the program string, mixed in with the input files and without using the
-v switch. If I recall correctly (consult your &quot;awk&quot; manpages), for files File1 and File2:
File1:
a
b

File2:
c
d

the instruction

awk '{print $0, filenum}' filenum=1 File1 filenum=2 File2

will produce the following output:

a 1
b 1
c 2
d 2


But from my undestanding of what you want to do, I would definitely go with the first alternative I mentioned.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top