A variable is including the Carriage Return char...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A variable is including the Carriage Return char...
# 1  
Old 05-16-2011
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.

Code:
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
Code:
`expr $valu_saved`

?

Thak you!
# 2  
Old 05-17-2011
Code:
valu_saved=`echo $b | awk '{print $2}' | xargs`

?
# 3  
Old 05-17-2011
Thank you for the note, but I'm still have the issue.

I have added
Code:
| xargs`

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

I added the following code just for validation.
Code:
 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.!
# 4  
Old 05-18-2011
Could you please just show us what output you get with a simple :
Code:
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

Code:
printf "$var"



also
Code:
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)


Last edited by ctsgnb; 05-18-2011 at 04:31 AM..
# 5  
Old 05-18-2011
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.

Code:
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).

Code:
sed -n l myfilename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Carriage return with cat variable

Hi, I wish to know how to works the carriage return with cat.. As a picture often speaks better than words, my code below : #te=`cat text.txt` (I tried this, but the same..) te=$(cat text.txt) echo $te My file text.txt below: BLABLABLABLABLALBLA BLABLABLABLABLALBLA ... (9 Replies)
Discussion started by: Arnaudh78
9 Replies

2. UNIX for Dummies Questions & Answers

Remove carriage return

I need to remove the carriage return comes inbetween the record. Need to have CR only at the end. I used the below command. tr -d '\n' < filewithcarriagereturns > filewithoutcarriagereturns But its removing all the CR and giving one line output. Input File: 12345 abcdegh... (11 Replies)
Discussion started by: srvn_saru
11 Replies

3. Shell Programming and Scripting

Remove carriage return from the variable

Hi, I try to handle very large numbers with a bash script. I run ssh command in a remote server and store the output in a local variable. But this output contains a return carriage at the end. So I try to remove it by tr But I can't figure out the right notation with printf. So my problem... (6 Replies)
Discussion started by: Meacham12
6 Replies

4. Shell Programming and Scripting

Insert carriage return on the 10th char position of each line

Hi experts, Need your help on how to insert carriage return after the 10th char position of each line in a file and then add two blank spaces after the carriage return. Example: >cat test.txt testingline dummystring samplesample teststringline Expected output should be.. ... (2 Replies)
Discussion started by: brichigo
2 Replies

5. Shell Programming and Scripting

sqlplus returns leading carriage return into a variable

I am trying to generate some scripts to help manage an Oracle database. When I check the value returned from Oracle it has a leading carriage return in the variable. Is there a way to prevent this? Is there a way to easily strip out the carriage return. See code and output below. ... (7 Replies)
Discussion started by: Panzer993
7 Replies

6. UNIX for Dummies Questions & Answers

carriage return and linefeed

hi can anyone please tell me the difference between carriage return, linefeed and newline ? (2 Replies)
Discussion started by: streetfi8er
2 Replies

7. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

8. Shell Programming and Scripting

Dont want carriage return

I have observed with print & echo, they produce carriage return <CR> or newline, after they display string next to them. Is there anyway to avoide these <CR> after the intended string is displayed? (3 Replies)
Discussion started by: videsh77
3 Replies

9. UNIX for Dummies Questions & Answers

Remove a carriage return at end of variable

Is there a command in unix to remove a carriage return character(^M) at the end of a variable value? (5 Replies)
Discussion started by: flagship99
5 Replies

10. Shell Programming and Scripting

Regex to pick up name from the following including carriage return at end of the line

has anyone got any suggestions how i would pick up the string as part of a substitution inclusive of the carriage return. ie i want to pick up <<NAME>> from the PS output but the <<; seems to be on the line before the NAME. Any ideas are appreciated! ... (3 Replies)
Discussion started by: Shakey21
3 Replies
Login or Register to Ask a Question