Assign o/p of awk to a variable

:confused: Hi UNIX gurus,

I am facing a typical problem while assigining while assigining output of awk to a variable.

I have a fixed length file say myinputfile.txt

When I allow the value/output of an awk to be redirected to a file, it works fine. i.e.
awk "/^.{232}$acctNum/ { printf(\"%s;Y;Account#\n\",substr(\$0,241,1)) ; exit} " myinputfile.txt >> mylogfile.txt

However, when I try to assign the the value of same to a variable, and then echo the variable, it displays blank.i.e.
myvariable=`awk "/^.{232}$acctNum/ { printf(\"%s;Y;Account#\n\",substr(\$0,241,1)) ; exit} " myinputfile.txt`

Can somebody please help. :confused:

Is this $acctNum a variable or pattern?

It is a variable.

Are you sure that you are getting output from awk command?
can you show your input file?

Only awk is working fine. However, while assigning the same to variable, it is not working.

My Sample Inputfile is as shown below:

MyAdd1 MyAdd2 MyAdd3 MyAdd4 MyAdd5 Sachin Tendulkar 222 222-2222 11111111111111A0000111122223333XYX
YourAdd1 YourAdd2 YourAdd3 YourAdd4 YourAdd5 Rahul Dravid 111 121-5555 22222222222222B0123456789123456XYX

(In above example, spaces are getting truncated in this forum)
Basically, position 233-240 denotes account number. Also, by substring, I am trying to extract 241 character.

Add another backslash before $0

myvariable=`awk "/^.{232}$acctNum/ { printf(\"%s;Y;Account#\n\",substr(\\$0,241,1)) ; exit} " myinputfile.txt`

Hey Thank you anbu :slight_smile:

But I have two question? :confused:
Can you please clearify them.

  1. I thought only single slash is required for escaping, then whats the meaning of double slash.

  2. Also why doesnt the following one work:
    myvariable=`awk '/^.{232}$acctNum/ { printf("%s;Y;Account",substr($0,241,1)) ; exit} ' myinputfile.txt`
    In this case we dont even need escaping. :confused:

YYYN939@CANPC-003249 ~
$ x=`awk "{ print \$0 }" f`
x=`awk "{ print \$0 }" f`
awk "{ print $0 }" f
++ awk '{ print bash }' f
+ x=

YYYN939@CANPC-003249 ~
$ x=`awk "{ print \\$0 }" f`
x=`awk "{ print \\$0 }" f`
awk "{ print \$0 }" f
++ awk '{ print $0 }' f
+ x='Hi
How are you'

YYYN939@CANPC-003249 ~
$ x=$(awk "{ print \$0 }" f)
x=$(awk "{ print \$0 }" f)
awk "{ print \$0 }" f
++ awk '{ print $0 }' f
+ x='Hi
How are you'

This wont work because variable substitution wont occur inside single quotes. So this variable $acctNum wont expand.
You can use try this

awk -v no var=$acctNum ' $0 ~ "^.{232}"var { ... } ' file

or

awk '/^.{232}'"$acctNum"'/ { ... } ' file 

I have a similar problem by assigning a value of a command to a variable. In this case, with:
Get list of Network interfaces: /usr/sbin/networksetup -listnetworkserviceorder
Get specifically the one that contains the word "Ethernet": awk -F'\\) ' '/\Ethernet/ {print $2}'`

Assign the whole string to a variable called: "networkInterface": networkInterface= "`/usr/sbin/networksetup -listnetworkserviceorder | awk -F'\\) ' '/\Ethernet/ {print $2}'`"

Any ideas? I'm really stuck in here.:frowning: