For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2005 > Negate of a group









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Negate of a group
vinod

2005-11-18, 9:55 pm

Hi All,
I am trying to ignore all the lines C++ which start with comment i.e.
//
but the comment may be start with <space><space>... //

So i am trying to write some regex which is going to be negate of
(\s*\/\/)
which will match all the line not starting with comment
Any idea how to go about it.

Thanks for help in advance
Vinod

William James

2005-11-18, 9:55 pm

vinod wrote:
> Hi All,
> I am trying to ignore all the lines C++ which start with comment i.e.
> //
> but the comment may be start with <space><space>... //
>
> So i am trying to write some regex which is going to be negate of
> (\s*\/\/)
> which will match all the line not starting with comment
> Any idea how to go about it.
>
> Thanks for help in advance
> Vinod


/^[ \t]*\/\// { next }

Ed Morton

2005-11-18, 9:55 pm



William James wrote:
> vinod wrote:
>
>
>
> /^[ \t]*\/\// { next }
>


It's pretty common to put control-Ls in C/C++ source files for
formatting output to the printers, e.g. to force a page break at the
start of blocks of code within a file related to some functionality, so
you really need to go beyond just " " and "\t". Try this:

/^[[:space:]]*\/\// { next }

You may also want to foget the whole thing and just use the compiler to
strip out comments (e.g. "gcc -E" with some additional shell tweaks to
avoid preporcessor directive expansion) so you don't have to worry about
C-style comments either. If you're interested in that, see recent
postings on the subject to comp.unix.shell and/or follow up over there.

Regards,

Ed.
vinod

2005-11-18, 9:55 pm

Thaks all for ur reply
Sorry but i want to ignore all the comment lines whtever regex u guys
provided will find comment line but i exactly want negation of that
Like putting -v commnd on grep or ! command in AWK but I can not use
such commands as i am using some regex API.
Something like
!(/^[[:space:]]*\/\// ) i.e. line not starting with i want ! to
replace with something not sure what :(
Ed,
This regex is only part of whole regex so I can not use the gcc option
to strip the comments.

Thanks,
Vinod

Ed Morton

2005-11-18, 9:55 pm

vinod wrote:
> Thaks all for ur reply
> Sorry but i want to ignore all the comment lines whtever regex u guys
> provided will find comment line but i exactly want negation of that


Are you sure about that? Did you try it? You might be surprised.

> Like putting -v commnd on grep or ! command in AWK but I can not use
> such commands as i am using some regex API.
> Something like
> !(/^[[:space:]]*\/\// ) i.e. line not starting with i want ! to
> replace with something not sure what :(
> Ed,
> This regex is only part of whole regex so I can not use the gcc option
> to strip the comments.


If whet William and I suggested really doesn't work, then why not just
tell us (succintly) what you're actually doing including a small sample
input and output then maybe we could help you.

Ed.
Janis Papanagnou

2005-11-18, 9:55 pm

vinod wrote:
> Thaks all for ur reply
> Sorry but i want to ignore all the comment lines whtever regex u guys
> provided will find comment line but i exactly want negation of that
> Like putting -v commnd on grep or ! command in AWK but I can not use
> such commands as i am using some regex API.
> Something like
> !(/^[[:space:]]*\/\// ) i.e. line not starting with i want ! to
> replace with something not sure what :(
> Ed,
> This regex is only part of whole regex so I can not use the gcc option
> to strip the comments.
>
> Thanks,
> Vinod
>


Give context to what you are replying to! You won't likely get much
help if you force people to do even the simplest work for you.

[ Quote re-inserted ]

Ed suggested to you to skip the comment lines:[color=darkred]

Then the complete awk program for your task is

/^[[:space:]]*\/\// { next } 1


Janis
vinod

2005-11-18, 9:55 pm

Thanks Ed,
Here is kind of sample input
1)// Search <something>
2)if(<something> )
3){
4) do <something>;
5)}

6) //Comment <something>
7)int <something>;

i have written regex to find the <something>
now i want to get only non commented C++ lines for <something>

so i want line # 2, 4, 7 as output but not 1, 6 in above sample


Thanks,
Vinod

Ed Morton

2005-11-19, 3:55 am

vinod wrote:
> Thanks Ed,
> Here is kind of sample input
> 1)// Search <something>
> 2)if(<something> )
> 3){
> 4) do <something>;
> 5)}
>
> 6) //Comment <something>
> 7)int <something>;
>
> i have written regex to find the <something>
> now i want to get only non commented C++ lines for <something>
>
> so i want line # 2, 4, 7 as output but not 1, 6 in above sample
>


Please read this before posting again:

http://cfaj.freeshell.org/google/

Now, to your problem:

a) What about lines 3, 5, and the un-numbered blank line between 5 and 6?

b) I still don't see why the solution you were already given wouldn't
work for just deleting the comment lines. If the above is in file
"file", run this:

awk '/^[[:space:]]*\/\//{next}1' file

and explain what is unsatisfactory about it's output.

Ed.
Ulrich M. Schwarz

2005-11-19, 3:55 am

"vinod" <vinod.patil1@gmail.com> writes:

> Thaks all for ur reply
> Sorry but i want to ignore all the comment lines whtever regex u guys
> provided will find comment line but i exactly want negation of that
> Like putting -v commnd on grep or ! command in AWK but I can not use
> such commands as i am using some regex API.
> Something like
> !(/^[[:space:]]*\/\// ) i.e. line not starting with i want ! to
> replace with something not sure what :(


Find yourself an introductory text on regular languages and finite
state automata. Find the bit where they show that RE and FSA accept
the same languages[0]. Translate the RE you have for _matching_
comments to a FSA. It's easy to find the complement language when you
have a FSA, right? Then, you translate the new FSA back to a RE. Then,
you realize that this is impossible to maintain or verify, so you'll
need a program that does it for you. At this point, one usually
rewrites the local program logic to not need the complement of REs.

Ulrich

[0] Caveat: I don't think there's a POSIX RE that never matches.
--=20
Sechs auf f=FCnftem Platz bedeutet:
Im Feld ist ein Wild. Es ist f=F6rdernd, es zu fangen.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com