Counting comma separated characters in a row

How can I count how many elements are separated with comma in a given column of a file (tab_delimited), using awk or sed?


AC    1,23,5
DC    2,8,89

Would return:

AC    3
DC    3
Asked By: dovah

awk -v col=2 -F 't' '{$col=gsub(",", "", $col)+1; print}'

col is a parameter representing the analyzed column number and -v assigns 2 to it.

-F 't' sets the field separator to the tab character1. gsub returns the number of substitutions, so it’s being used to count the number of commas. The result is assigned to analyzed column and the final print displays all the columns.

$ cat input 
AC  1,23,5
DC  2,8,89
XX  3,4,5,6,7   YYY,ZZZ
$ awk --assign col=2 -F 't' '{$col=gsub(",", "", $col)+1; print}' < input
AC 3
DC 3

1 Some awk implementations might not parse t and in that case you can use some Bash specific quoting$'t' – which will pass the tab character itself to awk.

Answered By: Cristian Ciupitu
$ awk -F '[t,]' '{print $1, NF-1}' some_file


  • -F sets the field separator, i.e. either tab or comma
  • $1 references the first field
  • NF is a built-in variable that contains the number of fields in a record
  • the awk statement is executed for each record (i.e. for each line)
Answered By: maxschlepzig
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.