9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] Non-parallel loop in Sam
@ 2008-02-06 16:57 Hongzheng Wang
  2008-02-07  9:55 ` Douglas A. Gwyn
  0 siblings, 1 reply; 7+ messages in thread
From: Hongzheng Wang @ 2008-02-06 16:57 UTC (permalink / raw)
  To: 9fans

Hi all,

I'm new to Plan9 and also this mailing list.

After some experience with Sam's structral regexp, I become quite
interested in this editor command.  And I also noticed that the loop
and condition commands of Sam, say x, y, g, v etc, are somewhat
different from the commands of ed.  For example, all changes in a x
command execution occure simultaneously, rather than the model used by
ed where each change would  be made at once even in a g command
execution.  As a result, some tips in ed, such as ',g/^/m0' to invert
all lines of a file, would be invalid.  My question is, how to
impliment non-parallel loop/condition commands in Sam?  In another
word, how to do things, such as inverse all lines, in Sam?

Thanks.

--
HZ


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

* Re: [9fans] Non-parallel loop in Sam
  2008-02-06 16:57 [9fans] Non-parallel loop in Sam Hongzheng Wang
@ 2008-02-07  9:55 ` Douglas A. Gwyn
  2008-02-07 11:33   ` Hongzheng Wang
  2008-02-07 11:43   ` Rob Pike
  0 siblings, 2 replies; 7+ messages in thread
From: Douglas A. Gwyn @ 2008-02-07  9:55 UTC (permalink / raw)
  To: 9fans

> My question is, how to
> impliment non-parallel loop/condition commands in Sam?

As you noticed, it's a different model from ed, sed, etc.
In sam, you can specify sequential edits on separate lines,
and a collection of lines can be grouped into a single action
by surrounding the lines with braces { }.

> In another
> word, how to do things, such as inverse all lines, in Sam

Don't forget that you have a lot of text-oriented tools at
your disposal in any Unix or Plan 9 environment.  To
reverse the order of lines within a file already opened by
sam, I would simply enter the following sam commands:
	,| nl | sort -rn
	,x/^ +[0-9]+	/d
(on Solaris; maybe a slight change would be needed on Plan 9).
This would be better packaged as a shell script, using sed
rather than sam for the final number-stripping operation.
You could then merely invoke that script for whatever "dot"
region is selected in sam:
	| reverse	# the script name
It is nice to build up a little collection of useful
editing scripts.  Sometimes it is useful to enter nroff
source and pipe it through nroff for automatic formatting:
	unformatted text
	.pl 12p\"prevent spacing to end of page afterward
	.ll 2i
	.tl 'le'ctr'ri'
	.ce
	centered title
	formatted text
	unformatted text
Highlight (set dot to) all but the "unformatted text" and
enter the same command
	| nroff -Tlp
(on Solaris; for Plan 9 -Tlp is probably different).  The
-ms or other nroff macro package could be used, as desired.


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

* Re: [9fans] Non-parallel loop in Sam
  2008-02-07  9:55 ` Douglas A. Gwyn
@ 2008-02-07 11:33   ` Hongzheng Wang
  2008-02-07 11:38     ` sqweek
  2008-02-07 11:42     ` y i y u s
  2008-02-07 11:43   ` Rob Pike
  1 sibling, 2 replies; 7+ messages in thread
From: Hongzheng Wang @ 2008-02-07 11:33 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Thank you for your advice.

> Don't forget that you have a lot of text-oriented tools at
> your disposal in any Unix or Plan 9 environment.  To
> reverse the order of lines within a file already opened by
> sam, I would simply enter the following sam commands:
>         ,| nl | sort -rn
>         ,x/^ +[0-9]+    /d
> (on Solaris; maybe a slight change would be needed on Plan 9).
> This would be better packaged as a shell script, using sed
> rather than sam for the final number-stripping operation.
> You could then merely invoke that script for whatever "dot"
> region is selected in sam:
>         | reverse       # the script name
> It is nice to build up a little collection of useful
> editing scripts.  Sometimes it is useful to enter nroff
> source and pipe it through nroff for automatic formatting:
>         unformatted text
>         .pl 12p\"prevent spacing to end of page afterward
>         .ll 2i
>         .tl 'le'ctr'ri'
>         .ce
>         centered title
>         formatted text
>         unformatted text
> Highlight (set dot to) all but the "unformatted text" and
> enter the same command
>         | nroff -Tlp
> (on Solaris; for Plan 9 -Tlp is probably different).  The
> -ms or other nroff macro package could be used, as desired.

I see.  The key point is to use external utilities, rather than depend
only on Sam itself.  And not only the scripts you provided here but
also some specific utilies, say tac, can be applied in my question.

Thanks.


--
HZ


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

* Re: [9fans] Non-parallel loop in Sam
  2008-02-07 11:33   ` Hongzheng Wang
@ 2008-02-07 11:38     ` sqweek
  2008-02-07 11:42     ` y i y u s
  1 sibling, 0 replies; 7+ messages in thread
From: sqweek @ 2008-02-07 11:38 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On Feb 7, 2008 8:33 PM, Hongzheng Wang <wanghz@gmail.com> wrote:
> > To
> > reverse the order of lines within a file already opened by
> > sam, I would simply enter the following sam commands:
> >         ,| nl | sort -rn
> >         ,x/^ +[0-9]+    /d
>
> tac

 Note that plan 9 has tail -r to reverse lines.
-sqweek


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

* Re: [9fans] Non-parallel loop in Sam
  2008-02-07 11:33   ` Hongzheng Wang
  2008-02-07 11:38     ` sqweek
@ 2008-02-07 11:42     ` y i y u s
  1 sibling, 0 replies; 7+ messages in thread
From: y i y u s @ 2008-02-07 11:42 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

2008/2/7, Hongzheng Wang <wanghz@gmail.com>:
> I see.  The key point is to use external utilities, rather than depend
> only on Sam itself.  And not only the scripts you provided here but
> also some specific utilies, say tac, can be applied in my question.
>

You could also use sed (I have never tried this in plan9):
sed -e '1!G;h;$!d'

--


- yiyus || JGL .


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

* Re: [9fans] Non-parallel loop in Sam
  2008-02-07  9:55 ` Douglas A. Gwyn
  2008-02-07 11:33   ` Hongzheng Wang
@ 2008-02-07 11:43   ` Rob Pike
  2008-02-07 12:37     ` Hongzheng Wang
  1 sibling, 1 reply; 7+ messages in thread
From: Rob Pike @ 2008-02-07 11:43 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

 , | tail -r
is simpler.

-rob

On Thu, Feb 7, 2008 at 8:55 PM, Douglas A. Gwyn <DAGwyn@null.net> wrote:
> > My question is, how to
>  > impliment non-parallel loop/condition commands in Sam?
>
>  As you noticed, it's a different model from ed, sed, etc.
>  In sam, you can specify sequential edits on separate lines,
>  and a collection of lines can be grouped into a single action
>  by surrounding the lines with braces { }.
>
>
>  > In another
>  > word, how to do things, such as inverse all lines, in Sam
>
>  Don't forget that you have a lot of text-oriented tools at
>  your disposal in any Unix or Plan 9 environment.  To
>  reverse the order of lines within a file already opened by
>  sam, I would simply enter the following sam commands:
>         ,| nl | sort -rn
>         ,x/^ +[0-9]+    /d
>  (on Solaris; maybe a slight change would be needed on Plan 9).
>  This would be better packaged as a shell script, using sed
>  rather than sam for the final number-stripping operation.
>  You could then merely invoke that script for whatever "dot"
>  region is selected in sam:
>         | reverse       # the script name
>  It is nice to build up a little collection of useful
>  editing scripts.  Sometimes it is useful to enter nroff
>  source and pipe it through nroff for automatic formatting:
>         unformatted text
>         .pl 12p\"prevent spacing to end of page afterward
>         .ll 2i
>         .tl 'le'ctr'ri'
>         .ce
>         centered title
>         formatted text
>         unformatted text
>  Highlight (set dot to) all but the "unformatted text" and
>  enter the same command
>         | nroff -Tlp
>  (on Solaris; for Plan 9 -Tlp is probably different).  The
>  -ms or other nroff macro package could be used, as desired.
>


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

* Re: [9fans] Non-parallel loop in Sam
  2008-02-07 11:43   ` Rob Pike
@ 2008-02-07 12:37     ` Hongzheng Wang
  0 siblings, 0 replies; 7+ messages in thread
From: Hongzheng Wang @ 2008-02-07 12:37 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Thank you all very much.

I think I learned many interesting and useful tips, e.g. the -r option
of tail shipped by Plan9 during this conversation.  And I still need
some time and experiences training myself to get used to the habits
and idioms of Sam.

--
HZ


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

end of thread, other threads:[~2008-02-07 12:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-06 16:57 [9fans] Non-parallel loop in Sam Hongzheng Wang
2008-02-07  9:55 ` Douglas A. Gwyn
2008-02-07 11:33   ` Hongzheng Wang
2008-02-07 11:38     ` sqweek
2008-02-07 11:42     ` y i y u s
2008-02-07 11:43   ` Rob Pike
2008-02-07 12:37     ` Hongzheng Wang

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