[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: delimiters with more than one character? ...



Hi,

Eric S Fraga wrote:
> See the "\n" (newline) in the replacement pattern?

Of course. Else there would be only one output line.


Greg Wooledge wrote:
> If you've got an input stream with one element per line that you want
> to import into a bash array, you use the "mapfile" (or "readarray")
> command.

I tried

  echo " 34 + 45 \| abc \| 1 2 3 \| c\|123abc " | \
  sed -e 's/\\|/\n/g' | \
  mapfile -t _S_AR

with no visible effect on the array _S_AR. bash 4.3.30 and 5.0.11.

This works

  echo " 34 + 45 \| abc \| 1 2 3 \| c\|123abc " | sed -e 's/\\|/\n/g' >/tmp/x

  mapfile -t _S_AR < /tmp/x

It also works interactively

  $ mapfile -t _S_AR
  1
  2
  3
  <Ctrl+D>
  $ echo "'${_S_AR[1]}'"
  '2'
  $

So the obstacle seems to be that variable settings made in a shell pipe
do not persist. (It's not a bug, but a feature ... )

A workaround without temporary file would be

  x=" 34 + 45 \| abc \| 1 2 3 \| c\|123abc "
  split=$(echo "$x" | sed -e 's/\\|/\n/g')
  end=$(echo -n "$x" | sed -e 's/./+/g')"+"

  mapfile -t _S_AR <<$end
  $split
  $end

Question with this hack is of course how long the $end string of the
here-document may become. Maybe one can come up with a shorter string
that surely does not occur as line in $split.


Have a nice day :)

Thomas


Reply to: