Hi,
Could someone please let me know me how i can use an awk command to print the No. of fields of each file present in a Directory?Suppose the Directory has 5 text files in which the first record in each file contains fields separated by a '|'. I need to use a awk command to display the No of fields for each of these txt files as follows:
From GNU awk manual:
"All known awk implementations silently skip over zero-length files. This is a by-product of awk's implicit read-a-record-and-match-against-the-rules loop: when awk tries to read a record from an empty file, it immediately receives an end of file indication, closes the file, and proceeds on to the next command-line data file, without executing any user-level awk program code."
GNU awk can deal with this but it's very specific. I believe it's better to write a shell script.
I think it is important to note that Scrutinizer's solution is the most efficient as it will need to read one line from each file. Any solution that uses something like
FNR == 1 { action }
will read all lines from all files. If the files being tested are large, this could take a significant amount of time to do an awful lot of unneeded I/O.
The only suggestion I'd make is to close the file after getting the first line. If there are a lot of files in the parameter list, not closing the file after use could result in exceeding the max open file descriptors limit for the process.