Print filenames & line number with number of fields greater than 'x'

I am running Ubuntu Linux. I am in need to print filenames & line numbers containing more than 7 columns. There are several hundred thousand files.

I am able to print the number of columns per file using awk. However the output I am after is something like

file1.csv-463 which is to suggest file1.csv has more than 7 records on line 463. I am using awk command awk -F"," '{print NF}' * to print the number of fields across all files.

Please could I request help?

>Solution :

If you have GNU awk with you, try following code then. This will simply check condition if NF is greater than 7 then it will print that particular file’s file name along with line number and nextfile will take program to next Input_file which will save our time because we need not to read whole Input_file then.

awk -F',' 'NF>7{print FILENAME,FNR;nextfile}' *.csv

Above will print only very first match of condition to get/print all matched lines try following then:

awk -F',' 'NF>7{print FILENAME,FNR}' *.csv

Leave a Reply