Can you pass vatiables to awk? (82 Views)
Reply
Advisor
Jamie Collins
Posts: 28
Registered: ‎10-12-2004
Message 1 of 14 (82 Views)

Can you pass vatiables to awk?

In a shell script:

server=jamie

awk '{if ($2 = $server).....
Please use plain text.
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 2 of 14 (82 Views)

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
Please use plain text.
Advisor
Jamie Collins
Posts: 28
Registered: ‎10-12-2004
Message 3 of 14 (82 Views)

Re: Can you pass vatiables to awk?

I got it to work with the following as well:

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

Thanks
Please use plain text.
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 4 of 14 (82 Views)

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
Please use plain text.
Advisor
Jamie Collins
Posts: 28
Registered: ‎10-12-2004
Message 5 of 14 (82 Views)

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...
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 6 of 14 (82 Views)

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.
Please use plain text.
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 7 of 14 (82 Views)

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
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 8 of 14 (82 Views)

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!
Please use plain text.
Advisor
Jamie Collins
Posts: 28
Registered: ‎10-12-2004
Message 9 of 14 (82 Views)

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?
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 10 of 14 (82 Views)

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!
Please use plain text.
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 11 of 14 (82 Views)

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
Please use plain text.
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 12 of 14 (82 Views)

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
Please use plain text.
Advisor
Jamie Collins
Posts: 28
Registered: ‎10-12-2004
Message 13 of 14 (82 Views)

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
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 14 of 14 (82 Views)

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!
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation