Working With Values After Decimal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Working With Values After Decimal
# 1  
Old 12-23-2011
Working With Values After Decimal

I have two files which have to be compared. One of them has leading & trailing zeroes in certain fields.

Code:
file1
----
John,Rambo,20100101,2119.5,3302.39,100.07,22211.0
 
file2
----
John,Rambo,20100101,000002119.50,0003302.39,00000.07,000022211.00

I am thinking of using diff to compare these files but that would need removing the leading and trailing zeroes. I have been able to remove the leading zeroes. Its the other part where i am stuck

How can i convert .50 to .5 / .00 to .0 & leave .39 as .39 / .07 as .07 ? This is column specific.

---------- Post updated at 05:10 AM ---------- Previous update was at 05:03 AM ----------

Converting .00 to .0 is done. Now i need to see how to convert .X0 to .X

Code:
awk 'BEGIN {OFS=FS=","} {if($7) sub(/\.00/,"\.0"); print}' infile

# 2  
Old 12-23-2011
Try:
Code:
awk -F, -vOFS="," '{for (i=1;i<=NF;i++)sub("0$","",$i)}1' infile

shorter:
Code:
awk '{gsub("0,",",");sub("0$","")}1' infile

# 3  
Old 12-23-2011
@bartus11 These are removing everything after decimal i.e changing 5.0 to 5. Converts 5.20 to 5.2 & 5.0 to 5.
# 4  
Old 12-23-2011
Try:
Code:
perl -pe 's/(\..)0,/\1,/g;s/(\..)0$/\1/' infile

# 5  
Old 12-23-2011
Cannot use perl unfortunately. Can you please suggest an awk/sed solution ?
# 6  
Old 12-23-2011
Code:
sed 's/\(\..\)0,/\1,/g;s/\(\..\)0$/\1/' infile

# 7  
Old 12-23-2011
@bartus11 Thanks it worked fine. But there is another problem with the method i used to remove leading zeroes. It is removing the entire ine when the colum has only zeors i.e. instead of converting the data from 0000000.00 to 0.0, the command is removing all the rows having 0000000.00 in the column being looked for. Why is this happening ? Any alternate soln ?

Here is the method i used to remove leading zeros

Code:
awk -F, '$7=sprintf("%.2f", $7)' OFS=, infile

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare decimal values in bash?

Hi, I am facing some error while doing the comparision between 2 decimal values in bash. Pl help me out. I have tried using below scripts. But its giving me error. 1)amt=12.3 opn_amt=12.5 var=$(awk 'BEGIN{ print "'$amt'"<"'$opn_amt'" }') if ;then echo "correct" else echo "Wrong"... (3 Replies)
Discussion started by: Siba Tripathy
3 Replies

2. Shell Programming and Scripting

How compare decimal values?

I have 2 files say tp1.txt and tp2.txt having following data cat tp1.txt abc,2.20,IN20 acb,3.15,DN10 bca,3,RD10 cat tp2.txt alv,1.00,IN20 aaa,4.05,DD10 abb,5.50,RD12 i want to compare the values on 2nd field of both the file, if value of first tp1.txt is greater than value... (3 Replies)
Discussion started by: ranabhavish
3 Replies

3. Shell Programming and Scripting

Rounding off decimal values

Hi Friends, This is my last post for today. My input file is chr1 100 200 chr1 123 300 chr1 300 400 chr1 420 520 chr10 132344343 132348674 When I try using this command awk '{v=($3+$2)/2; print $0"\t"v}' 1 This is my output chr1 100 200 150 chr1 123 300 211.5 (2 Replies)
Discussion started by: jacobs.smith
2 Replies

4. Shell Programming and Scripting

How to get decimal values ?

Hi All, In my script I've written like this- c=$( expr 100 / 3);echo $c The output coming is 33. but I want to see 33.33, decimal values too. How to get that? Thanks, Naresh (3 Replies)
Discussion started by: NARESH1302
3 Replies

5. Shell Programming and Scripting

round decimal values and use in loops

i have a file in which 3 values are stored like --4.72 4.42 3.86 what i wanna do is that take read each value and compare with a fixed value say 5 but cant do so as i am getting an error for the same. please check the code cat /home/nsadm/auto/Logging/uptime.txt| while read a b c do if... (2 Replies)
Discussion started by: gemnian.g
2 Replies

6. Shell Programming and Scripting

How to sum up two decimal values?

I am running the following script : cat ind_sls_extr_UX.out_sorted | while read each_rec do count=`echo "${each_rec}" | cut -c1-2` if then final_amount=0 amount=`echo "${each_rec}" | cut -c280-287` echo "${amount}" final_amount=`expr ${amount} + ${amount}` ... (7 Replies)
Discussion started by: mady135
7 Replies

7. Shell Programming and Scripting

print decimal values

Hi guys I'm trying to print average of 2 columns. awk '{print ($1+$2)/2}' file.txt Its printing average but not giving decimal values its giving 3.05521e+08 instead of 305521.... I tried %f to print float values but not quiet connected Could you help plz:confused: (5 Replies)
Discussion started by: repinementer
5 Replies

8. Shell Programming and Scripting

Evaluating Decimal values

How can I evaluate a decimal value in an if statement? echo "Enter limit:" read limit (enter a decmal value, ie: 2.5) decimallimit=`echo $limit+0|bc|quit` echo $decimallimit if then echo $decimallimit else echo "failed" fi (4 Replies)
Discussion started by: larrys721
4 Replies

9. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies
Login or Register to Ask a Question