skip to Main Content

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?

2

Answers


  1. 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
    
    Login or Signup to reply.
  2. This might work for you (GNU sed):

    sed -Ens 's/S+/&/8;T;F;=;p' *.csv | paste - - -
    

    If there is no eighth column, break.

    Output the file name F, the line number = and print the current line p.

    Feed the output into a paste command which prints three lines as one.

    N.B. The -s option resets the line numbers for each file, without it, it will number each line for the entire input.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search