csvkit: Cannot ADD a column to a CSV file

I was looking at all the tools in csvkit, but couldn’t find any tool to add/insert an empty column to a CSV file. Surely something which should be part of csvkit.

I’m happy to get involved, but wanted to check whether there is an easy way to do this and I have overlooked it. The closest I got was the the --linenumbers option of csvcut.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

csvstack can be used to add a column by utilizing the grouping variable feature:

csvstack -n NEWCOL -g newval your-csv.csv

Another option is to just use awk. The following adds a column of 1337 to a csv (with headers).

cat file.csv | awk '{if(NR==1){print $0",new_column"}else{print $0",1337"}}' > added_column.csv

As far as I can tell then

echo "," | csvjoin examples/your-csv.csv - > your-new-csv.csv

adds two columns called a and b.

echo "New column name" | csvjoin examples/your-csv.csv - > your-new-csv.csv

adds a single named column.

Well, there are lots of ways to do this, but I guess such a basic operation as adding a column should be able to be done with csvkit itself…