carriage return or funky character stuck in my variables - help :)

My variables contain carriage returns... or something.

Here is the line in my xml file of interest:

<tmp>72</tmp>

And here is the line that extracts the 72 from it:

REAL=`grep '<tmp>' test.xml | sed -e 's/^ *<tmp>//' -e 's/<\/tmp>//' -re 's/(N|n|A|a|\/)/U/g'`

If echo the variable it looks just fine, but if I string a few together, the output is all messed up suggesting to me that there is an extra character in there such as a carriage return or something.

REAL=`grep '<tmp>' test.xml | sed -e 's/^ *<tmp>//' -e 's/<\/tmp>//' -re 's/(N|n|A|a|\/)/U/g'`
echo ${REAL},${REAL},${REAL}

When I run that, it returns simply:

,72

If I try to pipe it into a comma separated file, it's equally messed-up.

REAL=`grep '<tmp>' test.xml | sed -e 's/^ *<tmp>//' -e 's/<\/tmp>//' -re 's/(N|n|A|a|\/)/U/g'`
echo ${REAL},${REAL},${REAL} > test.csv

Here are the contents of test.csv:

72
,72
,72

What am I doing wrong?

Seems to work for me on Linux. I get errors on OS X and OpenBSD though (doesn't understand the -re option).

What are you doing your work on?

Carl

Running Debian LINUX.... the output shouldn't contain any linefeeds/returns though...

In the example above, it should return a file with a single line like this:

72,72,72

Did it do that for you?

GOT IT!

REAL=`grep '<tmp>' $1 | sed -e 's/^ *<tmp>//' -e 's/<\/tmp>//' -e 's/.$//g' -re 's/(N|n|A|a|\/)/U/g'`

...you gotta love google. The solution was contained in this thread.

Just FYI, it did do that for me (I see you have an answer but thought I'd answer you :slight_smile: ).

I had 15 or so of the lines so I had 72 72 72 72 72, 72 72 72 72 72, 72 72 72 72 72 but all in a line.

Carl

The grep is quite redundant.

sed -e '/<tmp>/!d' -e 's/^ *<tmp>//' -e 's/<\/tmp>//' -e 's/.$//g' -re 's/(N|n|A|a|\/)/U/g' "$1"