9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] ed regular expressions in sam
@ 2021-07-20 10:17 revcomninos
  2021-07-20 10:32 ` Alex Musolino
  0 siblings, 1 reply; 11+ messages in thread
From: revcomninos @ 2021-07-20 10:17 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 459 bytes --]

In ed it is trivial to edit either side of a line such as changing the line,

I use sam and ed

to,

echo "I use sam and ed."

simply with,

g/.*/s//echo "&."/

I am trying to figure out how to do the same in sam. 


------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Te7fcdc06a68bb777-M760f05e7ade2bc65818fe6e1
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 1931 bytes --]

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

* Re: [9fans] ed regular expressions in sam
  2021-07-20 10:17 [9fans] ed regular expressions in sam revcomninos
@ 2021-07-20 10:32 ` Alex Musolino
  2021-07-20 11:00   ` revcomninos
  0 siblings, 1 reply; 11+ messages in thread
From: Alex Musolino @ 2021-07-20 10:32 UTC (permalink / raw)
  To: 9fans

> In ed it is trivial to edit either side of a line such as changing
> the line,
> 
> I use sam and ed
> 
> to,
> 
> echo "I use sam and ed."
> 
> simply with,
> 
> g/.*/s//echo "&."/
> 
> I am trying to figure out how to do the same in sam. 

,s/^.*$/echo "&."/g

or, perhaps closer to your ed example:

,x/^.*$/s//echo "&."/


------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Te7fcdc06a68bb777-Me4b54fadfcca529e4c53804e
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

* Re: [9fans] ed regular expressions in sam
  2021-07-20 10:32 ` Alex Musolino
@ 2021-07-20 11:00   ` revcomninos
  2021-07-20 11:12     ` umbraticus
  0 siblings, 1 reply; 11+ messages in thread
From: revcomninos @ 2021-07-20 11:00 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 271 bytes --]

Thank you. I will test this an revert back.
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Te7fcdc06a68bb777-Mc5bcc7a82cda1c6c53ac3225
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 759 bytes --]

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

* Re: [9fans] ed regular expressions in sam
  2021-07-20 11:00   ` revcomninos
@ 2021-07-20 11:12     ` umbraticus
  2021-07-20 13:06       ` Rob Pike
  2021-07-20 13:45       ` revcomninos
  0 siblings, 2 replies; 11+ messages in thread
From: umbraticus @ 2021-07-20 11:12 UTC (permalink / raw)
  To: 9fans

or:

,x/^/i/echo "
,x/$/i/"


------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Te7fcdc06a68bb777-M3a70c9ddc3e626951d6349fb
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

* Re: [9fans] ed regular expressions in sam
  2021-07-20 11:12     ` umbraticus
@ 2021-07-20 13:06       ` Rob Pike
  2021-07-20 13:45       ` revcomninos
  1 sibling, 0 replies; 11+ messages in thread
From: Rob Pike @ 2021-07-20 13:06 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 463 bytes --]

I would

,x s/.*/echo "&"/

as it seems most direct. The ,x idiom is the thing that turns sam into ed.

-rob


On Tue, Jul 20, 2021 at 9:18 PM <umbraticus@prosimetrum.com> wrote:

> or:
> 
> ,x/^/i/echo "
> ,x/$/i/"
> 

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Te7fcdc06a68bb777-Mbf5a4df73cf5986b3267f7a3
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 1793 bytes --]

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

* Re: [9fans] ed regular expressions in sam
  2021-07-20 11:12     ` umbraticus
  2021-07-20 13:06       ` Rob Pike
@ 2021-07-20 13:45       ` revcomninos
  2021-07-20 14:21         ` revcomninos
  1 sibling, 1 reply; 11+ messages in thread
From: revcomninos @ 2021-07-20 13:45 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 473 bytes --]

Thank you all for your responses. They were extremely helpful. The first example, which uses "g" threw me a little because I am not used to "g" at the end of the line. I presume it here means something like, "without changing the value of dot?"
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Te7fcdc06a68bb777-M7a228e27b06be7a6b6779077
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 1001 bytes --]

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

* Re: [9fans] ed regular expressions in sam
  2021-07-20 13:45       ` revcomninos
@ 2021-07-20 14:21         ` revcomninos
  2021-07-20 14:31           ` Silas
  0 siblings, 1 reply; 11+ messages in thread
From: revcomninos @ 2021-07-20 14:21 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 295 bytes --]

