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.gzconcatenates a compressed file tostdouthead -n 1grabs the first linegrep -o ","finds the commas and prints them on new lineswc -lcounts the linesxargs printf "%d + 1\n"creates the string(ncommas) + 1since we need to add one to the number of commas to count the columns.bc -luses the bash calculator to evaluate(ncommas) + 1