Round off Number in File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Round off Number in File
# 8  
Old 02-02-2015
Quote:
Originally Posted by senhia83
I didnt get your point, if you want all the even number fields to be rounded, just replace the 6 in the for loop by NF , ,, awk is the fastest in these type of manipulations as I know..
ideally its not all the even fields i would say its like some specific fields in my file but not the 6th field as last filed

for taking consider the below file
Code:
heading
head1,head2,head3,head4,head5,head6
aa,12,bb,-123,cc,0,aa,12,bb,-1231231.111,cc,0,aa,12,bb,-123,cc,abnnn

---------- Post updated at 12:51 PM ---------- Previous update was at 12:50 PM ----------

Quote:
Originally Posted by Scrutinizer
Hi rohit_shinez, 2 lakh isn't that much. I would just try it out and see if it is fast enough...

I mean will there be any better approach apart from mine
# 9  
Old 02-02-2015
Quote:
Originally Posted by rohit_shinez
i[..]
I mean will there be any better approach apart from mine
It seems like a good approach to me..
This User Gave Thanks to Scrutinizer For This Post:
# 10  
Old 02-02-2015
Quote:
Originally Posted by Scrutinizer
It seems like a good approach to me..
Is it possible to make use of the snippet in generic function way like

calling the function name say round_number 0.2decimal col2 col4 col5 col7 ....
# 11  
Old 02-02-2015
Try also
Code:
awk     'BEGIN          {OFS=FS=","}
         NR>2           {$2+=0.0001
                         $4+=0.0001
                         $6+=0.0001
                        }
         1
        ' CONVFMT="%.2f" file
heading
head1,head2,head3,head4,head5,head6
aa,12.00,bb,-123.00,cc,0.00
bb,-12.46,zz,-123.23,cc,12.10

---------- Post updated at 19:23 ---------- Previous update was at 19:14 ----------

Strange enough, this is slower than the explicit sprint approach. Of course, it has to run through the format conversion routines after each of the addition operations...
This User Gave Thanks to RudiC For This Post:
# 12  
Old 02-02-2015
Thanks rudic

As I said in my previous post will it be able to make it in generic function based script like

Round_function decimalvalue col1 col2 ..... ...

So that I can use wat decimal precision to round off and which all columns needs to be changed
# 13  
Old 02-02-2015
Try
Code:
awk     'BEGIN          {OFS=FS=","}

         function roundit(FIELDS)
                        {split (FIELDS, TMP)
                         for (t in TMP) $(TMP[t])+=0.0001
                        }

         NR>2           {roundit("2 4 6")
                        }
         1
        ' CONVFMT="%.2f" file

Surprisingly enough, this nearly cuts the time consumed in half ...!
This User Gave Thanks to RudiC For This Post:
# 14  
Old 02-02-2015
Round it function needs to called like this

Roundit decimal_precision fields Filename

So that i can use it in generic way of how many decimal precision you need and which all columns you want to make the changes

Code:
awk     'BEGIN          {OFS=FS=","}

         function roundit(FIELDS)
                        {split (FIELDS, TMP)
                         for (t in TMP) $(TMP[t])+=0.0001
                        }

         NR>2           {roundit("$1 $2 $3")
                        }
         1
        ' CONVFMT="%.2f" $4


Last edited by rohit_shinez; 02-04-2015 at 11:31 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Round up -FASTA file

I have the following script: awk 'FNR==NR{s+=$3;next;} { print $1 , $2, 100*$3/s }' and the following file: >P39PT-1224 Freq 900 cccctacgacggcattggtaatggctcagctgctccggatcccgcaagccatcttggatatgagggttcgtcggcctcttcagccaagg-cccccagcagaacatccagctgatcg >P39PT-784 Freq 2... (2 Replies)
Discussion started by: Xterra
2 Replies

2. Shell Programming and Scripting

Using sed to round a number

Hey everyone, I was wondering if i am able to write a sed command to round a number to two decimal places. So for example: 1.58674 would be 1.58 I just want to chop off the numbers to the right of the second digit after the period. I know this is probably trivial but the closest i got was... (8 Replies)
Discussion started by: GmGeubt
8 Replies

3. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

4. Shell Programming and Scripting

Round off the a Decimal value.

HI, I have a script which is used to calculate the Memory & CPU utilization a server. memx=`ssh -l siebel1 ${f} /usr/sbin/prtconf|grep -i 'Memory size'|tr -s " "|/usr/xpg4/bin/awk -F" " '{print $3 * 1024}'` v5=`ssh -l siebel1 ${f} vmstat 1 2 | tail -1 | tr -s " " | /usr/xpg4/bin/awk -v... (3 Replies)
Discussion started by: dear_abhi2007
3 Replies

5. Shell Programming and Scripting

Round Robin Distribution of Contents of file to 3 files

Hi I need to create a script that distributes in round robin fashion the contents of a file to 3 files. The number of lines in a content of file can vary from 1-n(Each line is just a one letter word).The entire lines needs to get distributed into 3 files ( The order doesnt matter) , at... (5 Replies)
Discussion started by: police
5 Replies

6. Shell Programming and Scripting

it's urgent ! round number in perl scripting

my $number = 12.345673412 I need 3 digits after decimal or after dot(.) i mean , i need only 12.345 I used int(), ceil(), floor() but it gives me only 12 I need it. (10 Replies)
Discussion started by: pritish.sas
10 Replies

7. Shell Programming and Scripting

round a number

In a shell script - How do I round a decimal number (contained in a variable) to the nearest whole number? (2 Replies)
Discussion started by: achieve
2 Replies

8. Shell Programming and Scripting

round in KSH

Is there an easy way to round a number up in Korn shell? ie. 10.4 --> 11 Thanks. (6 Replies)
Discussion started by: here2learn
6 Replies

9. UNIX for Dummies Questions & Answers

how to round a value

Hello, In a unix shell script,i want to round a variabele to a nearest number Ex: set count=104.4 How can i round that to 105.? Thanks, Sateesh (2 Replies)
Discussion started by: kotasateesh
2 Replies
Login or Register to Ask a Question