The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Searching for files with certain string pattern intrigue UNIX for Dummies Questions & Answers 1 02-28-2008 01:28 PM
Regarding Searching Pattern sbasetty Shell Programming and Scripting 2 09-29-2006 02:49 PM
Searching all files that contain pattern ravi.sadani19 Shell Programming and Scripting 3 01-26-2006 08:43 AM
Pattern searching inside Variable - not looking at files Rediranch UNIX for Dummies Questions & Answers 3 06-07-2005 11:07 AM
pattern searching using C giannicello High Level Programming 1 04-16-2002 09:53 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-15-2007
Registered User
 

Join Date: Nov 2007
Posts: 3
Stumble this Post!
Pattern searching pattern in c files

I have a problem in searching a specific pattern in c files.

My requirement:

I have to find all the division operator in all cfiles.

The problem is, the multi line comments and single line comments will also have forward slash in it.

Even after avoiding these comments also, if both the division operator and the comments come in the same line, we are missing the division operator.

Please provide me the solution...

Give me some suggestions
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-16-2007
Registered User
 

Join Date: Dec 2005
Posts: 74
Stumble this Post!
finding / operator

sed -n '/\/\//d; /\/\*/d ; /\*\//d ;/\//p' hello.c

This would first remove all the // comments and then /* and then */
and then from the remaining search for /

Hope this may help

Regards,
RUV
Reply With Quote
  #3 (permalink)  
Old 11-16-2007
Moderator
 

Join Date: Feb 2007
Posts: 1,962
Stumble this Post!
Quote:
Code:
sed -n '/\/\//d; /\/\*/d ; /\*\//d ;/\//p' hello.c
This doesn't work if you have lines like:

Code:
a=30/6;	/* Comment */
a=10/2;	// comment
Try this:

Code:
sed -n '/[0-9a-zA-Z] *\/ *[0-9a-zA-Z]*/p' file.c
Regards
Reply With Quote
  #4 (permalink)  
Old 11-17-2007
Registered User
 

Join Date: Nov 2007
Posts: 3
Stumble this Post!
Hi franklin

What does this command actually does.....

I am not much aware of sed command.....

Please explain me clearly...


thanks
Reply With Quote
  #5 (permalink)  
Old 11-17-2007
Moderator
 

Join Date: Feb 2007
Posts: 1,962
Stumble this Post!
The -n option suppresses the automatic output of input lines (that's the default).
The p at the end tells sed to output the lines that matches.

Code:
[0-9a-zA-Z] *
This searches for lines at the left side of the division operator.
The characters within the brackets search for a numeric character or a variable and the space and asteric after the brackets for zero or more spaces.

Code:
\/
This is the escaped division operator.

Code:
 *[0-9a-zA-Z]*
At the right side of the division operator we search for zero or more preceded spaces and a numeric character or a variable.

Regards

Last edited by Franklin52; 11-17-2007 at 05:50 AM. Reason: typo
Reply With Quote
  #6 (permalink)  
Old 11-17-2007
Registered User
 

Join Date: Nov 2007
Posts: 3
Stumble this Post!
Pattern searching pattern in c files

Thank you very much.



This solution is giving all the files which is having division operator.

And at the same time giving some files which dont have division operator.

xx.c : x=y; /*** dfldkfdk **/
xx.c : return; /** dfkdfdkd **/
xx.c : /** return some value **/

can you give me any suggestion on this??

thanks & regards,
murthy.
Reply With Quote
  #7 (permalink)  
Old 11-17-2007
Moderator
 

Join Date: Feb 2007
Posts: 1,962
Stumble this Post!
I placed those lines in a file and it works fine for me, I don't get those lines as output.
Have you typed the command exactly? Note the spaces before the asterics.

If you forgot them you didn't understand my explanation above.

Regards
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0