I have a comma-separated .txt file structured like this:
ABC,NAME1,LASTNAME1
DEF,NAME2,LASTNAME2
GHI,NAME3,LASTNAME3
When running:
$ ./script.sh file.txt
#!/bin/bash
awk -F, '/NAME1/{print $3}' "$1"
I get my desired output:
LASTNAME1
When trying to replace NAME1
by passing a variable to awk
:
$ ./script.sh file.txt NAME1
#!/bin/bash
awk -v n="$2" -F, '/$n/{print $3'} "$1"
I do not get any output at all. I checked, that $2
entered in bash really is NAME1
. What am I missing?