1825759 Members
2138 Online
109687 Solutions
New Discussion

Re: awk variable

 
SOLVED
Go to solution
Babak_4
Advisor

awk variable

Hi,

Is there any way to read a variable which is defined within an awk?

Thanks,

Babak
5 REPLIES 5
Francisco J. Soler
Honored Contributor

Re: awk variable

Hi Babak, where do you want to read the variable?, What do you want to do with the variable?

Frank.
Linux?. Yes, of course.
Nicolas Dumeige
Esteemed Contributor

Re: awk variable

Hello,

# echo "a b c" | awk '{ print $2 }' | read var
# echo $var
b

Not sure what you're looking thought ?

Cheers

Nicolas
All different, all Unix
Babak_4
Advisor

Re: awk variable

If I pass a vaiable to an awk and change its value, how can I have access to its updated value after exiting from awk. I'm looking for other solution than writing to stdout and assigning to a shell variable or writing to a file and read it from it, if there is any.

Thanks,

Babak
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: awk variable

What you are trying to do is forbidden by UNIX design. A child process is never allowed to alter a parent. The information flow is always parent to child; never the reverse. You couldn't even do this with C.

Your only option is to print the value to stdout and capture with a read. Bear in mind, that you could do something like
print 3 variables in awk on the same line and then read A B C in the shell would capture each of them.
If it ain't broke, I can fix that.
Babak_4
Advisor

Re: awk variable

Clay,

Thanks for your clarification.

Babak