script to find function names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to find function names
# 1  
Old 12-21-2008
script to find function names

hi,
i am a newbie and have to write a rather complicated script. Assume that i have a variable called x and a C source code file say file1.c (these are the inputs of the script) and i need to find the names of all the functions in the C file containing x.Take the following code as an example:

Code:
 1.int main(void);    
 2.void foo(void);    
 3.void bar(void);    
 4.    
 5.int main(void)    
 6.{    
 7.  int x;    
 8.  return 0;    
 9.}   
 10.   
 11.void foo(void)   
 12.{   
 13.  int x;   
 14.}   
 15.   
 16.void bar(void)   
 17.{   
 18.  int x;   
 19.}

so i guess the output should be:


in 5. main(void) x occurs in line 7 as int x;

in 11.foo(void) x occurs in line 13 as int x

and in 16.bar(void) x occurs in line 18 as int x

Something similar to but not exactly as above. To print the name of the functions containing x is crucial.

i can find line number using grep -n and probably use sed -n '/{/,/}/p' file1.c to select blocks of code delimited by { and } but I'm not sure how to refine this further.

any help would be welcome.

thanks,
Sam

Last edited by Franklin52; 12-21-2008 at 06:54 AM.. Reason: Reformat code
# 2  
Old 12-21-2008
Hi,

this will give you the function name containing "int x":

Code:
awk -F'\n' 'BEGIN{RS=""}/int x/{print $1}' file.c

HTH Chris
# 3  
Old 12-22-2008
try this it may help you egrep -n '\(|x' list | sort -n
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problems with script to find file names

Have a text file "test-array.txt" with contents below a23003 b23406 c23506 Tying to read the above file into an array and search for file-names in a directory TEST_DIR ,recursively with the above names in them. Example: If TEST_DIR has a files named xyxa_a23003_test.sql,... (4 Replies)
Discussion started by: gaurav99
4 Replies

2. Shell Programming and Scripting

Find function script

Hi everybody, I want your help on what, I suppose, might seem a simple task to many of you. the deal is to write a script that will look in a specific folder to see if there is any file whose size is bigger than say 1M and if so to execute a command. like if then log_rotate fi ... (3 Replies)
Discussion started by: lenci_xc
3 Replies

3. Shell Programming and Scripting

Extract function names and directories from php files

I need a script that extracts function names from php files together with their location (path and file in which they are defined). The php files are located in several directories under a base directory. Ideally the output should be something like: "Path/FileName/FunctionName" for a... (2 Replies)
Discussion started by: bamse
2 Replies

4. Homework & Coursework Questions

Shell script calling Perl function, sort and find data, write to new files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I must write a shell script that calls two external Perl functions--one of which sorts the data in a file, and... (6 Replies)
Discussion started by: kowit010
6 Replies

5. Shell Programming and Scripting

Find C function definition in from Shell script

Hi, I am having list of C function names say function1 function2 function3 and i am having many source files under my current directory. Now i would like to find the file names which contains the function definition from my list. It would be appreciable if... (1 Reply)
Discussion started by: tsaravanan
1 Replies

6. Shell Programming and Scripting

How to find pattern in file names?

I have some files, those are abbreviated (ed,ea, and bi) company_ed_20100719.txt company_ea_20100719.txt company_bi_20100719.txt I would like to rename these files by replacing ed with EmployeeDetails ea with EmployeeAddress bi with BankInfomration as company_... (3 Replies)
Discussion started by: LinuxLearner
3 Replies

7. Shell Programming and Scripting

Shell Script to display function names (called & defined) in a C++ Source Code

Hello to all, I am looking for a way to display only the names of function (calls & definition) of a C++ source code.There is already a post related to this, but the script is to find the functions using a specific variable, and the replies are not that convincing since they cannot be used for... (2 Replies)
Discussion started by: frozensmilz
2 Replies

8. Shell Programming and Scripting

sorting names after doing a find

hi all, is there an easy way in ksh to sort the results of a find in say alphabetical order ?? e.g in my script i am doing a find for all servers find . -name "server.xml" and the output is not in any order. thanks in advance. (4 Replies)
Discussion started by: cesarNZ
4 Replies

9. UNIX for Advanced & Expert Users

Find File names with sustitution

Hi All, Iam trying to find two kinds of files while ignoring rest of the files in a directory The files are like below Files to be found -------------------- perp45560 oerp4556 Files to be ignored ---------------------- oerp4556123450 oerp4556123470 I was trying the following... (4 Replies)
Discussion started by: baanprog
4 Replies

10. IP Networking

find computer names from IP addresses?

Arright, here's what I'm trying to do. I want to dig up currently active IP addresses on my subnet, and my present strategy is to ping every address until I find active ones, then ping them more often to verify their status. Next, I want to find the names of the computers associated with those... (1 Reply)
Discussion started by: sladuuch
1 Replies
Login or Register to Ask a Question