Awk command that will display the total number of fields in a file, can be like this:
awk '{ sum += NF } END { print sum }' inputfile
Because awk's variables (other than built-ins) are initialized to the null string, by default. This null string has also numerical value zero. So, you can skip BEGIN section. I like this feature of awk and other scripting languages.
Of course it depends on what you mean by a field? In you first example the unix command wc -w testfile will do the same thing, and if you want to return just the 12, then you can do wc -w testfile | tr -s " " | cut -d" " -f2 ... (which I guess is just as complicated as using awk!
Okay. Same idea.
Here is something really ugly. Could someone
show me how to write it entirely in awk?
#!/bin/sh
gen=`ls -l | awk ' { if ($! !~ /^d/) print $9}' > filelist`
$gen || echo $? ; exit 1 > /dev/null
for i in `cat filelist`
do
awk' {tot += NF} END { print tot, FILENAME }' $i
done
This gives you the files in the working directory and a field count the hard way;
and it is totally inelegant.
I am new here so if you have the time I would appreciate it.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.