$ cat myfile.csv 1,2,3,4,5 a,b,c,d,e a,b,c,d,e
First get only the first row using
$ head -1 myfile.csv 1,2,3,4,5Next use
sedto remove everything except commas:
$ head -1 myfile.csv | sed 's/[^,]//g' ,,,,All what has left is to simply use
wccommand to count number of characters.
$ head -1 myfile.csv | sed 's/[^,]//g' | wc -c 5The file has 5 columns. In case you wonder why there are only 4 commas and
wc -lreturned 5 characters it is because
\nthe carriage return as an extra character.