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

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

This doesn't work if you have lines like:

a=30/6;	/* Comment */
a=10/2;	// comment

Try this:

sed -n '/[0-9a-zA-Z] *\/ *[0-9a-zA-Z]*/p' file.c

Regards

What does this command actually does.....

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

Please explain me clearly...

thanks

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.

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

\/

This is the escaped division operator.

 *[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

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

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

Regards