I have an assigment where I have to count the number of words in each .c .cc and .h file.The problem is it keeps showing the syntax error at line 8 and 10 at or near { .This is not a finished script!It may have some other problems but I only needed help with the syntax error!
awk 'BEGIN {FS=" ";drb=0;valt=0;}
{if ( valt == 0 ){
for( i=1; i<=NF; i++)
drb++;
valt++;
}
else{
FNR==1{ printf "File name: %s,Word count: %dn",FILENAME, drb;drb=0;}
for(i=1;i<=NF;i++)
drb++;}}
END {printf "File name: %s,Word count: %d",FILENAME,drb }' `find $1 -name '*.c' -o -name '*.cc' -o -name '*.h'`
Advertisement
Answer
Inside an action block the awk condition syntax is C-like so you need:
if (FNR==1) { foo }
instead of
FNR==1 { foo }
but more importantly it SOUNDS like all you need is:
awk '
{ drb += NF }
ENDFILE { printf "File name: %s,Word count: %d",FILENAME,drb; drb=0 }
' files...
The above uses GNU awk for ENDFILE
. Note that this will work even for empty files which is a BIG problem for solutions using other awks (if they rely on testing FNR==1
instead of having a loop on ARGV[]
in an END
section, they will skip the file instead of printing it’s name with word count zero).
The correct way to do this with non-gawk awks (assuming no duplicate file names) is:
awk '
{ drb[FILENAME] += NF }
END {
for (i=1;i<ARGC;i++) {
fname = ARGV[i]
printf "File name: %s,Word count: %d",fname,drb[fname]
}
}
' files...
If you CAN have duplicate file names then it gets even harder to implement, something like this (untested):
awk '
FNR==1 { ++cnt[FILENAME] }
{ drb[FILENAME,cnt[FILENAME]] += NF }
END {
delete cnt
for (i=1;i<ARGC;i++) {
fname = ARGV[i]
printf "File name: %s,Word count: %d",fname,drb[fname,++cnt[fname]]
}
}
' files...