Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Array parameter expansion: take n-th word from array elements
- X-seq: zsh-users 22753
- From: Alexander Groß <agross@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: Array parameter expansion: take n-th word from array elements
- Date: Mon, 26 Jun 2017 11:50:46 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=therightstuff.de; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=FbqauOB2Bd0ZV0pwD9g3HduDybaoKNVdxmtuGcncKNI=; b=iJRUL/C7V7Q13KZEj+fHSq4NHvcfRkfg8JYcKEx8tfN9E2z3Fv1yhuIeAMQHV1hTTJ +SxO0sZjoyjEUnoLVqiJyfHhb0w3hyhH8daiLoUIPCKpbVkBd4kTUMYzxgP4h7eev+iO EgHHNxxuU7w4wL5TLI73Dvsv6uB1mh2RAyin8=
- In-reply-to: <170625161545.ZM29138@torch.brasslantern.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CACcHa=NNdnCAb8YcQkLqnXbQXt5-ttM5=MM=b5kdmUVcWhiuQQ@mail.gmail.com> <1710201498412064@web35j.yandex.ru> <170625161545.ZM29138@torch.brasslantern.com>
>
The only parameter substitution operator that iterates over elements of
> an array in the way you want is the ${NAME/PATTERN/REPL} syntax. If
> you setopt extendedglob and use (#m) in the pattern, $MATCH will give
> you each of the elements as a string, so you next need to interpret
> each string as words. The (w) subscript modifier does that.
> Thus:
>
> setopt extendedglob
> print -lr -- ${foo/(#m)*/${MATCH[(w)2]}}
Thank you, that was a pointer in the right direction!
I have another use case for a different output, generated from grep this
time. Some strings may not contain the second word and I want to remove
them. Your suggestion prints the last word in this case. Can this be
filtered out as well?
$ foo=('source interesting' ' source also-interesting' 'source')
$ setopt extendedglob
$
print -lr -- ${foo/(#m)*/${MATCH[(w)2]}}
interesting
also-interesting
source
Alex
--
Alexander Groß
http://therightstuff.de/
Messages sorted by:
Reverse Date,
Date,
Thread,
Author