9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] Sam / text search query
@ 2002-06-18 21:06 Andrew Simmons
  2002-06-19 15:37 ` Douglas A. Gwyn
  0 siblings, 1 reply; 11+ messages in thread
From: Andrew Simmons @ 2002-06-18 21:06 UTC (permalink / raw)
  To: 9fans

> this is the easiest way
>
>	,x g/sprintf/ .,/.*msgprint.*\n/ p
>
>it seems easy to me, almost a transcription of what you're asking to
>do.
>
>if you have multiple sprintfs before msgprint, though, you might
>prefer this:
>
>	,x g/msgprint/ ?.*sprintf.*\n?,.p
>
>subtler but maybe more correct; your specification was ambiguous.

Brilliant, thanks. Your second method is exactly what I wanted, your
first the best I was expecting. Would that it seemed as easy to me,
but my brain locks up and turns to custard when faced with regular
expressions beyond a minimal level of complexity.




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

* Re: [9fans] Sam / text search query
  2002-06-18 21:06 [9fans] Sam / text search query Andrew Simmons
@ 2002-06-19 15:37 ` Douglas A. Gwyn
  0 siblings, 0 replies; 11+ messages in thread
From: Douglas A. Gwyn @ 2002-06-19 15:37 UTC (permalink / raw)
  To: 9fans

Andrew Simmons wrote:
> ... my brain locks up and turns to custard when faced with
> regular expressions beyond a minimal level of complexity.

The best approach for developing structural r.e.s seems to be to think
of "layers of context", first establishing the crudest approximate
context (location), then adding more r.e.s to successively refine the
context until precisely the desired region is selected.  Then invoke
the action, not forgetting that the "s" command supports \(...\) etc.
which is good for rearranging the order of pieces of the selection.
Also use sam's "u" (undo) while successively refining context to back
out any attempts that don't work as you hoped.

The following was extracted from a typical MUVES project package
Makefile; it creates source listings that include usage counts in front
of each line, great for checking test coverage:

# CAUTION: The following procedure requires Rob Pike's "sam" text
editor.
$(PCFILES):     $(HFILES) $(CFILES) $(OBJS) $(TEST).o   # $(CFILES) is
overkill
        @(      c=`echo $@ | sed 's/^P\.//'`; \
                o=`basename $$c .c`.o; \
                $(CTRACE) $$c > T.$$c; \
                $(CC) -c T.$$c; \
                mv $$o B.$$o && mv T.$$o $$o; \
                $(CC) $(LDFLAGS) -o $(TEST) $(TEST).o $(OBJS) $(TLIBES);
\
                ./$(TEST) 2> X.$$c; \
                rm -f $(TEST) T.$$o && mv B.$$o $$o; \
                grep '^ *[1-9][0-9]* ' < X.$$c \
                | sed 's/^ *\([1-9][0-9]*\).*/\1/' \
                | sort > L.$$c; \
                rm -f X.$$c; \
                ( echo ',x/t_ct_\("\\\\n *[1-9][0-9]* /{'; \
                  echo 'x/[1-9][0-9]*/p\n/\\n/p'; \
                  echo '}\nq\n' \
                ) | sam -d T.$$c | sort -u > A.$$c; \
                ( echo f P.$$c; \
                  echo ',x/^/c/ /'; \
                  uniq -c L.$$c \
                  | sed 's;^\( *[1-9][0-9]*\) \([1-9][0-9]*\);\2i/\1/;';
\
                  uniq L.$$c | comm -13 - A.$$c | sed 's;.*;&i/   0/;';
\
                  echo 'w\nq\n' \
                ) | sam -d $$c; \
                rm -f [ALT].$$c \
        )


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

* Re: [9fans] Sam / text search query
  2002-06-20 18:58 David Gordon Hogan
@ 2002-06-21 15:00 ` Douglas A. Gwyn
  0 siblings, 0 replies; 11+ messages in thread
From: Douglas A. Gwyn @ 2002-06-21 15:00 UTC (permalink / raw)
  To: 9fans

David Gordon Hogan wrote:
> I much prefer sam's behaviour (which is actually Plan 9
> regexp's behaviour in general).  You can escape anything
> with "\".

Yeah, but that's backward.  I don't want every second text
character I type to have to be \ just to make sure it's not
taken as a metacharacter.

I omitted your discussion of egrep since I never mentioned
egrep and certainly don't think it did this right.


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

* Re: [9fans] Sam / text search query
@ 2002-06-20 18:58 David Gordon Hogan
  2002-06-21 15:00 ` Douglas A. Gwyn
  0 siblings, 1 reply; 11+ messages in thread
From: David Gordon Hogan @ 2002-06-20 18:58 UTC (permalink / raw)
  To: 9fans

>> > not forgetting that the "s" command supports \(...\) etc.
>> ...while remembering that, as with all regexps in plan 9,
>> the old ed-like \( and \) become egrep-like ( and ),
>
> Yeah, actually that's a pet peeve.

I'm a little confused.  Are you peeved at egrep's behaviour,
sam's regexp behaviour, or both?

I much prefer sam's behaviour (which is actually Plan 9
regexp's behaviour in general).  You can escape anything
with "\".  In egrep, both '(' and '\(' are (different)
metacharacters.



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

* Re: [9fans] Sam / text search query
@ 2002-06-20 16:21 rog
  0 siblings, 0 replies; 11+ messages in thread
From: rog @ 2002-06-20 16:21 UTC (permalink / raw)
  To: 9fans

from /sys/doc/sam.ms:

: Egrep expressions are arguably too complicated for an interactive
: editor -- certainly it would make sense if all the special characters
: were two-character sequences, so that most of the punctuation
: characters wouldn't have peculiar meanings -- but for an interesting
: command language, full regular expressions are necessary, and egrep
: defines the full regular expression syntax for UNIX programs.  Also,
: it seemed superfluous to define a new syntax, since various UNIX
: programs (ed, egrep and vi) define too many already.

i have to say i wouldn't mind a new regexp syntax with fewer metachars.



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

* Re: [9fans] Sam / text search query
  2002-06-19 16:08 rog
@ 2002-06-20 15:47 ` Douglas A. Gwyn
  0 siblings, 0 replies; 11+ messages in thread
From: Douglas A. Gwyn @ 2002-06-20 15:47 UTC (permalink / raw)
  To: 9fans

rog@vitanuova.com wrote:
> > not forgetting that the "s" command supports \(...\) etc.
> ...while remembering that, as with all regexps in plan 9,
> the old ed-like \( and \) become egrep-like ( and ),

Yeah, actually that's a pet peeve.
I much prefer a general-purpose text editor to have but a
single escape character (which can escape itself); otherwise
one ends up having to escape normal text characters, *and*
there is no convenient way to expand the editor special-
purpose syntax without breaking existing editing scripts.
Somebody (perhaps me) should write a good paper about
special characters and escape sequences.


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

* Re: [9fans] Sam / text search query
@ 2002-06-19 16:08 rog
  2002-06-20 15:47 ` Douglas A. Gwyn
  0 siblings, 1 reply; 11+ messages in thread
From: rog @ 2002-06-19 16:08 UTC (permalink / raw)
  To: 9fans

> not forgetting that the "s" command supports \(...\) etc.

...while remembering that, as with all regexps in plan 9,
the old ed-like \( and \) become egrep-like ( and ),
so the old:

	s/\([a-z]*\) \([a-z]*\)/\2 \1/

becomes:

	s/([a-z]*) ([a-z]*)/\2 \1/



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

* RE: [9fans] Sam / text search query
@ 2002-06-18 17:31 Gerry Tomlinson
  0 siblings, 0 replies; 11+ messages in thread
