awk - counting content of files

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

Answers (2)

glenn jackman
glenn jackman

Reputation: 246827

If you have GNU awk, there's also:

gawk 'BEGINFILE {n++; nextfile} END {print n}' *

Upvotes: 2

Sean
Sean

Reputation: 15961

$ awk 'BEGIN { print ARGC - 1 }' file1 file2 file3
3

Upvotes: 2

Related Questions