find function name in a program file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find function name in a program file
# 1  
Old 06-22-2010
Power find function name in a program file

Hello All,
Is there any way to find a function name in a program file using perl.

for example, there is a file called Test.C in that
.
.
Code:
void function1(..)
{
<some code>
}
int function2(..)
{
.
.
sam() /*RA abc100*/
...
..
..
xyz()/*RA abc201*/
..
.
}

from this I know only the string 'abc100', using this how can I find the function name and the other associate RA numbers within the funcion.

thanks in advance,
Parthiban.

Last edited by Scott; 06-24-2010 at 06:15 PM.. Reason: Please use code tags
# 2  
Old 06-22-2010
Do you mean to search in a file for the given pattern?

Code:
grep 'abc100' TEST.C

Not sure I understood you correctly.
# 3  
Old 06-22-2010
It will show only the particular line which contains abc100.
I want the function name which contains 'abc100' and other other numbers which having the same pattern 'RA '.

I tried the following
Code:
awk -F"RA " '{ print $2}' $FileName | awk '{print $1}'| sed 's/[-+;:!=?"|{}()<>~\%\$\@\#\^\&\*\[\\\/]/ /g' | sed '/^$/d' | uniq

it will list out all the strings with pattern "RA " in the file.

I just want to know the function name which contains the specific string, and the line numbers of starting of the function and ending of the function.

any idea?

Last edited by Scott; 06-24-2010 at 06:16 PM.. Reason: Code tags, please...
# 4  
Old 06-23-2010
try this

- put this code in a file called script.pl or whatever you like:

$str=shift @ARGV;
while(<>){
$c++ if $sw && /\{/;
$c-- if $sw && /\}/;
if($sw && $c==0){
print "end line $.\n";
$sw=0;
}
if(!$sw && /(.+\(.*?\)).*\/.*RA $str/){
print "function $1\nstart line $.\n";
$sw=1;
}
}

- your C program file assuming is named prog.c and has opening an closing curly braces, like this:

