How to Count Number of Columns in CSV File in Bash

Renesh Bedre    less than 1 minute read

You can use the awk command to count the number of columns in a CSV file as shown below,

awk -F ',' '{print NF; exit}' file.csv

In above command, the -F parameter sets the field separator to a comma. The NF is a predefined variable which counts the number of field in current row. The exit avoid processing the entire file. Briefly, it prints number of fields and exit.

Let’s take an example of file.csv to count the number of columns in a CSV file.

The content of the file.csv is as below


Ther are 6 columns in file.csv. Let’s count it with awk,

awk -F ',' '{print NF; exit}' file.csv  



Subscribe to get new article to your email when published

* indicates required

This work is licensed under a Creative Commons Attribution 4.0 International License

Some of the links on this page may be affiliate links, which means we may get an affiliate commission on a valid purchase. The retailer will pay the commission at no additional cost to you.