2

I am trying to insert into the file values which I get from another file (xml) and I want them to be in one row. I checked variables - separately they are but once I try to concatenate them somehow, something goes wrong and I get only parts of the prior strings.

My code looks as it follows:

echo $usd
echo $gbp
all=$usd$space$gbp
echo $all

The output looks like this:

3,6525
5,5407
 5,5407

Does anybody know what am I doing wrong? Or what can I do to restore my data?

2
  • The concatenation looks okay. It must be a problem with your data. You should show more code. Commented Apr 9, 2016 at 19:33
  • 1
    Your data contains carriage return characters \r -- verify with echo "$usd" | od -c -- run dos2unix on your input files. Commented Apr 9, 2016 at 19:43

1 Answer 1

3

You have a CR in your variable. I can reproduce your case with the vars

usd="3,6525^M"
gbp="5,5407^M"
space=" "

(I entered ^M in vi with CTRL-V CTRL-M, it is the \r character) When you do not want to clean the xml, usd and gbp, you can use

all=${usd%$'\r'}${space}${gbp%$'\r'}
Sign up to request clarification or add additional context in comments.

1 Comment

Or run dos2unix on the source file to remove the bad characters. That fixed it for my case.

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.