zsh-users
 help / color / mirror / code / Atom feed
* Is ":G" of ${name:s/l/r/:G} actually supported?
@ 2024-02-12 14:19 Joachim Ansorg
  2024-02-12 17:02 ` Mark J. Reed
  0 siblings, 1 reply; 5+ messages in thread
From: Joachim Ansorg @ 2024-02-12 14:19 UTC (permalink / raw)
  To: Zsh Users

Hi,
I was reading about modifiers on page 
https://zsh.sourceforge.io/Doc/Release/Expansion.html#Modifiers, which says:

>   The forms ‘gs/l/r’ and ‘s/l/r/:G’ perform global substitution, i.e. substitute every occurrence of r for l. Note that the g or :G must appear in exactly the position shown.

But zsh 5.9 doesn't seem to support this:
  > value="/dir/subdir/file.csv"
  > echo ${value:s/dir/_/:G}
  zsh: unrecognized modifier `G'

Is ":G" actually supported or is the documentation outdated here?
I might be overlooking something very simple, though.

Thank you!
Joachim Ansorg
-- 
BashSupport Pro
https://www.bashsupport.com/


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

* Re: Is ":G" of ${name:s/l/r/:G} actually supported?
  2024-02-12 14:19 Is ":G" of ${name:s/l/r/:G} actually supported? Joachim Ansorg
@ 2024-02-12 17:02 ` Mark J. Reed
  2024-02-12 17:11   ` Mark J. Reed
  0 siblings, 1 reply; 5+ messages in thread
From: Mark J. Reed @ 2024-02-12 17:02 UTC (permalink / raw)
  To: Joachim Ansorg; +Cc: Zsh Users

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

That's history substitution, not parameter expansion:


*$ ls foobar*
*...*
*$ !!:s/o/e/:G*
*ls feebar*



On Mon, Feb 12, 2024 at 9:19 AM Joachim Ansorg <mail@bashsupport.com> wrote:

> Hi,
> I was reading about modifiers on page
> https://zsh.sourceforge.io/Doc/Release/Expansion.html#Modifiers, which
> says:
>
> >   The forms ‘gs/l/r’ and ‘s/l/r/:G’ perform global substitution, i.e.
> substitute every occurrence of r for l. Note that the g or :G must appear
> in exactly the position shown.
>
> But zsh 5.9 doesn't seem to support this:
>   > value="/dir/subdir/file.csv"
>   > echo ${value:s/dir/_/:G}
>   zsh: unrecognized modifier `G'
>
> Is ":G" actually supported or is the documentation outdated here?
> I might be overlooking something very simple, though.
>
> Thank you!
> Joachim Ansorg
> --
> BashSupport Pro
> https://www.bashsupport.com/
>
>

-- 
Mark J. Reed <markjreed@gmail.com>

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

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

* Re: Is ":G" of ${name:s/l/r/:G} actually supported?
  2024-02-12 17:02 ` Mark J. Reed
@ 2024-02-12 17:11   ` Mark J. Reed
  2024-02-12 21:50     ` Lawrence Velázquez
  0 siblings, 1 reply; 5+ messages in thread
From: Mark J. Reed @ 2024-02-12 17:11 UTC (permalink / raw)
  To: Zsh Users

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

Well, that was a shorter reply than I intended. But you should be able to
see that the section you linked (14.1.4 Modifiers) is part of 14.1, History
Expansion. Those modifiers don't apply to parameter substitution.

For doing replacements with parameter expansion, you can just use the slash
modifier. One / replaces the first occurrence, two //s does all of them:

