1

I want to be able to give a country's name and have the script search a CSV file for me and pop out that country's capital. I am having issues getting the awk command to pull the capital based on my input.

#!/bin/bash

#searchforacountry'scapital

read -p "What country's capital are you looking for? " countryname

awk -F "\"*,\"*" -v awkvar="$countryname" '/awkvar/ {print $2}' /workspaces/Code/testfiles/countriesandcapitals.csv

This is my code so far. When run it gives no results after inputting a country's name.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
James Chin
  • 55
  • 3

2 Answers2

1

warning you can't use the syntax /awkvar/ with a variable, you need to use the ~ aka match operator:

awk -F, -v country="$countryname" '$0 ~ country{print $2}' file

$0 is awk syntax for the entire line, and ~ is the regex match operator. It's a lot like the /awkvar/ part in your attempt, except that /.../ treats its contents as a literal string rather than a variable reference.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • thank you so much for your responses and for solving my issue! could you explain this section to me? $0 ~ country I do not know how it works – James Chin Apr 26 '23 at 19:19
  • 2
    @JamesChin `$0` is awk syntax for "the entire line", and `~` is the regex match operator. It's a lot like the `/awkvar/` part in your attempt, except that `/.../` treats its contents as a literal string rather than a variable reference. – Gordon Davisson Apr 26 '23 at 19:29
  • Thanks Gordon, I add this description to my post – Gilles Quénot Apr 26 '23 at 20:12
  • I guess the original intent was more like `$1 == awkvar { print $2 }`. Matching everywhere in the line is probably a bad idea anyway. – U. Windl Apr 29 '23 at 22:50
1

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.

Daweo
  • 31,313
  • 3
  • 12
  • 25