The answer to your question is you are doing it wrong. In your manual method you are assigning a value to FS (using -F) but then in second process you create an awk variable using -v and
assign the string 'awkvar' as FS.
The reason this did not work in the way you think is because awk variables are only visible to awk and before entering the single quotes of the awk script you are not in awk (hope that made sense)
So to do what you wanted:
Code:
grep "line2" file.txt | awk -F"$var1" '{print $2}'
That being said, this would seem to be the long way to do this.
Try:
Code:
awk -vawkvar="$var1" '$0 ~ awkvar && /line2/{print $NF}' file.txt