* substring extraction
@ 2006-05-08 7:32 Com MN PG P E B Consultant 3
2006-05-08 8:07 ` Stephane Chazelas
0 siblings, 1 reply; 3+ messages in thread
From: Com MN PG P E B Consultant 3 @ 2006-05-08 7:32 UTC (permalink / raw)
To: zsh-users Mailinglist
bash has the nice feature to extract a substring from a shell variable.
For example
# bash example
var=abcd
echo ${var:1:2} # Start at offset 1, returns 2 characters
displays
bc
Is there an easy way to achieve a similar effect in zsh, without
reverting to external
tools such as awk or sed?
Ronald
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: substring extraction
2006-05-08 7:32 substring extraction Com MN PG P E B Consultant 3
@ 2006-05-08 8:07 ` Stephane Chazelas
2006-05-08 15:29 ` Peter Stephenson
0 siblings, 1 reply; 3+ messages in thread
From: Stephane Chazelas @ 2006-05-08 8:07 UTC (permalink / raw)
To: zsh-users Mailinglist
On Mon, May 08, 2006 at 09:32:50AM +0200, Com MN PG P E B Consultant 3 wrote:
> bash has the nice feature to extract a substring from a shell variable.
> For example
>
> # bash example
> var=abcd
> echo ${var:1:2} # Start at offset 1, returns 2 characters
>
> displays
>
> bc
That's a ksh feature borrowed by bash. And as of many ksh
features, it's got its flaws. That one conflicts with the
${var:-default-value} Bourne operator, so that you need to do
${var: -1} (add a space) to get the last char.
> Is there an easy way to achieve a similar effect in zsh, without
> reverting to external
> tools such as awk or sed?
[...]
In zsh:
$var[2,3]
zsh can do it because arrays and scalar variables are two
different types. In zsh, $var[2,5] gives the 2nd to 5th element
when $var is of array type, and 2nd to 5th char when $var is
scalar. In ksh/bash all variables are arrays and $var is a
shortcut for ${var[0]} (though it's not completely true of
bash).
--
Stéphane
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: substring extraction
2006-05-08 8:07 ` Stephane Chazelas
@ 2006-05-08 15:29 ` Peter Stephenson
0 siblings, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2006-05-08 15:29 UTC (permalink / raw)
To: zsh-users
"Stephane Chazelas" <Stephane_Chazelas@yahoo.fr> wrote:
> In zsh:
>
> $var[2,3]
>
> zsh can do it because arrays and scalar variables are two
> different types. In zsh, $var[2,5] gives the 2nd to 5th element
> when $var is of array type, and 2nd to 5th char when $var is
> scalar. In ksh/bash all variables are arrays and $var is a
> shortcut for ${var[0]} (though it's not completely true of
> bash).
Just to point out you can combine the two in the same expression in zsh.
As soon as you've extracted a single element, later suffixes work on
characters. (One day this may be multibyte characters but that's not done
yet.)
% array=(one two three four)
% print ${array[3][2,-2]}
hre
Since the rule is applied after every subscript match, you can do bizarre
stuff like:
% print ${array[1,3][-1][2,-2][2]}
r
which works in the following steps:
array one two three four -> [1,3]
array one two three -> [-1]
scalar three -> [2,-2]
scalar hre -> [2]
scalar r
--
Peter Stephenson <pws@csr.com> Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK Tel: +44 (0)1223 692070
To access the latest news from CSR copy this link into a web browser: http://www.csr.com/email_sig.php
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2006-05-08 15:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-08 7:32 substring extraction Com MN PG P E B Consultant 3
2006-05-08 8:07 ` Stephane Chazelas
2006-05-08 15:29 ` Peter Stephenson
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).