4

I want to do comma separated string from my array. In my script i collecting data to array outputArr and then i want to echo it to check it out, but now i'm stuck. I want to print it on console using:

echo ${outputArr[*]}

But i'm getting wrong output. So i'm trying to debug it and write it like this:

echo ${outputArr[0]}
echo ${outputArr[1]}
echo ${outputArr[*]}
echo "-------------"

OK here is my output:

Terminally Yours
2204
 2204nally Yours
-------------

and other example:

Alfa
1491
 1491
-------------

Why this is overwritten with space in the beginning? Of course my goal is (finally):

Alfa;1491

In this point:

Alfa1491

EDIT:

while read -r line
do
    singleLine=$line
    if [[ $singleLine =~ $regexVenueName ]]
    then
        singleLine=${singleLine/<span id=\"ctl00_MainContent_lbPartner\" class=\"Partner\">/}
        singleLine=${singleLine/<\/span> <br \/><br \/>/}
        partnerVenueNameOutput+="$singleLine"
        outputArr[0]=$singleLine
    fi
done < "$f"
4
  • Have you tried quoting your variables? echo "${outputArr[0]}" Commented Jan 20, 2014 at 14:40
  • 1
    Your array elements end with carriage returns, probably because you read from a file with DOS line endings or captured output from a command that used carriage returns. Commented Jan 20, 2014 at 14:42
  • @fedorqui - Yep, didn't work. Commented Jan 20, 2014 at 14:43
  • @chepner - maybe... how can i remove it? Commented Jan 20, 2014 at 14:43

2 Answers 2

4

Your array contains elements with carriage returns:

$ foo=($'\rterminally yours' $'\r2204')
$ echo "${foo[0]}"
terminally yours
$ echo "${foo[1]}"
2204
$ echo "${foo[*]}"
2204inally yours 

For your code, you can add the following to remove the carriage return from the variable:

singleLine=${singleLine//$'\r'/}
Sign up to request clarification or add additional context in comments.

1 Comment

There probably it. How can i remove it? I read my file line by line from html file downloaded with wget. I update my answer adding some code.
2

To remove any trailing carriage returns from each array element, try

outputArray=( "${outputArray[@]%$'\r'}" )

However, it would probably be better to examine how outputArray is set in the first place and prevent the carriage returns from being added in the first place. If you are reading from a file that uses DOS line endings, try "cleaning" it first using dos2unix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.