I have a file which has three columns. Column 3 contains names of genes and it looks like this:
Rv0729,Rv0993,Rv1408
Rv0162c,Rv0761c,Rv1862,Rv3086
Rv2790c
How can I print the number of genes in each row?
text processing
I have a file which has three columns. Column 3 contains names of genes and it looks like this:
Rv0729,Rv0993,Rv1408
Rv0162c,Rv0761c,Rv1862,Rv3086
Rv2790c
How can I print the number of genes in each row?
Best Answer
You simply want to add a column with the count of columns in it. This may be done using
awk
:NF
is anawk
variable containing the number of fields (columns) in the current record (row). We print this number followed by a comma and the rest of the row, for each row.An alternative (same result, but may look a bit cleaner):
FS
is the field separator whichawk
uses to split each record into fields, and we set that to a comma with-F ','
on the command line (as in the first solution).OFS
is the output field separator, and we set that to be the same asFS
before reading the first line of input.