Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading