unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* grep can help?
@ 2004-04-21 14:01 FCC
  2004-04-21 15:09 ` Glenn Morris
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: FCC @ 2004-04-21 14:01 UTC (permalink / raw)


Hello all,

Here is what I am trying to do: I have fortran90 source files full of 
comments everywhere. A comment starts with ! and continues until the end 
of line, and it can start in the middle of a line after some code that 
gets compiled.

Initially I was trying to use isearch for a regexp but only to search 
for those occurences which are not in comments (or which do not appear 
after the character !). After spending some time with Google, it turned 
out that this is not possible because one cannot "invert a match", which 
is what is required here.

Then came the idea to use grep. Now, grep --regexp="\B!.*" 
--only-matching --no-filename efe2.f90 successfully displays all 
comments in the file efe2.f90, regardless of whether thay appear by 
themselves on a line or they share the same line with legitimate code, 
thanks to the option --only-matching. Now, what I need is exact inverse, 
so I use: grep --invert-match --regexp="\B!.*" --only-matching 
--no-filename EFEDriver2.f90. I was expecting an output equal to the 
efe2.f90 without all the comments. But I get nothing, only a comment 
saying that there were matches.

Any idea how I can do that?

Thanks,

FCC

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: grep can help?
  2004-04-21 14:01 grep can help? FCC
@ 2004-04-21 15:09 ` Glenn Morris
  2004-04-21 16:33   ` FCC
  2004-04-21 16:52 ` LEE Sau Dan
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Glenn Morris @ 2004-04-21 15:09 UTC (permalink / raw)


FCC wrote:

> Initially I was trying to use isearch for a regexp but only to
> search for those occurences which are not in comments (or which do
> not appear after the character !). After spending some time with
> Google, it turned out that this is not possible because one cannot
> "invert a match", which is what is required here.

M-x occur ^[^!C-qC-j]*regexp

ought to work (the C-qC-j bit gives you a newline).
It will be fooled by instances of your desired regexp occurring after
a string containing !, but I imagine that's unlikely. If you needed
to deal with that, you could cook up some elisp using f90-in-comment.

> Then came the idea to use grep.

I guess comp.unix.shell or somesuch place is the right place for grep
questions.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: grep can help?
  2004-04-21 15:09 ` Glenn Morris
@ 2004-04-21 16:33   ` FCC
  0 siblings, 0 replies; 9+ messages in thread
From: FCC @ 2004-04-21 16:33 UTC (permalink / raw)


Glenn Morris wrote:

> FCC wrote:
> 
> 
>>Initially I was trying to use isearch for a regexp but only to
>>search for those occurences which are not in comments (or which do
>>not appear after the character !). After spending some time with
>>Google, it turned out that this is not possible because one cannot
>>"invert a match", which is what is required here.
> 
> 
> M-x occur ^[^!C-qC-j]*regexp
> 
> ought to work (the C-qC-j bit gives you a newline).
> It will be fooled by instances of your desired regexp occurring after
> a string containing !, but I imagine that's unlikely. If you needed
> to deal with that, you could cook up some elisp using f90-in-comment.

I did try M-x occur ^[^!C-qC-j]*stress and the result was `no match'.

Thanks anyway,

FCC.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: grep can help?
  2004-04-21 14:01 grep can help? FCC
  2004-04-21 15:09 ` Glenn Morris
@ 2004-04-21 16:52 ` LEE Sau Dan
  2004-04-21 17:59 ` FCC
  2004-04-22 15:51 ` Johan Bockgård
  3 siblings, 0 replies; 9+ messages in thread
From: LEE Sau Dan @ 2004-04-21 16:52 UTC (permalink / raw)


>>>>> "FCC" == FCC  <fcc509@netscape.net> writes:

    FCC> Hello all, Here is what I am trying to do: I have fortran90
    FCC> source files full of comments everywhere. A comment starts
    FCC> with ! and continues until the end of line, and it can start
    FCC> in the middle of a line after some code that gets compiled.

...


    FCC> Then came the idea to use grep. Now, grep --regexp="\B!.*"
    FCC> --only-matching --no-filename efe2.f90 successfully displays
    FCC> all comments in the file efe2.f90, regardless of whether thay
    FCC> appear by themselves on a line or they share the same line
    FCC> with legitimate code, thanks to the option
    FCC> --only-matching. Now, what I need is exact inverse, so I use:
    FCC> grep --invert-match --regexp="\B!.*" --only-matching
    FCC> --no-filename EFEDriver2.f90. I was expecting an output equal
    FCC> to the efe2.f90 without all the comments. But I get nothing,
    FCC> only a comment saying that there were matches.

sed 's/!.*$//' foobar.f90 > foobar-sans-comments.f90



-- 
Lee Sau Dan                     李守敦(Big5)                    ~{@nJX6X~}(HZ) 

E-mail: danlee@informatik.uni-freiburg.de
Home page: http://www.informatik.uni-freiburg.de/~danlee

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: grep can help?
  2004-04-21 14:01 grep can help? FCC
  2004-04-21 15:09 ` Glenn Morris
  2004-04-21 16:52 ` LEE Sau Dan
@ 2004-04-21 17:59 ` FCC
  2004-04-22 10:27   ` Albert Reiner
  2004-04-22 15:51 ` Johan Bockgård
  3 siblings, 1 reply; 9+ messages in thread
From: FCC @ 2004-04-21 17:59 UTC (permalink / raw)


FCC wrote:

