1821980 Members
3445 Online
109638 Solutions
New Discussion юеВ

use variable in awk..

 
SOLVED
Go to solution
amonamon
Regular Advisor

use variable in awk..

Hello I have part of my program that uses awk..and calculate value...
so I get $w variable with one value..

then in the other part of script I want to use that value in other nawk...

basiclly

other part would be...
...
...
..

q=`cat file20060808 | wc -l`
echo $q


nawk -F"|" -v x=20060808 -v w=$q'
{u=u+1; l=l+$77}
($77 != 0)&&($123 == 1)&&($124 == 0) { a=a+1; s=s+$77}
END { print x"|"u"|"l"|"q"|"s } ' fileA >> final


but this does not pass value of echo $q to my nawk..

do I miss something?

thanks
8 REPLIES 8
Chan 007
Honored Contributor

Re: use variable in awk..

Hi,

Why is this line, with only one "'"?

nawk -F"|" -v x=20060808 -v w=$q'

Chan


amonamon
Regular Advisor

Re: use variable in awk..

I was just courious how to pass variable from previous operation to this nawk..so that nawk can print it in its print function...
Chan 007
Honored Contributor
Solution

Re: use variable in awk..

Hi,

try passing like w=$(echo $q)

Chan
amonamon
Regular Advisor

Re: use variable in awk..

chan...perfecto...that worked...;)
Peter Nikitka
Honored Contributor

Re: use variable in awk..

Hi,

using 'nawk' tells me that you probably are working under Solaris...

Nevertheless this should work, since the spaces are stripped from the content of $q:

x=file20060808
q=`wc -l <$x`
echo "'$q'" # blanks contained
q=`echo $q` # no blanks contained
q=`wc -l <$x | tr -d ' '`
echo "'$q'" # no blanks contained

nawk -F'|' -v x=$x -v w=$q '...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Hein van den Heuvel
Honored Contributor

Re: use variable in awk..

amonamon,

>>> q=`cat file20060808 | wc -l`

Why create a process, activate 'cat' and pipe all the data to wc when wc is perfectly happy to read the file itself?

>>> then in the other part of script I want to use that value in other nawk...

The problem has been solved in prior replies, but I'd like you to consider just doing all the work in AWK.

Something like (untested...)

nawk -F"|" -v x=20060808 '
BEGIN {file = "file" x; while (getline < file ) {w++}}
{....


btw... in your question yo pick up $q, try to assign to awk variable 'w' but then appear to use 'q' in the END for the awk script. Did you not intent to use 'w' in the print, or just use q as the variable for awk as well?

fwiw,
Hein.
Bill Hassell
Honored Contributor

Re: use variable in awk..

Another technique to pass shell variables to awk/nawk is to single quote the special awk symbols, then insert the variable in the appropriate location:

SEARCHSTRING="somePattern"
awk '/'"$SEARCHSTRING"'/ {print $2}' /mypath/myfile

or

MYFIELD=5
awk '{ print $'$MYFIELD'}'

Just escape the characters needed by awk and insert the variable's value as needed. It's a little simpler than using awk variables on the command line. But if you need the awk variables inside a separate script (not in the command line or in a here document), you can use the previous examples.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: use variable in awk..

Hi:

Most modern versions of 'awk' support the '-v' option for passing variables. Older versions insist the the external assignment is made as part of the argument list passed. Compare:

# awk -v VAR="world" 'BEGIN{print "hello",VAR}' /dev/null

# awk -v VAR="world" 'END{print "hello",VAR}' /dev/null

# awk 'BEGIN{print "hello",VAR}' VAR="world" /dev/null

# awk 'END{print "hello",VAR}' VAR="world" /dev/null

Note particularly the diffence in output in the third form. The advanage to the '-v arg=value' form is that variable assignment occurs even before the 'BEGIN' rules are executed.

Regards!

...JRF...