Add index column to CSV file
I assume you have a commas delimited file.
Using vim, open the file. In normal mode, type
:%s/^/\=line('.').','/
:%s/^/\=line('.')/
adds the line number at the beginning of the line. Since you have a commas delimited file (add a column) you need a comma after your line number. so the .','
see this answer for full explanation about :%s/^/\=line('.')/
This will probably work:
awk -F'\t' -v OFS='\t' '
NR == 1 {print "ID", $0; next}
{print (NR-1), $0}
' input.csv > output.csv
In awk
, the NR
variable is "the total number of input records seen so far", which in general means "the current line number". So the NR == 1
in the first line is how we match the first record and add the "ID" column header, and for the remaining lines we use NR-1
as the index.
The -F'\t'
argument sets the input field separator, and -vOFS='\t'
sets the output field separator.
Since no technology is specified in the original post, I'd be happy here to keep it simple.
(all the fancy Vim
/bash
solutions are fine if you know what you're doing).
- Open the CSV file in your favourite spreadsheet programme (I'm using LibreOffice, but Excel or a native Mac equivalent will do)
- insert a column to the left of column A
- Enter a 1 into cell A2, the first cell under the headers
- Double-click the blob at the bottom right of the cell as shown in the screenshot:
This last step will fill the index column with 1,2,3...
etc.
You can then save the resulting spreadsheet as a CSV file again.