my code below extracts ‘Country’ but how would I also extract the ‘city’ in the
second row?
geoiplookup 212.58.246.82 | grep Country | cut -c 28-45
GeoIP Country Edition: GB, United Kingdom
GeoIP City Edition, Rev 1: GB, N7, Tadworth, N/A, 51.283298, -0.233300, 0, 0
GeoIP City Edition, Rev 0: GB, N7, Tadworth, N/A, 51.283298, -0.233300
I want my output to look like this -> city, Country
for example: Tadworth, United Kingdom
My code currently just gives me United Kingdom
The
geoiplookupis producing comma-separated output. Trying extract data by column numbers, as you’re doing with thecutcommand, is going to be very fragile.cutwith field seperators — orawk, or a high-level scripting language — is going to be more flexible. For example, the followingawkscript will do approximately what you want:We first split each line into a label and data separated by a
:.Then within the data, we split on
,and extract the relevant fields.