Retain upto 2 decimal point for numeric values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retain upto 2 decimal point for numeric values
# 1  
Old 10-11-2012
Retain upto 2 decimal point for numeric values

Hi

I have a big file with lines like below

<tr align="center" bgcolor="SEASHELL"><td>94% </td><td>4.62178 </td><td>73.4375 </td></tr>
<tr align="center" bgcolor="SEASHELL"></td><td>97% </td><td>3.2962 </td><td>125 </td></tr>

I want to format the file such that i get like below. i.e retain upto two point decimal where it sees decimal point

<tr align="center" bgcolor="SEASHELL"></td><td>94% </td><td>4.62 </td><td>73.43 </td></tr>
<tr align="center" bgcolor="SEASHELL"></td><td>97% </td><td>3.29 </td><td>125 </td></tr>

Last edited by ningy; 10-11-2012 at 03:47 AM..
# 2  
Old 10-11-2012
Code:
sed 's/\([0-9]\{1,\}[.][0-9][0-9]\)[0-9]\{1,\}/\1/g' file

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 10-11-2012
awesome !!works well . I completely dont understand it though..
# 4  
Old 10-11-2012
we can do it like this also..

although it is almost same as elixir...Smilie

Code:
sed 's/\([0-9]\+[.][0-9][0-9]\)[0-9]\{2,\}/\1/g' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Convert a numeric to 2 decimal point value

Hi , I have a file which contains text like A|Mau|Code|12|Detail B|Mau|Code|20|Header I want to write a command using awk which will output A|Mau|Code|12.00|Detail B|Mau|Code|20.00|Header I used a command like awk -F"|" {printf "%s|%s|%s|%.2f|%s",$1,$2,$3,$4,$5}' which does the... (4 Replies)
Discussion started by: LoneRanger
4 Replies

2. UNIX for Beginners Questions & Answers

How to add following decimal point to a CSV value?

hi there I being trying to figure out way to add " .0" to an integer value in a csv using sed or awk with out success. just as a work around for 2147483647 32 bit limitation that influxdb is currently having the data base will accept values and work fine if it has the XXX.0 ... (7 Replies)
Discussion started by: sash99
7 Replies

3. Shell Programming and Scripting

How to round up value upto 2 decimal places using sed?

Please help me in rounding up value upto 2 decimal palces using sed command #!/usr/bin/bash a=15.42 b=13.33 c=`echo $a*$b |bc -l` echo $c above code is is giving output "205.5486" but i want the output as "205.55" Thank you... (15 Replies)
Discussion started by: ranabhavish
15 Replies

4. Shell Programming and Scripting

remove directories with two digits after decimal point

Hi everyone, I am new here and generally not experienced with linux. My question must be easy, but as for now I have no idea how to do it. I have lots of directories with numerical names, e.g. 50 50.1 50.12 etc. What I want is to leave directories with no or single digit after the decimal... (2 Replies)
Discussion started by: cabaciucia
2 Replies

5. Shell Programming and Scripting

Insert decimal point for numbers

Hi In Unix, I have a file with some numbers like : 45600 12345 I want to insert a decimal point for these numbers based on user input. If the input is 2, the numbers should be changed to 456.00 123.45 If the input is 3, the numbers should be changed to 45.600 12.345 Can... (2 Replies)
Discussion started by: yoursdivu
2 Replies

6. Shell Programming and Scripting

Comparing two numbers with decimal point

How to compare two numbers with decimal points ? Is there a way in bash to do this? (33 Replies)
Discussion started by: kinny
33 Replies

7. Shell Programming and Scripting

PERL:How to convert numeric values txt file to PACKED DECIMAL File?

Is there any way to convert numeric values txt file to PACKED DECIMAL File using PERL. Regards, Alok (1 Reply)
Discussion started by: aloktiwary
1 Replies

8. Shell Programming and Scripting

Insert a decimal point

Hi all. Using /bin/sh on an HPUX system. I want to place a decimal in the field 2 charactors from the right (yes, converting to currency). The field lengths are variable. Here's what I'm doing: exec < filename while read FIELD1 FIELD2 do FIELD1="echo $FIELD1 | sed 'syntax that will... (4 Replies)
Discussion started by: lyoncc
4 Replies

9. Shell Programming and Scripting

how to get rid of decimal point?

Hi, I have input with decimal point ( 9.99 ) for hours variable hrs. I need to change it to seconds. Here is my code: secs=`/usr/ucb/echo $hrs*3600 |bc` But I don't want to see the decimal point. I can use awk to trim it if there is one. I am just wondering if there is better standard... (2 Replies)
Discussion started by: cin2000
2 Replies

10. Shell Programming and Scripting

Floating point numeric comparisions in bash

Hi, I am trying to compare 2 floating point numbers 0.8 and 0.15 using bash and get the largest of the two. Can anyone advise. (2 Replies)
Discussion started by: borncrazy
2 Replies
Login or Register to Ask a Question