Re: [9fans] ed regular expressions in sam

2021-07-20 Thread revcomninos
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


Re: [9fans] ed regular expressions in sam

2021-07-20 Thread Silas McCroskey
> 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


Re: [9fans] ed regular expressions in sam

2021-07-20 Thread revcomninos
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


Re: [9fans] ed regular expressions in sam

2021-07-20 Thread Silas
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 )
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


Re: [9fans] ed regular expressions in sam

2021-07-20 Thread revcomninos
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


Re: [9fans] ed regular expressions in sam

2021-07-20 Thread revcomninos
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


Re: [9fans] ed regular expressions in sam

2021-07-20 Thread Rob Pike
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  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


Re: [9fans] ed regular expressions in sam

2021-07-20 Thread umbraticus
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


Re: [9fans] ed regular expressions in sam

2021-07-20 Thread revcomninos
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


Re: [9fans] ed regular expressions in sam

2021-07-20 Thread Alex Musolino
> 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


[9fans] ed regular expressions in sam

2021-07-20 Thread revcomninos
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