Your code
awk -F "\"*,\"*" -v awkvar="$countryname" '/awkvar/ {print $2}' /workspaces/Code/testfiles/countriesandcapitals.csv
will output 2nd field for each line which contain literal awkvar
, you probably wanted to find lines containh substring which is hold in awkvar
, which could be done using index
function (note that it does just substring check, so you do not have to care about user giving input containing character of special meaning for regular expressions) as follows
awk -F "\"*,\"*" -v awkvar="$countryname" 'index($0,awkvar){print $2}' /workspaces/Code/testfiles/countriesandcapitals.csv
however note that this will also output capital name if you provide just capital name, to avoid this, if you are storing country name in 1st field you can do
awk -F "\"*,\"*" -v awkvar="$countryname" 'index($1,awkvar){print $2}' /workspaces/Code/testfiles/countriesandcapitals.csv
however this would also trigger for part of names, e.g. if you give e
as name you will get all countries having e
anywhere in name, if you wish to limit to only triggering when whole name is given
awk -F "\"*,\"*" -v awkvar="$countryname" '$1==awkvar{print $2}' /workspaces/Code/testfiles/countriesandcapitals.csv
Disclaimer this answer assumes you want case-sensitive solution, if this does not hold ignore this answer entirely.