Remove carriage return from the variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove carriage return from the variable
# 1  
Old 12-06-2013
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 is:

Code:
 
var1=$( some command )

echo $var1
2.80985e+09
 
var2=`expr $var1 + 10000000`
expr: non-integer argument

printf "%f\n" $var1
2809850000.000000

printf "%s\n" $var1
2.80985e+09

If I use tr -dc '[:digit:]' with this printf output, it will remove all non character not only return carriage at the end.

So for first notation it will give me a number like 2809850000000000 and for second 28098509 and both incorrect. I need a display like 2809850000 so tr could only remove return carriage. So which parameter of printf would produce this notation? (w/o exponential nor decimal notation)

Thanks

Last edited by Meacham12; 12-06-2013 at 04:23 AM..
# 2  
Old 12-06-2013
You could use awk also
Code:
$ a="2.80985e+09\r\r\n"
$ echo $a
2.80985e+09\r\r\n

$ b=$(awk -v var=$a 'BEGIN{print var+10000000}')
$ echo $b
2819850000

# 3  
Old 12-06-2013
Actually I have a lot of variables, 5,6 sometimes even 10. So I should perform arithmetic operations with all of them.

So I need something like (after your example)

Code:
a=20000000000000000000^m
b=300000000000000000000^m
c=40000000000000000000^m
d=250000000000000^m

e=$( awk -v var1=$a var2=$b var3=$c var4=$d 'BEGIN{print var1 * var2 / var3 + var4}' )

But this example above didn't work. Do you have any suggestion for this?
# 4  
Old 12-06-2013
Quote:
Originally Posted by Meacham12
Actually I have a lot of variables, 5,6 sometimes even 10. So I should perform arithmetic operations with all of them.

So I need something like (after your example)

Code:
a=20000000000000000000^m
b=300000000000000000000^m
c=40000000000000000000^m
d=250000000000000^m

e=$( awk -v var1=$a var2=$b var3=$c var4=$d 'BEGIN{print var1 * var2 / var3 + var4}' )

But this example above didn't work. Do you have any suggestion for this?
I didn't see calculation part -v is missing
Code:
e=$( awk -v var1=$a -v var2=$b -v var3=$c  -v var4=$d 'BEGIN{print var1 * var2 / var3 + var4}' )

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 12-06-2013
Quote:
Originally Posted by Akshay Hegde
I didn't see calculation part -v is missing
Code:
e=$( awk -v var1=$a -v var2=$b -v var3=$c  -v var4=$d 'BEGIN{print var1 * var2 / var3 + var4}' )

Oh I see. I need a separate -v for each variable. It worked like that. Thanks buddy. This saved my life Smilie SmilieSmilie
# 6  
Old 12-06-2013
Quote:
Originally Posted by Meacham12
Oh I see. I need a separate -v for each variable. It worked like that. Thanks buddy. This saved my life Smilie SmilieSmilie
Yes, it's for defining variable

from man page of awk

Code:
-v var=val
       --assign var=val
              Assign the value val to the variable var, before execution of the program begins.  Such variable values are  available  to  the
              BEGIN block of an AWK program.

# 7  
Old 12-06-2013
Please be careful - that trick with awk works because when converting a field to a numerical value, awk will start at the beginning of the field and scan until an unconvertible char is encountered - which is "\" "r" in post#2. Yes - it's not <CR> but two normal chars.
Code:
 a="2.80985e+09\r\r\n"
echo $a xxx
2.80985e+09\r\r\n xxx
a="2.80985e+09"$'\r'
echo $a xxx
 xxx985e+09

The second assignment really has a <CR> char in it, making echo overprint the first chars.
To remove the <CR>, you can try this bashism (pattern substitution expansion):
Code:
echo ${a/$'\r'} xxx
2.80985e+09 xxx

or e.g.
Code:
echo $a  |hd
00000000  32 2e 38 30 39 38 35 65  2b 30 39 0d 0a
echo $a |tr -d '\r' |hd
00000000  32 2e 38 30 39 38 35 65  2b 30 39 0a

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove carriage return and append the next line

Hi All, My requirement is to remove the carriage return in from the lines which i am reading if the length is lesser than 1330 and append the next line with it. Below is the realistic example of file structure. Input file: Blah blah blah blah Blah blah blah blah Blah blah blah blah Blah... (16 Replies)
Discussion started by: mad man
16 Replies

2. Shell Programming and Scripting

Remove Carriage Return (CRLF) within double quotes

How to remove Carriage Return (CRLF) within double quotes in a file. There are multiple CRLFs within double quotes. We are on Ubuntu 14.04.2 LTS. The file that we are importing is a csv file from unix to windows and the file was formatted to unix2dos. Therefore all lines in the file all have... (12 Replies)
Discussion started by: covina
12 Replies

3. 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

4. Shell Programming and Scripting

Awk to remove carriage return from 65th field

Hi, I have a pipe delimited file. There are around 700 columns in the file. The 65th column has carriage return which is causing read issue with our ETL process. I would like to replace the new line characters in 65th field with "nothing" i have return the following code and need help to... (7 Replies)
Discussion started by: pinnacle
7 Replies

5. Shell Programming and Scripting

Remove carriage return in a record

Hi all gurus, I need help in removing carriage return existed within a record delimited by pipe <|>. Sample: A_01|Test1|Testing1|Remarks1 A_02|Test2|Test ing2|Remarks2 A_03|Test3|Testing3| Remarks3 Desire output: A_01|Test1|Testing1|Remarks1 A_02|Test2|Testing2|Remarks2... (10 Replies)
Discussion started by: agathaeleanor
10 Replies

6. UNIX for Dummies Questions & Answers

Remove ^M (carriage return) with string manipulation

Hello, I want remove ^M at end of my files line if I use command : tr -d '\r' <inp>out it work fine but get I the same result by manipulating the string ? I want this because in my text file I manipulate some other part I have input "the cat^M" I want output "the cat" I have made... (3 Replies)
Discussion started by: aquila_1
3 Replies

7. Shell Programming and Scripting

sqlplus returning value - remove carriage return '\r' - Please help

Guys - Simple code, i am trying to get a number back from sqlplus call to a query. After that, i need to use that number in a loop. --------------------------------- #!/bin/ksh VALUE=`sqlplus -silent sh/password@sh <<END set pagesize 0 feedback off verify off heading off echo off select... (10 Replies)
Discussion started by: sunshine1974
10 Replies

8. 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

9. UNIX for Dummies Questions & Answers

To remove carriage return between the line

Hi, I have a situation where I need to remove the carriage return between the lines. For.eg. The input file: 1,ad,"adc sdfd",edf 2,asd,"def fde",asd The output file should be 1,ad,adc sdfd,edf 2,asd,def fde,asd Thanks Shash (5 Replies)
Discussion started by: shash
5 Replies

10. 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
Login or Register to Ask a Question