I.e. in sam. In ed, g is frequently used at the end of the line. 
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Te7fcdc06a68bb777-M976ef16f9b129d3abeaa2511
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 797 bytes --]

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

* Re: [9fans] ed regular expressions in sam
  2021-07-20 14:21         ` revcomninos
@ 2021-07-20 14:31           ` Silas
  2021-07-20 15:45             ` revcomninos
  0 siblings, 1 reply; 11+ messages in thread
From: Silas @ 2021-07-20 14:31 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 815 bytes --]

In sam, g has two uses - either as a guard or after an s command where all substitutions are made

Quoting sam(1) (available at <http://9p.io/magic/man2html/1/sam>)
after an s command:
> If the command is followed by a g, as in
>                s/x/y/g, all matches in the range are substituted.
as a guard:
> g/regexp/ command
> v/regexp/ command
>                If the range contains (g) or does not contain (v) a
>                match for the expression, set dot to the range and run
>                the command.

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Te7fcdc06a68bb777-Md69f4d24cf42efd3c4ceca26
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 1827 bytes --]

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

* Re: [9fans] ed regular expressions in sam
  2021-07-20 14:31           ` Silas
@ 2021-07-20 15:45             ` revcomninos
  2021-07-20 18:32               ` Silas McCroskey
  0 siblings, 1 reply; 11+ messages in thread
From: revcomninos @ 2021-07-20 15:45 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 479 bytes --]

Thank you for the clarification. This first usage conforms to ed. I am currently working slowly through "A Tutorial for the Sam Command Language" http://doc.cat-v.org/bell_labs/sam_lang_tutorial/ but I have yet gotten to this usage of "g" after "s".
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Te7fcdc06a68bb777-M0246b5a74e07da02c53789c1
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 1075 bytes --]

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

* Re: [9fans] ed regular expressions in sam
  2021-07-20 15:45             ` revcomninos
@ 2021-07-20 18:32               ` Silas McCroskey
  2021-07-20 18:57                 ` revcomninos
  0 siblings, 1 reply; 11+ messages in thread
From: Silas McCroskey @ 2021-07-20 18:32 UTC (permalink / raw)
  To: 9fans

> I have yet gotten to this usage of "g" after "s".

The key difference here is not in the behavior of suffixed 'g', but in
how selections are made and operated on in the first place.

Ed's "selection" (aka dot, as you can reference it with '.') is always
a full, single line. It can *iterate* over multiple lines with
prefixed 'g', but each command (e.g. 's') will be run against only one
line at a time. As a result, suffixed 'g' always means "allow multiple
edits within this line" (alternatively, *not* suffixing with 'g' means
"only edit the first occurence of each line").

Sam's "selection" is more arbitrary -- it can be (and frequently is)
less than a line or multiple lines at once. In this case 'g' takes on
a different meaning -- "allow multiple edits within this selection" --
meaning if you *don't* pass 'g', and you're working with a multi-line
selection, only the first match in the entire selection will be
changed by the 's', regardless of whether other matches occurred on
separate lines.

As rob pointed out, interposing an 'x' without a regex will split the
selection into lines and iterate over each, getting you back to the
line-at-a-time behavior that is often easier to reason about.

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Te7fcdc06a68bb777-M4fb1e908c2fdeb00adbda077
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

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

* Re: [9fans] ed regular expressions in sam
  2021-07-20 18:32               ` Silas McCroskey
@ 2021-07-20 18:57                 ` revcomninos
  0 siblings, 0 replies; 11+ messages in thread
From: revcomninos @ 2021-07-20 18:57 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 415 bytes --]

Thank you. Those distinctions are very helpful. I agree that thinking in terms of lines is easier to reason about. I am going to have to change some of my thinking and break old habits. 
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Te7fcdc06a68bb777-M096ade136fe434ef64a397b8
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 913 bytes --]

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

end of thread, other threads:[~2021-07-20 18:57 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-20 10:17 [9fans] ed regular expressions in sam revcomninos
2021-07-20 10:32 ` Alex Musolino
2021-07-20 11:00   ` revcomninos
2021-07-20 11:12     ` umbraticus
2021-07-20 13:06       ` Rob Pike
2021-07-20 13:45       ` revcomninos
2021-07-20 14:21         ` revcomninos
2021-07-20 14:31           ` Silas
2021-07-20 15:45             ` revcomninos
2021-07-20 18:32               ` Silas McCroskey
2021-07-20 18:57                 ` revcomninos

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