1753873 Members
7546 Online
108809 Solutions
New Discussion юеВ

AWK

 
Sundar_7
Honored Contributor

AWK

Hi all,

I have one query in AWK..

I am trying to assign some value to a variable and then trying to access that variable out of awk..

But then I could not succeed in doing that..

Say for ex

$ echo "one two" | awk '{var=$1}'

$ echo "$var"

I would like to use the value of variable "VAR" out of awk.

Is there any way to do it ?

I checked the awk documentation as well..

But then could not find what I want..

So..Can any one pls give me some input on this issue ?

Thanks

Sundar
Learn What to do ,How to do and more importantly When to do ?
7 REPLIES 7
Corthouts Carlo
Valued Contributor

Re: AWK

Hi,

Maybe try :

export var=`echo "one two" | awk '{print $1}'

echo $var then shows 'one'

Peter Kloetgen
Esteemed Contributor

Re: AWK

Hi,

first you have to give variables to AWK when using the -v option

awk -v var=value

you can now use the variable in awk- programs.

And you have the possibility to put the output of awk into a variable using command substitution:

var=`cat file |awk '{print $5}'`

after this "trick", you can use var in your scripts.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Peter Kloetgen
Esteemed Contributor

Re: AWK

Hi,

there is a little typo in Corthouts Carlos script, he forgot to close the backticks for the command substitution.

Additional:

when using a ksh or a POSIX shell you can also use the following syntax:

var=$(command1 | command2 | command3)

this is bad if you use a Bourne shell, which doesn't know this.


Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Robin Wakefield
Honored Contributor

Re: AWK

Hi Sundar,

# a=fred
# echo hello | awk '{print $0,var}' var=$a
hello fred

should do it for you.

Rgds, Robin.
Sundar_7
Honored Contributor

Re: AWK

Friends & Gurus,

I know there are two possible ways to take out the value from AWK to a variable

$ var="Value"

$ awk -v a=$var '{print a}' /tmp/somefile

But then that is not what I am trying to do..

We can only pass down the values from Shell to AWK ..

I am looking out for a way to pass down the value from AWK to SHELL..i.e I will assign some value to a variable inside AWK and then I would like to refer that Variable out of AWK in the SHELL...

and when it comes to assigning variable a value like this

$ var=$(awk '{inst}' files)

I beleive We can assign value to only one variable..

But I am looking to assign value to MORE THAN one VARIABLE..and then refer that variable out of AWK..

Hope and wish I am clear and pls let me have some inputs on these things

Thanks

Sundar
Sundarhp@hotmail.com
Learn What to do ,How to do and more importantly When to do ?
A. Clay Stephenson
Acclaimed Contributor

Re: AWK

In that case what you need to do is someting like this:

awk { ..... } | read var1 var2 var2

You can get the idea by doing something like this:

echo "XX" "YY" "ZZ" | read var1 var2 var3

$var1 -> XX
$var2 -> YY
$var3 -> ZZ

If it ain't broke, I can fix that.
Peter Kloetgen
Esteemed Contributor

Re: AWK

Hi,

of course you can read several variables the way described by Clay. Or you can redirect the output of the awk- command into a file:

awk '{action}' file > "file_name"

and work with the file after that....

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping