0

I want to split a string on a delimiter in zsh on MacOS 11.6.1. I looked at the following questions How do I split a string on a delimiter in Bash?

I tried to reproduce its 2 most upvoted answers as follows:

IN="[email protected];[email protected]"
IFS=';' read -ra ADDR <<< "$IN"
for i in "${ADDR[@]}"; do
  # process "$i"
done
IN="[email protected];[email protected]"
while IFS=';' read -ra ADDR; do
  for i in "${ADDR[@]}"; do
    # process "$i"
  done
done <<< "$IN"

but get the following output : bad option: -a

IN="[email protected];[email protected]"
arrIN=(${IN//;/ })
echo ${arrIN[1]}                  # Output: [email protected]

but get the following output : [email protected] [email protected] instead of [email protected]

What am I doing wrong?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
ecjb
  • 5,169
  • 12
  • 43
  • 79
  • 7
    `bash` and `zsh` are 2 different shells, so "in bash in zsh" doesn't make much sense. The error suggests you're using `zsh` in which case it would be `arrIN=(${(s[;])IN})` – jqurious Aug 15 '23 at 16:33
  • Many thanks @jqurious! that worked! (I removed the "bash" from the question) – ecjb Aug 15 '23 at 20:48
  • "bash" was still in title and tagging; I've removed it more thoroughly. – Charles Duffy Aug 15 '23 at 21:12
  • sorry about that. thank you @CharlesDuffy – ecjb Aug 15 '23 at 21:23
  • According to the zsh man pages, `read` does not have an `-a` option. Perhaps you mean `-A`: _-A ... The first name is taken as the name of an array and all words are assigned to it_. – user1934428 Aug 16 '23 at 06:02

1 Answers1

1

You can use awk and avoid different shells particularities

awk '{sub(";","\n"); print}' <<<"[email protected];[email protected]" | xargs process
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134