0

I want to add a 5th column to this csv file

$ head countries_int_code.csv
country,latitude,longitude,name
AD,42.546245,1.601554,Andorra
AE,23.424076,53.847818,United Arab Emirates
AF,33.93911,67.709953,Afghanistan
AG,17.060816,-61.796428,Antigua and Barbuda
AI,18.220554,-63.068615,Anguilla
AL,41.153332,20.168331,Albania
AM,40.069099,45.038189,Armenia
AN,12.226079,-69.060087,Netherlands Antilles
AO,-11.202692,17.873887,Angola

this adds the values to the column

$ awk -F, 'NR>1 {$5="Unknown"}1' OFS=, countries_int_code.csv > countries_int_code2.csv

$ head countries_int_code2.csv
country,latitude,longitude,name
AD,42.546245,1.601554,Andorra,Unknown
AE,23.424076,53.847818,United Arab Emirates,Unknown
AF,33.93911,67.709953,Afghanistan,Unknown
AG,17.060816,-61.796428,Antigua and Barbuda,Unknown
AI,18.220554,-63.068615,Anguilla,Unknown
AL,41.153332,20.168331,Albania,Unknown
AM,40.069099,45.038189,Armenia,Unknown
AN,12.226079,-69.060087,Netherlands Antilles,Unknown
AO,-11.202692,17.873887,Angola,Unknown

This gives the column its name

$ awk -F, 'NR<=1 {$5="ColName"}1' OFS=, countries_int_code2.csv > countries_int_code3.csv


$ head countries_int_code3.csv
country,latitude,longitude,name,ColName
AD,42.546245,1.601554,Andorra,Unknown
AE,23.424076,53.847818,United Arab Emirates,Unknown
AF,33.93911,67.709953,Afghanistan,Unknown
AG,17.060816,-61.796428,Antigua and Barbuda,Unknown
AI,18.220554,-63.068615,Anguilla,Unknown
AL,41.153332,20.168331,Albania,Unknown
AM,40.069099,45.038189,Armenia,Unknown
AN,12.226079,-69.060087,Netherlands Antilles,Unknown
AO,-11.202692,17.873887,Angola,Unknown

Instead of writing to various files I could just use a tmpfile to write to and then rename it to the original. ... countries_int_code.csv > tmpfile && mv tmpfile countries_int_code.csv

But can i combine the 2 awk commands above into the 1 command to do it all, so I can add column values and column name in 1 command?

1 Answer 1

2
$ awk 'BEGIN{FS=OFS=","} {print $0, (NR>1?"Unknown":"ColName")}' file
country,latitude,longitude,name,ColName
AD,42.546245,1.601554,Andorra,Unknown
AE,23.424076,53.847818,United Arab Emirates,Unknown
AF,33.93911,67.709953,Afghanistan,Unknown
AG,17.060816,-61.796428,Antigua and Barbuda,Unknown
AI,18.220554,-63.068615,Anguilla,Unknown
AL,41.153332,20.168331,Albania,Unknown
AM,40.069099,45.038189,Armenia,Unknown
AN,12.226079,-69.060087,Netherlands Antilles,Unknown
AO,-11.202692,17.873887,Angola,Unknown
Sign up to request clarification or add additional context in comments.

2 Comments

tks, could you kindly just give a brief breakdown of the parameters?
Not sure what you mean - I just combined your examples but used a ternary expression to decide what to print at the end of each line instead of unnecessarily assigning to a 5th field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.