0

I am going crazy to understand why the following bash code:

#! /bin/bash
myarrx=($(cat /Users/Porcac/Downloads/2.csv))

for ((i=0;i<${#myarrx[@]};++i)); do
echo ${myarrx[i]} | while IFS=, read -r number alphabet animal colour behaviourCode behaviour
do
   echo "number='$number' alphabet='$alphabet' animal='$animal' colour='$colour' behaviourCode='$behaviourCode' behaviour='$behaviour'" 
done
done

Prints the string like the one in the screenshot

enter image description here

As you can see, the part of the string that comes after the last array item, is then printed at the beginning of the line and is overriding the initial part of the string. I really don't understand why it doesn't accept other strings after the last array item. The array is taking the content from a .cvs file that looks like this:

enter image description here

Any suggestion?

The array DOES NOT contain any special character, only simple words

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Porcac1x
  • 105
  • 9
  • 1
    Could by end of line mishandling? Try running the file through `dos2unix` first to convert the line endings to `'\n'`. – TenG Jun 05 '21 at 10:50
  • Awesome, thanks TenG, that was it. I didn't want to install dos2unix as I am currently on a Mac but running sed -e 's/\r$//' 2.csv > 3.csv and then opening that new file seems to work. Many thanks! – Porcac1x Jun 05 '21 at 10:55

0 Answers0