A variable is including the Carriage Return char...

Hi all,

I'm reading a file with this layout:

First_Col Second_Col

The Second_Col has values as 1000, -1, 10, 43...

While reading the file I'm getting the second column value with awk command, but it is including the CR control char.

do
   item_saved=`echo $b | awk '{print $1}'`   #Get value ITEM_SAVED
   valu_saved=`echo $b | awk '{print $2}'`   #Get value VALU_SAVED
   valu_saved=`expr $valu_saved`
   .....

It is affecting my output in a new flat file (after validations).

Is there a way to remove this CR char from the string? converted to number using

`expr $valu_saved`

?

Thak you!

valu_saved=`echo $b | awk '{print $2}' | xargs`

?

Thank you for the note, but I'm still have the issue.

I have added

| xargs`

to my code string, but the variable still has the CR char.

I added the following code just for validation.

 echo "Saved:$valu_saved ."

Note the last dot. It is being displayed in another output line, the CR is still there.

Do you have any other idea?

Thank you.!

Could you please just show us what output you get with a simple :

echo :$b:

The colons will help us to see where your $b starts and ends, then it will be easier to parse it so that it returns the output you need

Please provide more clue about your code : what are you trying to achieve ?

To print a variable without end of line you can just use

printf "$var"

also

echo "$var\c"

But less portative since depending on the implementation of the "echo" command you may have to use additionnal option ( -n or -e )

I can reproduce your problem if there is a carriage-return character in the middle of the line. We can remove unwanted carriage-return characters with a "tr" command.

valu_saved=`echo "${b}" | tr -d '\r' | awk '{print $2}'`   #Get value VALU_SAVED

Depending on how your script actually reads the record we may be able to remove the bad character earlier in the loop.

If this does not fix the issue, please post a few sample data lines which have been displayed with the following "sed" enquiry (which makes control codes visible).

sed -n l myfilename