Skip to content
Advertisement

Syntax error at or near {

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!

JavaScript

Advertisement

Answer

Inside an action block the awk condition syntax is C-like so you need:

JavaScript

instead of

JavaScript

but more importantly it SOUNDS like all you need is:

JavaScript

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:

JavaScript

If you CAN have duplicate file names then it gets even harder to implement, something like this (untested):

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement