How to list all the .txt files (pipe delimited file) and the number of columns of each file in a directory?

  • post how should look the expected result – RomanPerekhrest Nov 21 '17 at 12:48
  • name and number of columns in that file. – Srijith A.H Nov 21 '17 at 12:52
  • elaborate your question whether it should find all text files recursively – RomanPerekhrest Nov 21 '17 at 12:53
  • it must list all the text files and number of columns in each text file – Srijith A.H Nov 21 '17 at 12:54
  • again, specify whether each filename should contain the full path – RomanPerekhrest Nov 21 '17 at 12:56
find . -name '*.txt' -type f -size +0 -exec awk -F '|' '
  FNR == 1 {print FILENAME ": " NF; nextfile}' {} +

Would print something like

./dir/foo.txt: 2

for each regular non-empty file whose name ends in .txt where "2" is the number of |-separated fields in the first line of the file.

Note that nextfile is not available in all awk implementations, but in those where it's not, it should be harmless (just less efficient as awk would read the files fully).

If you wanted to consider only the files that have the same number of columns in all their non-empty lines, with GNU awk:

find . -name '*.txt' -type f -size +0 -exec awk -F '|' '
  BEGINFILE {n = 0}
  NF {
    if (n && NF != n) {
      print "skipping "FILENAME" ("NF" != "n")" > "/dev/stderr"
      n = 0; nextfile
    n = NF
  ENDFILE {if (n) print FILENAME ": " n}' {} +
  • this worked out – Srijith A.H Nov 21 '17 at 13:07
  • @RomanPerekhrest, well, a file without pipes is also a pipe-separated file with only one column. I'm skipping empty files (with -size +0 but FNR == 1 would also take care of it) as the number of columns is not defined there. – Stéphane Chazelas Nov 21 '17 at 13:08

Another approach for any awk implementation:

find . -type f -name "*.txt" -exec awk -F'|' 'NF{ print FILENAME,NF; exit }' {} \;

  • -F'|' - treat | as field separator
  • NF{ ... } - ensures that there's at least one field/column in the file. NF itself points to the number of columns
  • perfect. getting the results... – Srijith A.H Nov 21 '17 at 13:16
find . -name "*.txt" -type f -exec wc -l {} \;
  • 1
    this is giving number of rows; instead I need number of columns (field) – Srijith A.H Nov 21 '17 at 13:05

You question isn't very clear but you can try this command: find DIRECTORY -name "*.txt" | xargs awk -F "SEPARATOR" "{print FILENAME, NF }" just replace DIRECTORY and SEPARATOR with your directory and column separator.

  • 1
    this is giving out the number of columns, but I need the name of file along with it. example: File name ||| number of columns – Srijith A.H Nov 21 '17 at 13:00
  • @SrijithA.H i see i didn't know you wanted it to print filename if you want to print filename alongside it just add FILENAME in the print argument check the example i gave above – AsenM Nov 21 '17 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.