count columns in a big text file
Today I wanted to know how many columns were in a large compressed CSV file. Here’s a hideous way to find out:
$ zcat bigfile.txt.gz | head -n 1 | grep -o "," | wc -l | xargs printf "%d + 1\n" | bc -l
I call this hideous because it’s totally unintelligble. But each element isn’t so bad:
zcat bigfile.txt.gz
concatenates a compressed file tostdout
head -n 1
grabs the first linegrep -o ","
finds the commas and prints them on new lineswc -l
counts the linesxargs printf "%d + 1\n"
creates the string(ncommas) + 1
since we need to add one to the number of commas to count the columns.bc -l
uses the bash calculator to evaluate(ncommas) + 1