bash - Add index column to CSV file -
i have large comma-separated file (6gb) , add index column it. i'm looking @ unix type solutions efficiency. i'm using mac.
i have this:
v1 v2 v3 0.4625 0.9179 0.8384 0.9324 0.2486 0.1114 0.6691 0.7813 0.6705 0.1935 0.3303 0.4336
would this:
id v1 v2 v3 1 0.4625 0.9179 0.8384 2 0.9324 0.2486 0.1114 3 0.6691 0.7813 0.6705 4 0.1935 0.3303 0.4336
this work:
awk -f'\t' -v ofs='\t' ' nr == 1 {print "id", $0; next} {print (nr-1), $0} ' input.csv > output.csv
in awk
, nr
variable "the total number of input records seen far", in general means "the current line number". nr == 1
in first line how match first record , add "id" column header, , remaining lines use nr-1
index.
the -f'\t'
argument sets input field separator, , -vofs='\t'
sets output field separator.
Comments
Post a Comment