I'm using a bash script. I have a tab delimited file with data that looks like

Afghanistan     AF      AFG     004
ALA     Aland Islands   AX      ALA     248
Albania AL      ALB     008
Algeria DZ      DZA     012

I want to rewrite the data like so


which boils down to extracting the third to last token, writing an equals, and then printing the second to last token. I thought "awk" could help me, but I don't know how to say "second to last". This doesn't cut it

awk '{print $2=$3}' /tmp/country_data.tsv 

How can I extract the data in the way I want?


awk '{printf "%s=%s\n", $(NF-2),$(NF-1)}' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.