* Paramater subsiution
@ 2021-06-27 22:40 Lewis Butler
2021-06-27 22:47 ` Gamma
0 siblings, 1 reply; 4+ messages in thread
From: Lewis Butler @ 2021-06-27 22:40 UTC (permalink / raw)
To: Zsh Users
Given:
name="file 1234 - name"
echo ${I% - name}
I can easily get 'file', but how would I get "name" instead given that:
o name and 1234 are variable lengths and values
o "file" and " - " are constant across all the input
I've trued using
${i:s/file \[0-9\]+ - //}
${i:s/file \[0-9\]\[0-9\]\[0-9\]\[0-9\] - //}
${i:s/file [0-9]+ - //}
${i:s/file [0-9][0-9][0-9][0-9] - //}
${i:s/file [^ ]+ - //}
And similar to no avail, though:
${i:s/file //}
prints "1234 - name"
--
ɹןʇnqן
<mailto:lbutler@covisp.net>
tel:+1.303.219.0564
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Paramater subsiution
2021-06-27 22:40 Paramater subsiution Lewis Butler
@ 2021-06-27 22:47 ` Gamma
2021-06-28 5:34 ` Lawrence Velázquez
2021-06-28 22:33 ` Lewis Butler
0 siblings, 2 replies; 4+ messages in thread
From: Gamma @ 2021-06-27 22:47 UTC (permalink / raw)
To: Lewis Butler; +Cc: Zsh Users
Jun 27, 2021 17:42:48 Lewis Butler <lbutler@covisp.net>:
> Given:
>
> name="file 1234 - name"
> echo ${I% - name}
>
> I can easily get 'file', but how would I get "name" instead given that:
>
> o name and 1234 are variable lengths and values
> o "file" and " - " are constant across all the input
echo ${i##* - }
Removes the longest prefix ending with ' - '.
Check man zshexpn for more.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Paramater subsiution
2021-06-27 22:47 ` Gamma
@ 2021-06-28 5:34 ` Lawrence Velázquez
2021-06-28 22:33 ` Lewis Butler
1 sibling, 0 replies; 4+ messages in thread
From: Lawrence Velázquez @ 2021-06-28 5:34 UTC (permalink / raw)
To: Gamma, Lewis Butler; +Cc: zsh-users
On Sun, Jun 27, 2021, at 6:47 PM, Gamma wrote:
> echo ${i##* - }
>
> Removes the longest prefix ending with ' - '.
${i#* - } would also work and would permit "name" to be completely
arbitrary (assuming that "file 1234" could not possibly contain
" - " as a substring).
https://zsh.sourceforge.io/Doc/Release/Expansion.html#Parameter-Expansion
On Sun, Jun 27, 2021, at 6:40 PM, Lewis Butler wrote:
> I've trued using
>
> ${i:s/file \[0-9\]+ - //}
> ${i:s/file \[0-9\]\[0-9\]\[0-9\]\[0-9\] - //}
> ${i:s/file [0-9]+ - //}
> ${i:s/file [0-9][0-9][0-9][0-9] - //}
> ${i:s/file [^ ]+ - //}
First, you probably wanted ${i/foo/bar} rather than ${i:s/foo/bar}.
The latter matches foo literally unless the HIST_SUBST_PATTERN
option is set. It's also less appropriate in a conceptual sense.
Second, these substitution features use patterns ("globs"), not
regular expressions. Bracket expressions are treated specially
([...], not \[...\]), but '+' generally is not. You can find details
here, including how you could achieve the same effect as '+' (if
you really must):
https://zsh.sourceforge.io/Doc/Release/Expansion.html#Filename-Generation
But you should use Gamma's solution (or my variant), which is simple
and highly portable.
--
vq
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Paramater subsiution
2021-06-27 22:47 ` Gamma
2021-06-28 5:34 ` Lawrence Velázquez
@ 2021-06-28 22:33 ` Lewis Butler
1 sibling, 0 replies; 4+ messages in thread
From: Lewis Butler @ 2021-06-28 22:33 UTC (permalink / raw)
To: Zsh Users
On 27 Jun 2021, at 16:47, Gamma <GammaFunction@vivaldi.net> wrote:
> Jun 27, 2021 17:42:48 Lewis Butler <lbutler@covisp.net>:
>
>> Given:
>>
>> name="file 1234 - name"
>> echo ${I% - name}
>>
>> I can easily get 'file', but how would I get "name" instead given that:
>>
>> o name and 1234 are variable lengths and values
>> o "file" and " - " are constant across all the input
>
> echo ${i##* - }
<thunks head on desk>
> Removes the longest prefix ending with ' - '.
>
> Check man zshexpn for more.
I swear I knew that at one point, but missed it several times in reading the man page.
Thank you.
--
ɹןʇnqן
<mailto:lbutler@covisp.net>
tel:+1.303.219.0564
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-06-28 22:34 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-27 22:40 Paramater subsiution Lewis Butler
2021-06-27 22:47 ` Gamma
2021-06-28 5:34 ` Lawrence Velázquez
2021-06-28 22:33 ` Lewis Butler
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).