Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've got this code:

Unix+=("Stock List")
while read line; do 
  result=$(wget -O - -o /dev/null "http://finance.yahoo.com/d/quotes.csv?s=$line&f=sl1&e=.csv" | tr ',' '    ' | tr '"' ' ')
  Unix+=("$result")
done < stock.txt

echo ${Unix[@]}
echo ${Unix[0]}
echo ${Unix[1]}
echo ${Unix[2]}

that reads a text file with only these lines in it:

SXX.L
QPP.L

And outputs this:

 QPP.L 14.2495.L 27.50
Stock List
SXX.L 27.50
QPP.L 14.2495

Why is the array[@] output missing information and messed up?

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

Executing with #!/bin/bash -x shows:

+ echo Stock List SXX.L $'27.50\r' QPP.L $'14.2495\r'

Those \r will likely be what's messing up the output. It moves the cursor back to the beginning of the line, and whatever follows afterward overwrites what's already there.

share|improve this answer
1  
Good find; confirmed that stripping out the \r characters fixes it –  Michael Mrozek Jan 30 '13 at 19:48
    
Thanks! I've added stripping the \r bits out, and as MM says, it fixes it! –  user31459 Jan 30 '13 at 20:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.