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`
(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.
But I have two question?
Can you please clearify them.
I thought only single slash is required for escaping, then whats the meaning of double slash.
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.
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}'`"