> Hello all,
> 
> Here is what I am trying to do: I have fortran90 source files full of 
> comments everywhere. A comment starts with ! and continues until the end 
> of line, and it can start in the middle of a line after some code that 
> gets compiled.
> 
> Initially I was trying to use isearch for a regexp but only to search 
> for those occurences which are not in comments (or which do not appear 
> after the character !). After spending some time with Google, it turned 
> out that this is not possible because one cannot "invert a match", which 
> is what is required here.

OK, I think I solved my problem: replace-regexp "!.*" " " and all the 
comments are gone, and the line numbers are unchanged. Now, the 
resulting file can be saved under a different file name, or the buffer 
could be renamed and the intended regexp search can be performed, either 
with occur or isearch-regexp.

Thanks all anyway.

Best,

FCC.

PS. If there is a better solution, I would like to hear it.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: grep can help?
  2004-04-21 17:59 ` FCC
@ 2004-04-22 10:27   ` Albert Reiner
  2004-04-22 11:23     ` FCC
  0 siblings, 1 reply; 9+ messages in thread
From: Albert Reiner @ 2004-04-22 10:27 UTC (permalink / raw)


FCC <fcc509@netscape.net> writes:

> > Hello all,
> > Here is what I am trying to do: I have fortran90 source files full
> > of comments everywhere. A comment starts with ! and continues until
...
> OK, I think I solved my problem: replace-regexp "!.*" " " and all the
> comments are gone, and the line numbers are unchanged. Now, the
...

Just to point out the obvious: This does not take into account
exclamation marks in string constants, and the standard also allows
you to write F90 in fixed format in which case you might use ! in
column 5 (?) for continuation lines.  Of course it depends on your
code whether this is a problem; a general solution would need to parse
F90 in both formats.

Albert.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: grep can help?
  2004-04-22 10:27   ` Albert Reiner
@ 2004-04-22 11:23     ` FCC
  2004-04-22 14:14       ` Barry Margolin
  0 siblings, 1 reply; 9+ messages in thread
From: FCC @ 2004-04-22 11:23 UTC (permalink / raw)




Albert Reiner wrote:
> Just to point out the obvious: This does not take into account
> exclamation marks in string constants, and the standard also allows
> you to write F90 in fixed format in which case you might use ! in
> column 5 (?) for continuation lines.  Of course it depends on your
> code whether this is a problem; a general solution would need to parse
> F90 in both formats.
> 
> Albert.

I see. I think your 2nd point is not an example of good programming, so 
I would not worry about it. But your 1st point needs to be taken care 
of: replace-regexp "\B!.*" " " and all the comments will be gone, 
respecting the ! inside the strings, as long as they don't come with a 
preceding whitespace. But if they do, this is again not a good practice, 
normally for example you write "Nice to see you!", but not "Nice to see 
you !".

FCC.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: grep can help?
  2004-04-22 11:23     ` FCC
@ 2004-04-22 14:14       ` Barry Margolin
  0 siblings, 0 replies; 9+ messages in thread
From: Barry Margolin @ 2004-04-22 14:14 UTC (permalink / raw)


In article <c68a4v$b39$1@defalla.upc.es>, FCC <fcc509@netscape.net> 
wrote:

> Albert Reiner wrote:
> > Just to point out the obvious: This does not take into account
> > exclamation marks in string constants, and the standard also allows
> > you to write F90 in fixed format in which case you might use ! in
> > column 5 (?) for continuation lines.  Of course it depends on your
> > code whether this is a problem; a general solution would need to parse
> > F90 in both formats.
> > 
> > Albert.
> 
> I see. I think your 2nd point is not an example of good programming, so 
> I would not worry about it. But your 1st point needs to be taken care 
> of: replace-regexp "\B!.*" " " and all the comments will be gone, 
> respecting the ! inside the strings, as long as they don't come with a 
> preceding whitespace. But if they do, this is again not a good practice, 
> normally for example you write "Nice to see you!", but not "Nice to see 
> you !".
> 
> FCC.

That's true if the strings contain normal prose text.  But suppose you 
were writing a program that adds comments to programs.  It's likely to 
contain " ! " as a string.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: grep can help?
  2004-04-21 14:01 grep can help? FCC
                   ` (2 preceding siblings ...)
  2004-04-21 17:59 ` FCC
@ 2004-04-22 15:51 ` Johan Bockgård
  3 siblings, 0 replies; 9+ messages in thread
From: Johan Bockgård @ 2004-04-22 15:51 UTC (permalink / raw)


FCC <fcc509@netscape.net> writes:

> Here is what I am trying to do: I have fortran90 source files full
> of comments everywhere. A comment starts with ! and continues until
> the end of line, and it can start in the middle of a line after some
> code that gets compiled.

,----[ C-h f comment-kill RET ]
| comment-kill is an interactive compiled Lisp function in `newcomment'.
| (comment-kill ARG)
| 
| Kill the comment on this line, if any. With prefix ARG, kill comments
| on that many lines starting with this one.
`----

-- 
Johan Bockgård

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2004-04-22 15:51 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-04-21 14:01 grep can help? FCC
2004-04-21 15:09 ` Glenn Morris
2004-04-21 16:33   ` FCC
2004-04-21 16:52 ` LEE Sau Dan
2004-04-21 17:59 ` FCC
2004-04-22 10:27   ` Albert Reiner
2004-04-22 11:23     ` FCC
2004-04-22 14:14       ` Barry Margolin
2004-04-22 15:51 ` Johan Bockgård

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).