*$ value=/dir/subdir/file.csv*
*$ echo ${value//dir/_G}*
*/_G/sub_G/file.csv*


That's not a zsh-specific feature; ksh and bash have it as well. Zsh likely
has a different mechanism to accomplish the same thing, but I've not needed
it so am not familiar with it.
--
Mark J. Reed <markjreed@gmail.com>


On Mon, Feb 12, 2024 at 12:02 Mark J. Reed <markjreed@gmail.com> wrote:

> That's history substitution, not parameter expansion:
>
>
> *$ ls foobar*
> *...*
> *$ !!:s/o/e/:G*
> *ls feebar*
>
>
>
> On Mon, Feb 12, 2024 at 9:19 AM Joachim Ansorg <mail@bashsupport.com>
> wrote:
>
>> Hi,
>> I was reading about modifiers on page
>> https://zsh.sourceforge.io/Doc/Release/Expansion.html#Modifiers, which
>> says:
>>
>> >   The forms ‘gs/l/r’ and ‘s/l/r/:G’ perform global substitution, i.e.
>> substitute every occurrence of r for l. Note that the g or :G must appear
>> in exactly the position shown.
>>
>> But zsh 5.9 doesn't seem to support this:
>>   > value="/dir/subdir/file.csv"
>>   > echo ${value:s/dir/_/:G}
>>   zsh: unrecognized modifier `G'
>>
>> Is ":G" actually supported or is the documentation outdated here?
>> I might be overlooking something very simple, though.
>>
>> Thank you!
>> Joachim Ansorg
>> --
>> BashSupport Pro
>> https://www.bashsupport.com/
>>
>>
>
> --
> Mark J. Reed <markjreed@gmail.com>
>

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

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

* Re: Is ":G" of ${name:s/l/r/:G} actually supported?
  2024-02-12 17:11   ` Mark J. Reed
@ 2024-02-12 21:50     ` Lawrence Velázquez
  2024-02-13  2:23       ` Mark J. Reed
  0 siblings, 1 reply; 5+ messages in thread
From: Lawrence Velázquez @ 2024-02-12 21:50 UTC (permalink / raw)
  To: Mark J. Reed, Joachim Ansorg; +Cc: zsh-users

On Mon, Feb 12, 2024, at 12:11 PM, Mark J. Reed wrote:
> Well, that was a shorter reply than I intended. But you should be able 
> to see that the section you linked (14.1.4 Modifiers) is part of 14.1, 
> History Expansion. Those modifiers don't apply to parameter 
> substitution.

They do.

	% var=foobar
	% print -- $var:s/o/x
	fxobar

This is documented in zshexpn(1) under "Modifiers":

	After the optional word designator, you can add a sequence
	of one or more of the following modifiers, each preceded
	by a `:'.  These modifiers also work on the result of
	_filename generation_ and _parameter expansion_, except
	where noted.

and "PARAMETER EXPANSION":

	In addition to the following operations, the colon modifiers
	described in the section `Modifiers' in the section `History
	Expansion' can be applied: for example, ${i:s/foo/bar/}
	performs string substitution on the expansion of parameter $i.


> For doing replacements with parameter expansion, you can just use the 
> slash modifier. One / replaces the first occurrence, two //s does all 
> of them:
>
>> *$ value=/dir/subdir/file.csv*
>> *$ echo ${value//dir/_G}*
>> */_G/sub_G/file.csv*
> 
> That's not a zsh-specific feature; ksh and bash have it as well. Zsh 
> likely has a different mechanism to accomplish the same thing, but I've 
> not needed it so am not familiar with it.

The :s and :gs history modifiers are similar but not exactly so.
Among other things, they perform literal searches by default, allow
referring to the matched text with "&", and apply nested expansions
differently.


>> On Mon, Feb 12, 2024 at 9:19 AM Joachim Ansorg <mail@bashsupport.com> wrote:
>>> Hi,
>>> I was reading about modifiers on page 
>>> https://zsh.sourceforge.io/Doc/Release/Expansion.html#Modifiers, which says:
>>> 
>>> >   The forms ‘gs/l/r’ and ‘s/l/r/:G’ perform global substitution, i.e. substitute every occurrence of r for l. Note that the g or :G must appear in exactly the position shown.
>>> 
>>> But zsh 5.9 doesn't seem to support this:
>>>   > value="/dir/subdir/file.csv"
>>>   > echo ${value:s/dir/_/:G}
>>>   zsh: unrecognized modifier `G'

It doesn't work on zsh 4.3.11 either, which means it hasn't worked
for at least 13 years.  (This probably says something about the
prevalence of applying :s/l/r/:G to parameter expansions.)


>>> Is ":G" actually supported or is the documentation outdated here?

I can't say for sure, but this feels like a bug to me.


-- 
vq


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

* Re: Is ":G" of ${name:s/l/r/:G} actually supported?
  2024-02-12 21:50     ` Lawrence Velázquez
@ 2024-02-13  2:23       ` Mark J. Reed
  0 siblings, 0 replies; 5+ messages in thread
From: Mark J. Reed @ 2024-02-13  2:23 UTC (permalink / raw)
  To: Lawrence Velázquez; +Cc: Joachim Ansorg, zsh-users

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

>
        In addition to the following operations, the colon modifiers
        described in the section `Modifiers' in the section `History
        Expansion' can be applied: for example, ${i:s/foo/bar/}
        performs string substitution on the expansion of parameter $i.

That's a very helpful snippet that I missed. Thanks. Sorry for the
misinformation.


On Mon, Feb 12, 2024 at 4:52 PM Lawrence Velázquez <larryv@zsh.org> wrote:

> On Mon, Feb 12, 2024, at 12:11 PM, Mark J. Reed wrote:
> > Well, that was a shorter reply than I intended. But you should be able
> > to see that the section you linked (14.1.4 Modifiers) is part of 14.1,
> > History Expansion. Those modifiers don't apply to parameter
> > substitution.
>
> They do.
>
>         % var=foobar
>         % print -- $var:s/o/x
>         fxobar
>
> This is documented in zshexpn(1) under "Modifiers":
>
>         After the optional word designator, you can add a sequence
>         of one or more of the following modifiers, each preceded
>         by a `:'.  These modifiers also work on the result of
>         _filename generation_ and _parameter expansion_, except
>         where noted.
>
> and "PARAMETER EXPANSION":
>
>         In addition to the following operations, the colon modifiers
>         described in the section `Modifiers' in the section `History
>         Expansion' can be applied: for example, ${i:s/foo/bar/}
>         performs string substitution on the expansion of parameter $i.
>
>
> > For doing replacements with parameter expansion, you can just use the
> > slash modifier. One / replaces the first occurrence, two //s does all
> > of them:
> >
> >> *$ value=/dir/subdir/file.csv*
> >> *$ echo ${value//dir/_G}*
> >> */_G/sub_G/file.csv*
> >
> > That's not a zsh-specific feature; ksh and bash have it as well. Zsh
> > likely has a different mechanism to accomplish the same thing, but I've
> > not needed it so am not familiar with it.
>
> The :s and :gs history modifiers are similar but not exactly so.
> Among other things, they perform literal searches by default, allow
> referring to the matched text with "&", and apply nested expansions
> differently.
>
>
> >> On Mon, Feb 12, 2024 at 9:19 AM Joachim Ansorg <mail@bashsupport.com>
> wrote:
> >>> Hi,
> >>> I was reading about modifiers on page
> >>> https://zsh.sourceforge.io/Doc/Release/Expansion.html#Modifiers,
> which says:
> >>>
> >>> >   The forms ‘gs/l/r’ and ‘s/l/r/:G’ perform global substitution,
> i.e. substitute every occurrence of r for l. Note that the g or :G must
> appear in exactly the position shown.
> >>>
> >>> But zsh 5.9 doesn't seem to support this:
> >>>   > value="/dir/subdir/file.csv"
> >>>   > echo ${value:s/dir/_/:G}
> >>>   zsh: unrecognized modifier `G'
>
> It doesn't work on zsh 4.3.11 either, which means it hasn't worked
> for at least 13 years.  (This probably says something about the
> prevalence of applying :s/l/r/:G to parameter expansions.)
>
>
> >>> Is ":G" actually supported or is the documentation outdated here?
>
> I can't say for sure, but this feels like a bug to me.
>
>
> --
> vq
>


-- 
Mark J. Reed <markjreed@gmail.com>

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

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

end of thread, other threads:[~2024-02-13  2:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-12 14:19 Is ":G" of ${name:s/l/r/:G} actually supported? Joachim Ansorg
2024-02-12 17:02 ` Mark J. Reed
2024-02-12 17:11   ` Mark J. Reed
2024-02-12 21:50     ` Lawrence Velázquez
2024-02-13  2:23       ` Mark J. Reed

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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