void function1(..)
{
<some code>
}
int function2(..)
{
.
.
sam() /*RA abc100*/
{
...
..
..
}
xyz()/*RA abc201*/
{
..
.
}

- so if you do this you might get what you want:

perl script.pl abc100 prog.c

---------- Post updated at 08:12 PM ---------- Previous update was at 04:09 PM ----------

sorry I think i made a mistake, please use this perl script instead teh one i sent:

Code:
$str=shift @ARGV;
while(<>){
        if(/(.+\s+.+\(.*\))/){
                $func_name=$1;
                $func_start=$.;
                $sw=1;
                @ra=();
                next;
        }
        $c++ if $sw && /\{/;
        $c-- if $sw && /\}/; 
        if(/\/\*RA (.+)\*\//){
                push(@ra,$1);
        }
        if(/\/\*RA $str/){
                $print=1;
        }
        if($sw && $c==0){
                if($print){
                        print "here4\n";
                        print "function $func_name\n";
                        print "start line $func_start\n";
                        print "end line $.\n";
                        print "ra's:\n  ".join("\n  ",@ra)."\n";
                        $print=0;
                }
                $sw=0;
        }
}

This User Gave Thanks to rugdog For This Post:
# 5  
Old 06-24-2010
Data

Hi, Thanks for your great work. but This script doesn't print anything. I used the the following file for testing

file name : prog.c

Code:
function1()
{
  abcd;
  xyz;
  sam()/*RA abc00123*/
}

function2()
{
  abcd;
  xyz;
  sam;/*RA xyz00891*/
  sdfsdg;
  dgsdg;
  ertwt; /*RA abc100*/
  sf;
}

>perl script.pl abc100 prog.c
>


Last edited by Scott; 06-24-2010 at 06:17 PM.. Reason: Code tags, please...
# 6  
Old 06-24-2010
try this version

Code:
$str=shift @ARGV;
while(<>){
        if(!$sw && /(.+\(.*\))/){
                $func_name=$1;
                $func_start=$.;
                $sw=1;
                @ra=();
                next;
        }
        $c++ if $sw && /\{/;
        $c-- if $sw && /\}/; 
        if(/\/\*RA (.+)\*\//){
                push(@ra,$1);
        }
        if(/\/\*RA $str/){
                $print=1;
        }
        if($sw && $c==0){
                if($print){
                        print "here4\n";
                        print "function $func_name\n";
                        print "start line $func_start\n";
                        print "end line $.\n";
                        print "ra's:\n  ".join("\n  ",@ra)."\n";
                        $print=0;
                }
                $sw=0;
        }
}

This User Gave Thanks to rugdog For This Post:
# 7  
Old 06-25-2010
MySQL

Hi, Its working great... Thank you very much.. you made my work simple now.. As am new to perl can you explain me how it works.?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

how to find which program that update a specific file

Hello Would you tell me how to find which program that update a specific file? I am implementing migration project. My machines OS are AIX. It is because lack of documentation, some program cannot working properly on new machine. We found the root cause of this problem is that some data... (4 Replies)
Discussion started by: cstsang
4 Replies

2. Shell Programming and Scripting

Perl program for find one entry and put in different file

Hi I have a file name1 xxxxx name1 xxxxx name1 yyyyy name 1 zzzzz name1 Uniprot Id 1234 name2 sssss name2 eeeee name2 bengamine name2 Uniprot Id 3456 ......................and so on I have to capture Uniprot IDs only in a separate file so that output contain only ... (20 Replies)
Discussion started by: manigrover
20 Replies

3. UNIX for Dummies Questions & Answers

shell program- how many times a function is called

We have a program source C and is required to indicate how many times each function is called from the C program. also print the line number where there is a call. I've tried something like this: #!/bin/sh for i in $*;do if ! then echo $i is not a C file. else echo $i... (0 Replies)
Discussion started by: oana06
0 Replies

4. Shell Programming and Scripting

How to find the matched numbers between 2 text file using perl program??

hi dudes, I nee you kind assistance, I have to find the matched numbers from 2 text files and output of matched numbers should be in another text file.. I do have text files like this , for example File 1 787 665*5-p 5454 545-p 445-p 5454*-p File 2 5455 787 445-p 4356 2445 144 ... (3 Replies)
Discussion started by: sureshraj
3 Replies

5. Shell Programming and Scripting

use File::Find function

Hello, I'm learning the perl's Find function using unix but I keep getting this error when running the script: "Not a CODE reference at /usr/lib.perl5/5.8.8/File/Find.pm line 822" - what does this mean? Does anyone know??? Here's my script: use File::Find; find (\$dir,$ENV{HOME}); ... (4 Replies)
Discussion started by: new bie
4 Replies

6. Shell Programming and Scripting

program name and function name builtins

Hi Is there a way to get the program/script name or function name usng built ins. Like in many languages arg holds the program name regards (2 Replies)
Discussion started by: xiamin
2 Replies

7. Shell Programming and Scripting

want to find out a function name in a cpp file

I have an error in my logs as it shows some function name . 1. I dnt know where is the file.cpp located only i know the machine . 2. How to find out that the function name is loacated in which path and which file into that machine. Thanks . (1 Reply)
Discussion started by: madfox
1 Replies

8. UNIX for Dummies Questions & Answers

Program to find palindrome from a file

hello i came up with this ..The problematic part that i am facing is in underline and bold format. exec<file while read line do set $line i=1 while do str=$i #problematic i want the positional parameter but here it takes integer value. p=1 q=`expr length $str` flag=1... (6 Replies)
Discussion started by: salman4u
6 Replies

9. Programming

Tracing Function Calls in a program

Apart from writing debug and statements in constructors is there any way by which we can trace the function call stack at any depth? The issue that we always face is that when program crashes (Web Server running on Linux) we have no idea where it crashes and we have to do the hard way of... (1 Reply)
Discussion started by: uunniixx
1 Replies

10. UNIX for Dummies Questions & Answers

EXIT from a Program not from the function..

Hi, I want a command in unix shell script which will exit my whole program, not only from the function it's using. For e.g: $1 == "m_who" && $4 == "Extrnl Vendor" { print "You don have access" exit (0); } If this error is showing on the screen, then nothing should not... (9 Replies)
Discussion started by: ronix007
9 Replies
Login or Register to Ask a Question