Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Can you pass vatiables to awk?

Can you pass vatiables to awk?

In a shell script:

server=jamie

awk '{if ($2 = $server).....
13 REPLIES
Honored Contributor Honored Contributor

Re: Can you pass vatiables to awk?

Hi Jamie,

Yes. You will have use -v flag with awk to pass them. For ex.,

string="Sridhar"
echo "test" |awk -v name=$string '{print name}'

-Sri
You may be disappointed if you fail, but you are doomed if you don't try

Re: Can you pass vatiables to awk?

I got it to work with the following as well:

awk '{if ($2 = "'$server'"...

Thanks
Honored Contributor Honored Contributor

Re: Can you pass vatiables to awk?

Yeah.. its' the same way

server=jamie

awk -v server=$server '{if ($2 = $server)...


-Sri
You may be disappointed if you fail, but you are doomed if you don't try

Re: Can you pass vatiables to awk?

Actually it dosn't work either way...

Instead of the string 'jamie' I get:

if $2 = server

I tried it with $server and "$server" but it doesn't work...
Acclaimed Contributor Acclaimed Contributor

Re: Can you pass vatiables to awk?

That's because in awk only positional variables ($1,$2, ...) get the $; all user defined variables do not use the $ prefix.
If it ain't broke, I can fix that.
Honored Contributor Honored Contributor

Re: Can you pass vatiables to awk?

I simply cut and pasted your line. I didn't realize you had a $ in front of $server. Look at my first example where I didn't use $ in front of 'name'.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Honored Contributor Honored Contributor

Re: Can you pass vatiables to awk?

Yes. We can do this as,


echo "jamie" | awk '{ if ( $1 == server ) print "ok" }' server=jamie

It will pass that environment variable of server with jamie string there.

You can more check as,

echo "hai" | awk '{ print server }' server=jamie

HTH.
Easy to suggest when don't know about the problem!

Re: Can you pass vatiables to awk?

Here's what I get... (changed the variables to do a time range instead...)

server_time="07:08:55"
awk -v time=$server_time {if ($9 < time && $10 > time)

when I debug it looks like this:

awk -v time=07:08:55 {if ($9 < time && $10 > time)

shouldn't the time be replaced with the value of the time variable?
Honored Contributor Honored Contributor

Re: Can you pass vatiables to awk?

You can also --assign var=value on awk too.

It is as,

echo "hai jamie" | awk --assign server=jamie '{ if ( $1 == server ) print "OK" }'

Else try awk -v =value

see man awk.

HTH.
Easy to suggest when don't know about the problem!
Honored Contributor Honored Contributor

Re: Can you pass vatiables to awk?

Hi Jamie,

Your time variable should get passed correctly but your 'test' conditions may produce unexpected results as the time variable is treated as 'text' and using <,> operators on it may not give what you expect.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Honored Contributor Honored Contributor

Re: Can you pass vatiables to awk?

...and just to confirm that your time variable is passed correctly, put a "print time" statement in your awk script.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try

Re: Can you pass vatiables to awk?

I see now that the variable is getting passed corectly.. my error is now in my script. I'm going to keep playing...
Thanks
Highlighted
Honored Contributor Honored Contributor

Re: Can you pass vatiables to awk?

Hai Jamie,

Started to play with awk right now. If you get your problem solved, treat responders with points to get more informations in near future and get good interaction.

Best wishes.
Muthukumar.
Easy to suggest when don't know about the problem!