-2

I have a string like:

Spain-South Africa:2-1

And I want to split it like:

Spain-South Africa
2-1

I have tried to split it by IFS=':' but it gives me:

Spain-South

Africa
2-1

My code:

code

jww
  • 97,681
  • 90
  • 411
  • 885
Man
  • 3
  • 2
  • `IFS` by itself does nothing; what command that *uses* `IFS` are you running to produce that output? – chepner Dec 16 '19 at 15:35
  • I read that string from a file,so I use this: read -r -a results <<< "$str" – Man Dec 16 '19 at 15:37
  • And how are you displaying the contents of `results`? – chepner Dec 16 '19 at 15:38
  • See [Bash FAQ 001](https://mywiki.wooledge.org/BashFAQ/001); `str` is never set to the value you think it is in the first place, because the shell is splitting the output of `cat` on *whitespace*, not just newlines. – chepner Dec 16 '19 at 15:47
  • 1
    Type your code in the question; don't post links to images. – chepner Dec 16 '19 at 15:49

1 Answers1

1

Cannot reproduce, but you are probably either not setting IFS correctly for the read command, or you are not displaying the output correctly.

$ str="Spain-South Africa:2-1"
$ IFS=: read -ra results <<< "$str"
$ declare -p results
declare -a results=([0]="Spain-South Africa" [1]="2-1")

Based on your short-lived comment, you want something like

while IFS=: read -ra results; do
    ...
done < "$1"

rather than

for str in $(cat "$1"); do
    ...
done
chepner
  • 497,756
  • 71
  • 530
  • 681