Round off Number in File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Round off Number in File
# 1  
Old 02-02-2015
Round off Number in File

Hi Guys,

i am having a csv file where i need to round off numerical column to 2 decimal precision in specific columns. i need to ignore the first two line i.e the header columns and manipulate rest of the lines of the csv file. My columns are specific i.e i need to round off only 2nd,4th and 6th field of the file without affecting other fields.

Code:
heading
head1,head2,head3,head4,head5,head6
aa,12,bb,-123,cc,0
bb,-12.456666,zz,-123.23333,cc,12.1

i need the below output

Code:
heading
head1,head2,head3,head4,head5,head6
aa,12.00,bb,-123.00,cc,0.00
bb,-12.45,zz,-123.23,cc,12.10

I have tried with below

Code:
head -2 file.csv>file.tmp

awk 'BEGIN{OFS=FS=","}
{
 $2=sprintf("%.2f",$2) 
$4=sprintf("%.2f",$4)
$6=sprintf("%.2f",$6)  
}1' file.csv >file_conv.tmp

tail +2 file_conv.tmp>>file.tmp
mv file.tmp > file.csv

# 2  
Old 02-02-2015
And what did it do?
# 3  
Old 02-02-2015
Try

Code:
awk -F, 'NR>2{ for ( i = 2 ; i<=6; i=i+2 ) { $i=sprintf("%.2f",$i);   }}1' OFS="," file

# 4  
Old 02-02-2015
Try this small change to your own code:
Code:
awk 'BEGIN{OFS=FS=","}
NR>2 {
  $2=sprintf("%.2f",$2) 
  $4=sprintf("%.2f",$4)
  $6=sprintf("%.2f",$6)  
}1' file.csv

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-02-2015
Quote:
Originally Posted by senhia83
Try

Code:
awk -F, 'NR>2{ for ( i = 2 ; i<=6; i=i+2 ) { $i=sprintf("%.2f",$i);   }}1' OFS="," file

Just a few suggestion its just a sample file my csv is having more than 6 fields so 6th field is not the last field and also my file size is huge having lines of more than 2 lakh will it be faster using awk

---------- Post updated at 12:45 PM ---------- Previous update was at 12:43 PM ----------

Quote:
Originally Posted by Scrutinizer
Try this small change to your own code:
Code:
awk 'BEGIN{OFS=FS=","}
NR>2 {
  $2=sprintf("%.2f",$2) 
  $4=sprintf("%.2f",$4)
  $6=sprintf("%.2f",$6)  
}1' file.csv

Hi is it way to perform in better way i mean taking performance in do consideration since i would process more than 2 lakh line records
# 6  
Old 02-02-2015
Quote:
Originally Posted by rohit_shinez
Just a few suggestion its just a sample file my csv is having more than 6 fields so 6th field is not the last field and also my file size is huge having lines of more than 2 lakh will it be faster using awk
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.. It doesnt matter how many fields you have,, this script should work as long as you know which fields to round...
# 7  
Old 02-02-2015
Quote:
Originally Posted by rohit_shinez
[..]
Hi is it way to perform in better way i mean taking performance in do consideration since i would process more than 2 lakh line records
Hi rohit_shinez, 200,000 lines isn't that much. I would just try it out and see if it is fast enough...
This User Gave Thanks to Scrutinizer For This Post:
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