Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate a CSV column in Bash

Tags:

bash

csv

awk

I have an issue where a client needs to duplicate a column in a CSV file. The values are always going to be identical and unfortunately our API doesn't allow for duplicate columns to be specified in the JSON.

For example I have the following column structure and values:

Name, Surname, City, Age, Job
John, Doe, Johannesburg, 28, Technical Support

Now I need to duplicate City so the output should be:

Name, Surname, City, City Again, Age, Job
John, Doe, Johannesburg, Johannesburg, 28, Technical Support

The column needs to be placed after that which will be duplicated. The value is also dependent on this first column.

like image 933
Predator Avatar asked May 25 '17 09:05

Predator


1 Answers

awk can handle this easily:

awk 'BEGIN{FS=OFS=", "} {$3 = $3 OFS $3} 1' file.csv

Name, Surname, City, City, Age, Job
John, Doe, Johannesburg, Johannesburg, 28, Technical Support

Note that this does the job in single and shorted command that is easy to read and is far more efficient than pipeline command that involves calling cut twice and then a `paste.

As @codeforester rightly commented below, cut doesn't let a column be repeated in the output; it is used for stripping out a value.

like image 125
anubhava Avatar answered Sep 19 '22 19:09

anubhava