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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with searching a word,find the identifier under which it is there and giving a report
# 1  
Old 04-19-2012
Java 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. Smilie

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
-----------
Code:
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.Smilie

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

Code:
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.Smilie
-----------
Dir2
file3.c
----------

Code:
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.Smilie
I would appreciateSmilie your help with the shell script to get the output in this format.
Code:
---------------------------------------------------------------------------------------------------------------
FileName                                               FunctionName                                              pattern
---------------------------------------------------------------------------------------------------------------
file1                                                          func1                                                    pennum 
file1                                                          func3                                                    pen++
file2                                                          func19                                                  pen

Thanks,
SriniSmilie

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 04-19-2012 at 07:03 PM..
# 2  
Old 04-19-2012
I had a script which could be modified for this:

Code:
$ 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

$

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-20-2012
how to find the func name when a function name can be anything.

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

Code:
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.

Code:
new.c    retNum    pendent

# 4  
Old 04-20-2012
That's okay, because my code didn't actually depend on that.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

2. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

3. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

4. Shell Programming and Scripting

Searching a word in multiple files

Hi All, I have a issue in pulling some heavy records , I have my input file has 10,000 records which i need to compare with daily appended log files from (sep 1st 2009 to till date) . I tried to use grep fgrep and even sed , but the as time is factor for me , i cannot wait for 5 days to get the... (3 Replies)
Discussion started by: rakesh_411
3 Replies

5. Shell Programming and Scripting

Insert tags in lines after searching for a word

Hi, I am new to Unix and this is my first post on this forum. I am trying to convert a file into an xml. In my input I want to search for any line that starts with a 'F' and make it a tag in the xml. See below for the input and output. Input : <Payment> <REFERENCE>78</REFERENCE> F123 : ... (7 Replies)
Discussion started by: akashgov
7 Replies

6. Shell Programming and Scripting

searching in a while where a word is not there.

Hi, I am new to unix, pls help I have a file suc_Logfile file. liek this. output success success abc Now i need to generate a file in shell script where it shows only abc. Now i am doing like this grep "^successfully\|$" $suc_Logfile >> $Final_Suc Pls help. Thanks,... (6 Replies)
Discussion started by: sailaja_80
6 Replies

7. Shell Programming and Scripting

shell script to find and replace a line using a identifier

Hi all im having trouble starting with a shell script, i hope someone here can help me i have 2 files file1: 404905.jpg 516167 404906.jpg 516168 404917.psd 516183 404947.pdf 516250 file2: 516250 /tmp/RecyclePoster18241.pdf 516167 /tmp/ReunionCardFINAL.jpg 516168... (7 Replies)
Discussion started by: kenray
7 Replies

8. Shell Programming and Scripting

Searching word in a file with awk

Hello everyone! I use a script to query for installed packages with yum (I use RHEL 4 with yum installed) and the output is redirected to a file. My script scan this file to find a package and the version of it. The script works fine until I search for a package name with special characters.... (3 Replies)
Discussion started by: studieu
3 Replies

9. UNIX for Dummies Questions & Answers

searching word in files

hi all i want to search some word in file but i want that it will search in all the files that i have in all the directories i do'nt know the name of the file i know that grep command want some specified file in which command shell i used thanks (2 Replies)
Discussion started by: naamas03
2 Replies

10. UNIX for Dummies Questions & Answers

Searching for key word within a file

Hello, I have a directory that holds all of my matlab codes. I am trying to run a searh on all of the matlab files that have the word "while" written inside the sytax of the code. Looking for all of the times that I did a while loop. Can someone help me do this? Thanks in advance. ... (1 Reply)
Discussion started by: moradwan
1 Replies
Login or Register to Ask a Question