Help with searching a word,find the identifier under which it is there and giving a report

Hi,

i have a directory structure which contains few files each.
each file in turn has some functions. i'm searching for a word, say 'pen' in all the files present in all the directories. :wall:

consider a file file1.c in Dir1. out of the 3 funcs present in this file, func1(pennum) and func3('pen++' in the commented part) has 'pen' word in them.

-----------
Dir1
file1.c
-----------

int func1(int var1,char var2)
{
	int pennum = 0;
	while(pennum<10)
	{
		pennum++;
	}
}

char func2(int var1,char var2)
{
	int hen = 0;
	while(hen<10)
	{
		hen++;
	}
}

other func3(int var1,char var2)
{
	int den = 0;
	
	while(den<10)
	{
		/* should this be pen++ :P*/
		den++;
	}
}

similarly a file file2.c in Dir2. out of the 2 funcs present in this file, func19('pen' in the commented part) has 'pen' word in them.:rolleyes:

-----------
Dir2
file2.c
----------

char func2(int var1,char var2)
{
	int hen = 0;
	while(hen<10)
	{
		hen++;
	}
}

other func19(int var1,char var2)
{
	int den = 0;
	
	while(den<10)
	{
		/* should this be pen :P*/
		den++;
	}
}

there is another file file3.c with no 'pen' word in the only function present in it.:wall:
-----------
Dir2
file3.c
----------

char func2(int var1,char var2)
{
	int hen = 0;
	while(hen<10)
	{
		hen++;
	}
}

Now i need all the function names which have pen in them.:confused:
I would appreciate:) your help with the shell script to get the output in this format.

---------------------------------------------------------------------------------------------------------------
FileName                                               FunctionName                                              pattern
---------------------------------------------------------------------------------------------------------------
file1                                                          func1                                                    pennum 
file1                                                          func3                                                    pen++
file2                                                          func19                                                  pen

Thanks,
Srini:b:

I had a script which could be modified for this:

$ cat flatc.awk

BEGIN {
        ORS=" " # Records separated by space instead of newline
}

{
        if($0 ~ /^#/)
                printf("\n%s\n", $LINE); # preprocessors statements
        else
        {
                $1=$1;  # awk prints the unmodified line unless you
                        # modify $1..$N somewhere, so $1=$1 flattens
                print;
        }
}

END {
        printf("\n"); # Even one big line must end in \n
}

$ cat infunc.sh

#!/bin/sh

for FILE in "$@"
do
        awk -f flatc.awk "$FILE" |
                sed 's/[{}]/\n&\n/g;s/)/)\n/g;s/(/ (/g' |
                awk -v F="$FILE" -v OFS="\t" '
                        /^[{]$/ { B++ }
                        /^[}]$/ { B--; if(!B) FUNC="" }

                        (!B) && /^ *[a-zA-Z]+ +[a-zA-Z][a-zA-Z0-9]* *\(.*\)$/ {
                                FUNC=$2
                        }

                        FUNC && match($0, /pen[^ ]*/) && !A[FUNC]++ {
                                print F, FUNC, substr($0, RSTART, RLENGTH);
                        }'
done

$ ./infunc.sh file*.c

file1.c func1   pennum
file1.c func3   pen++
file2.c func19  pen

$
1 Like

Forgot to mention this condition earlier:o. It is NOT the convention that function names should start with func. There can be a function name retNum in a file new.c.

int retNum(int n)
{
    int pendent = 0;
  /* Though 'pen' is present thrice in the function only one instance of retNum has to be given in the report*/
    pendent = n;
return pendent;
}

And the result should be like along with the earlier output.

new.c    retNum    pendent 

That's okay, because my code didn't actually depend on that.