Using grep to find C programming definition


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Using grep to find C programming definition
# 1  
Old 03-05-2019
Using grep to find C programming definition

Let's say I have a file written in C programming
Code:
...
int
function A (Param1, Param2)
{
....
}

How to grep this function?

I tried
Code:
grep -ER '^functionA(.*)/n{' filename > result.txt

Not work Smilie
Moderator's Comments:
Mod Comment Please use CODE tags on all sample input, output, and code segments; not just on shell code segments.

Last edited by Don Cragun; 03-06-2019 at 05:00 AM.. Reason: Add missing CODE tags.
# 2  
Old 03-06-2019
Code:
awk '
/function/      { print
                i++
                if ( $0 !~ "{") {
                        getline
                        print
                        }
                next
                }
(i)             { print
                if ( $0 ~ "{" )
                        i++
                if ( $0 ~ "}" )
                        i--
}
' file

This User Gave Thanks to nezabudka For This Post:
# 3  
Old 03-06-2019
Thank you for reply
# 4  
Old 03-06-2019
I think the request was "How to grep this function?", i.e. function A. So a small adaption of nezabudka's fine script might be suited for this:





Code:
awk '
/function *A/   {print
                 i++
                 while (! /{/)  {getline           # wait for "{" to occur reading as many lines as it takes
                                 print
                                }
                 next
                }

i               {print
                 if (/{/)       i++
                 if (/}/)       i--
                }
 ' file


Aside, @cmdcmd: "Not work Smilie" doesn't really help analyse your code and find possible errors. Post facts: error messages, exit codes, undesired vs. desired output
These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 03-06-2019
Quote:
Originally Posted by cmdcmd
Let's say I have a file written in C programming
Code:
...
int
function A (Param1, Param2)
{
....
}

How to grep this function?

I tried
Code:
grep -ER '^functionA(.*)/n{' filename > result.txt

Not work Smilie
Moderator's Comments:
Mod Comment Please use CODE tags on all sample input, output, and code segments; not just on shell code segments.
Hi cmdcmd,
Please be a little more verbose on what you are trying to do.

Are you trying to find the definition of the function named A in you C source file? If so, does it need to include the type of the value returned by the function? Do you want the complete function definition or just the part from function up to and including the first opening brace ({) character?

Are you trying to find all calls to the function named A in your C source file?

The code you provided can't work if you are trying to find the function definition in your C source file because the ERE you're using won't match the <space>s before and after the name of your function.

Assuming that you're using a GNU grep utility (which I don't have available to verify my suggestion) you might come closer to what you were trying to do with:
Code:
grep -EA1 '^function +A *(.*)' filename,c > result.txt

assuming that you're just looking for the text on a line starting with function for the function declaration for the function named A, the function prototype (not including the return code) fits on a single line, that the next line contains the opening brace for the function definition, and that you don't care about the return type of this function.

Just saying that something does not work isn't really helpful. We need to know what doesn't work; what diagnostic were produced, if any; what output it produced; what additional output you wanted; what output it produced that you did not want.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find C programming functions using shell script?

I want to find c function definition with pattern with shell script by checking condition for each line: data_type functionname(param_list){ .... } I knew cscope or ctag is usable for this task, but if there any ways to do without using them. I am thinking of checking line condition... (3 Replies)
Discussion started by: cmdcmd
3 Replies

2. UNIX for Advanced & Expert Users

Variable definition

Hi all, I'm bit new to the advanced bash shell scripting. When I'm looking at some of the existing code in my organization, got confused with a few variable definings. For ex: var1={1:-30} var2="abc def ghi" var3={xyz:-$var2} In above, 1st and last lines are confusing me.... (4 Replies)
Discussion started by: raghu.iv85
4 Replies

3. Shell Programming and Scripting

Grep problem from The Unix Programming Environment

Hi Here is the problem ( Exercise 3-3, Using The Shell of The Unix Programming Environment, Kerninghan, Pike, 3rd edition ): Predict what each of the following grep commands will do, and then verify your understanding. grep \$ grep \\$ grep \\\$ grep '\$' grep '\'$' grep \\ grep \\\\... (3 Replies)
Discussion started by: dum_dum20
3 Replies

4. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

5. Shell Programming and Scripting

help needed with using grep in shell programming

Hi, im working on an assignment (airline ticketing system). im kinda having problems with the search function. i want users to be able to search by departure time or by flight. however, my output displays the whole chunk of data instead of what they are supposedly searching for. appreciate if... (1 Reply)
Discussion started by: crazybean
1 Replies

6. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

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

8. Shell Programming and Scripting

Korn Shell programming (FTP, LOOPS, GREP)

Hello All, I have another Korn shell question. I am writing a script that will ftp a file from my production database to my test database. To this, I have to construct a loop that checks a specified folder for a file. If the file exists, I want it execute the ftp protocol and then exit. ... (2 Replies)
Discussion started by: jonesdk5
2 Replies

9. UNIX for Dummies Questions & Answers

Definition of $-

Could someone please direct me to a link that gives the definitions for each of the letters from the results of the $- environment variable? It would be nice to know what shell options each of the letters represents, but I am specifically looking for the shell option for 'c' (lowercase c). Thank... (12 Replies)
Discussion started by: sszd
12 Replies

10. UNIX for Dummies Questions & Answers

Carreer:Networking Programming in Unix (C programming Language)

Hello, I am trying to learn Networking Programming in C in unix enviorment. I want to know how good it is to become a network programmer. i am crazy about Network programming but i also want to opt for the best carreer options. Anybody experienced Network Programmer, please tell me is my... (5 Replies)
Discussion started by: vibhory2j
5 Replies
Login or Register to Ask a Question