Re: Problem with positional output
"Stephane Chazelas [email protected] [sed-users]" <[email protected]> Tue, 15 Nov 2016 12:47:23 +0000
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <[email protected]> |
2016-11-14 12:28:10 -0800, Jim Hill:
> I think Stephane's method is best, though I don't see the need for the
> noglob here, I think
>
> (IFS=\|; set -- $RETVAL; printf %s\\n "$3")
No, the set -o noglob is needed here.
Using a variable unquoted in Bourne-like shells except zsh is
the split+glob operator.
Try with:
RETVAL='*|?|[a-z]'
For instance.
See also
https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells
More generally, when leaving a variable or other expansion
unquoted, you need to consider $IFS and the status of the noglob
option.
> As an alternative if your data doesn't contain newlines, awk is
> specialized for this,
>
> awk -F\| '{print $3}' <<<"$RETVAL"
>
> will do the same job.
Again, that retrieves the 3rd field of every line of $RETVAL, not
the 3rd field in $RETVAL.
If you're going to use that zsh <<< operator (though it's now
also supported by recent versions of ksh93 and bash), you could
as well use ${"${(@s:|:)REVAL}"[3]}.
s:|: splits on |
@ preserves empty elements within quotes
--
Stephane