Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Array parameter expansion: take n-th word from array elements



​> ​
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