Reputation: 8346
I tried to use bash variable inside awk by creating a variable in awk command as below. but it does not work it seems
b=hi
$ echo "hihello" |awk -v myvar=$b -F"$0~myvar" '{print $2}'
Actual Output is :
<empty / nothing printed >
Expected output is :
hello
Upvotes: 0
Views: 143
Reputation: 7959
Why don't you do this:
b=hi ; echo "hihello" | awk -F"$b" '{print $2}'
hello
Upvotes: 1
Reputation: 174696
Try the below awk command. Put the Field Separator inside BEGIN block.
$ b=hi; echo "hihello" | awk -v myvar=$b 'BEGIN{FS=myvar}{print $2}'
hello
It sets the value of myvar variable to the Field Separator. Thus inturn printing the second column will give you the string hello
Upvotes: 0