From: Gerry Tomlinson @ 2002-06-18 17:31 UTC (permalink / raw)
  To: 9fans


>> 
> Drifting ever more OT, has anyone had problems running Sam under 
> Windows 2000? It seems to start happily enough, but then gets angry 
> and refuses to respond to anything I type in the command window, 
> before finally locking up under circumstances I have not so far 
> managed to identify.
> 


i get similar behaviour if i have cleverkeys running, a utility which
intercepts keystrokes in all applications,

	gerry

---
gerry.tomlinson@newcastle.ac.uk
computing officer
department of computing Science, university of newcastle, tel: +44 191
222 8139
newcastle upon Tyne, ne1 7ru, united kingdom.             fax: +44 191
222 8232
 





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

* Re: [9fans] Sam / text search query
  2002-06-18  5:03 Andrew Simmons
@ 2002-06-18 15:03 ` Douglas A. Gwyn
  0 siblings, 0 replies; 11+ messages in thread
From: Douglas A. Gwyn @ 2002-06-18 15:03 UTC (permalink / raw)
  To: 9fans

Andrew Simmons wrote:
> ... has anyone had problems running Sam under Windows 2000?

I've recently seen it corrupt its snarf buffer.


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

* Re: [9fans] Sam / text search query
@ 2002-06-18  5:07 rob pike, esq.
  0 siblings, 0 replies; 11+ messages in thread
From: rob pike, esq. @ 2002-06-18  5:07 UTC (permalink / raw)
  To: 9fans

this is the easiest way

	,x g/sprintf/ .,/.*msgprint.*\n/ p

it seems easy to me, almost a transcription of what you're asking to do.

if you have multiple sprintfs before msgprint, though, you might
prefer this:

	,x g/msgprint/ ?.*sprintf.*\n?,.p

subtler but maybe more correct; your specification was ambiguous.

-rob



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

* [9fans] Sam / text search query
@ 2002-06-18  5:03 Andrew Simmons
  2002-06-18 15:03 ` Douglas A. Gwyn
  0 siblings, 1 reply; 11+ messages in thread
From: Andrew Simmons @ 2002-06-18  5:03 UTC (permalink / raw)
  To: 9fans

Sorry if this is slightly OT here, but I thought it was the likeliest
place to get an answer. I have a problem I was hoping to use Sam for,
partly so that I have an incentive to get better at using the command
language, but I'm too thick to work out how to do what I want.

I have a large number of files, and for each one I need to print out
all sequences of adjacent lines such that the first line in the
sequence contains "sprintf" and the last line contains "msgprint".
Ideally there should be no other occurences of "sprintf" in the
sequence of lines, but I can live with it if there are. My naïve
attempt to do this resulted in the selection of the sequence of lines
from the first "sprintf" to the final "msgprint". Can some kind soul
point out how to do what I want, or failing that suggest an
alternative tool - I could write a program, but feel it shouldn't be
necessary.

Drifting ever more OT, has anyone had problems running Sam under
Windows 2000? It seems to start happily enough, but then gets angry
and refuses to respond to anything I type in the command window,
before finally locking up under circumstances I have not so far
managed to identify.


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

end of thread, other threads:[~2002-06-21 15:00 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-06-18 21:06 [9fans] Sam / text search query Andrew Simmons
2002-06-19 15:37 ` Douglas A. Gwyn
  -- strict thread matches above, loose matches on Subject: below --
2002-06-20 18:58 David Gordon Hogan
2002-06-21 15:00 ` Douglas A. Gwyn
2002-06-20 16:21 rog
2002-06-19 16:08 rog
2002-06-20 15:47 ` Douglas A. Gwyn
2002-06-18 17:31 Gerry Tomlinson
2002-06-18  5:07 rob pike, esq.
2002-06-18  5:03 Andrew Simmons
2002-06-18 15:03 ` Douglas A. Gwyn

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 NNTP newsgroup(s).