1748031 Members
5195 Online
108757 Solutions
New Discussion юеВ

AWK

 
Karthick K S
Frequent Advisor

AWK

How to assign awk value in some Variable for example
awk NR==1 { print substr($0,33,3); } ${MQUTIL}/abc.snf

for above output value will go to some variable
10 REPLIES 10
RAC_1
Honored Contributor

Re: AWK

awk -v "var=value" '{your code}'
There is no substitute to HARDWORK
Muthukumar_5
Honored Contributor

Re: AWK

You can use awk -v var="value"

# echo "hi" | awk -v var=bye '{ print $0" "var }'
hi bye
# Another way as after awk using variable
# echo "hi" | awk '{ print $0" "var }' var=bye
hi bye

hth.
Easy to suggest when don't know about the problem!
VEL_1
Valued Contributor

Re: AWK

You want store the ouput of awk to Variable or want use variable in awk?


you can use BEGIN block or use awk option:

awk --assign var=val

eg:

$ echo | gawk -v var=200 'BEGIN { var = var + 100 }; END { print var }'
300


Karthick K S
Frequent Advisor

Re: AWK

What My question is

Var = 'awk NR==1 { print substr($0,33,3); } ${MQUTIL}/abc.snf'

echo $VAR
output is not coming
Arunvijai_4
Honored Contributor

Re: AWK

As per # man awk says,

-v var=value Cause var=value assignment to occur before the BEGIN action (if it exists) is executed.

Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Karthick K S
Frequent Advisor

Re: AWK

Raj,

I want to store output of awk in some variable
VEL_1
Valued Contributor

Re: AWK



Its case sensitive.

var=`awk NR==1 { print substr($0,33,3); } ${MQUTIL}/abc.snf`

echo $var
Muthukumar_5
Honored Contributor

Re: AWK

Sorry. Do you want to store the return contents or return value of awk NR==1 { print substr($0,33,3); } ${MQUTIL}/abc.snf command execution.?

# Return String or Content
retstr=$(awk NR==1 { print substr($0,33,3); } ${MQUTIL}/abc.snf)

# Return Value of execution
awk NR==1 { print substr($0,33,3); } ${MQUTIL}/abc.snf
ret=$?
echo "Return value is ${ret}"

hth.

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

Re: AWK

Var = `awk NR==1 { print substr($0,33,3); } ${MQUTIL}/abc.snf`

echo $Var

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"