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

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



On 7/14/20 8:52 AM, Albretch Mueller wrote:
 I have a string delimited by two characters: "\|"

 _S=" 34 + 45 \| abc \| 1 2 3 \| c\|123abc "

 which then I need to turn into a array looking like:

  _S_AR=(
" 34 + 45 "
" abc "
" 1 2 3 "
" c"
"123abc "
)

Try:

echo " 34 + 45 \| abc \| 1 2 3 \| c\|123abc " | tr -d '\\' | awk 'BEGIN { FS="|" } { printf "  _S_AR=(\n\"%s\"\n\"%s\"\n\"%s\"\n\"%s\"\n\"%s\"\n)\n",$1,$2,$3,$4,$5}'

All one line.

First tr deletes the backslash leaving the pipe for a field separator in awk.  You could also use tr -s '\\\|' '\|' to guarantee that you only work on the \| combination in case there are other backslashes not followed by a pipe.


--


...Bob

Reply to: