linux - awk usage in a variable -
actlist file contains around 15 records. want print/store each row in variable perform further action. script runs echo $j displays blank value. issue? script:
#/usr/bin/sh aclist=/root/john/actlist rowcount=`wc -l $aclist | awk -f " " '{print $1}'` ((i=1; i<=rowcount; i++)); j=`awk 'fnr == $i{print}' $aclist` echo $j done file: actlist
cat > actlist 5663233332 2223 2 5656556655 5545 5 4454222121 5555 5 . . .
the issue happens related quotes , way shell interpolates variables.
more specifically, when write
j=`awk "fnr == $i{print}" $aclist` the awk code must enclosed double quotes. necessary if want shell able substitute $i actual value stored in i variable.
on other hand, if write
j=`awk 'fnr == $i{print}' $aclist` i.e. single quotes, $i interpreted literal string.
hence fixed code read:
#/usr/bin/sh aclist=/root/john/actlist rowcount=`wc -l $aclist | awk -f " " '{print $1}'` ((i=1; i<=rowcount; i++)); j=`awk "fnr == $i{print}" $aclist` echo $j done remember: shell variable interpolation before calling other commands.
having said that, there places, in supplied code, improvements devised. that's story.
Comments
Post a Comment