Re: Problem with positional output

"'Ruud H.G. van Tol' [email protected] [sed-users]" <[email protected]> Mon, 28 Nov 2016 16:31:14 +0100
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
On 2016-11-28 14:25, [email protected] [sed-users] wrote:
>
> ---In [email protected], <linux4michelle@...> wrote :
>  > Not realy, because I need to separate the output of "zenity" which return> VAL1|VAL2|VAL3
>
>> LINK="$(echo ${RETURN} |sed 's!\(.*\)|\(.*\)|\(.*\)!\1!')"
>> FNAME="$(echo ${RETURN} |sed 's!\(.*\)|\(.*\)|\(.*\)!\2!')"
>> DIR="$(echo ${RETURN} |sed 's!\(.*\)|\(.*\)|\(.*\)!\3!')"
>
>
>  There are many issues with the above:
>    1. Running a command multiple times.
>    2. echo will behave incorrectly for certain values of $RETURN
>    3. $RETURN might contain globbing chars which might play truant since $RETURN is left unquoted.
>    4. if any of the VALs happen to contain trailing newlines then they shall be lost.
>    5. There is no need to quote the var=$(...) construct coz it's superfluous.
>
>
>  We can do something like the following:
>
>
>      set -f; IFS=\|; set -- $RETURN
>      case ${3++} in ?) LINK=$1  FNAME=$2   DIR=$3;;
>
>      '' ) echo >&2 "Error";; esac
>
>  N.B.: You should restore the IFS and set -f settings afterwards.

# In bash:

RET="VAL1|VAL2|VAL3"

if [[ $RET =~ ([^|]*)\|([^|]*)\|([^|]*) ]]
then
   echo "${BASH_REMATCH[1]}:${BASH_REMATCH[2]}:${BASH_REMATCH[3]}";
fi

# which echoes:
#
# VAL1:VAL2:VAL3

-- Ruud