Reputation: 1144
I want to make an awk script which counts how many files it has as arguments, if I use a counter at BEGIN or END the result will always be 1; Does awk merge the files which I give as parameters ?
The following script prints "1" no matter how many files I give as arguments ("n" will be used to count how many words are in all the files )
BEGIN {nrFiles++}
{ n+=NF}
END {print nrFiles}
And the final result:
{ n+=NF}
END {print "Number of files=",ARGC-1, "\nNumber of words=",n,"\nMean number of words=",n/(ARGC-1) }
Thanks for your time
Upvotes: 2
Views: 434
Reputation: 246827
If you have GNU awk, there's also:
gawk 'BEGINFILE {n++; nextfile} END {print n}' *
Upvotes: 2