# Round off Number in File

**URL:** <https://community.unix.com/t/round-off-number-in-file/350092>\
**Category:** Shell Programming and Scripting\
**Created:** [February 2, 2015, 12:19pm UTC](https://community.unix.com/t/round-off-number-in-file/350092 "2015-02-02T12:19:18Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![rohit\_shinez](https://community.unix.com/letter_avatar/rohit_shinez/32/6_449c533f586939b6e5f246632a46421f.png) [@rohit\_shinez](https://community.unix.com/u/rohit_shinez)\
**Post date:** [February 2, 2015, 12:19pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/1 "2015-02-02T12:19:18Z")

</div>

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.

```nohighlight
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

```nohighlight
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

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 2, 2015, 12:34pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/2 "2015-02-02T12:34:52Z")

</div>

And what did it do?

---

<div class="post-metadata">

**Author:** ![senhia83](https://community.unix.com/letter_avatar/senhia83/32/6_449c533f586939b6e5f246632a46421f.png) [@senhia83](https://community.unix.com/u/senhia83)\
**Post date:** [February 2, 2015, 12:36pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/3 "2015-02-02T12:36:00Z")

</div>

Try

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

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [February 2, 2015, 12:38pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/4 "2015-02-02T12:38:40Z")

</div>

Try this small change to your own code:

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

```

---

<div class="post-metadata">

**Author:** ![rohit\_shinez](https://community.unix.com/letter_avatar/rohit_shinez/32/6_449c533f586939b6e5f246632a46421f.png) [@rohit\_shinez](https://community.unix.com/u/rohit_shinez)\
**Post date:** [February 2, 2015, 12:45pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/5 "2015-02-02T12:45:15Z")

</div>

> [@senhia83](#):
>
> Try
> 
> ```plaintext
> 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 ----------

> [@scrutinizer](#):
>
> Try this small change to your own code:
> 
> ```plaintext
> 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

---

<div class="post-metadata">

**Author:** ![senhia83](https://community.unix.com/letter_avatar/senhia83/32/6_449c533f586939b6e5f246632a46421f.png) [@senhia83](https://community.unix.com/u/senhia83)\
**Post date:** [February 2, 2015, 12:47pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/6 "2015-02-02T12:47:03Z")

</div>

> [@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...

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [February 2, 2015, 12:49pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/7 "2015-02-02T12:49:10Z")

</div>

> [@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...

---

<div class="post-metadata">

**Author:** ![rohit\_shinez](https://community.unix.com/letter_avatar/rohit_shinez/32/6_449c533f586939b6e5f246632a46421f.png) [@rohit\_shinez](https://community.unix.com/u/rohit_shinez)\
**Post date:** [February 2, 2015, 12:51pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/8 "2015-02-02T12:51:17Z")

</div>

> [@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

```plaintext
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 ----------

> [@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

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [February 2, 2015, 12:53pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/9 "2015-02-02T12:53:08Z")

</div>

> [@rohit\_shinez](#):
>
> i[..]  
> I mean will there be any better approach apart from mine

It seems like a good approach to me..

---

<div class="post-metadata">

**Author:** ![rohit\_shinez](https://community.unix.com/letter_avatar/rohit_shinez/32/6_449c533f586939b6e5f246632a46421f.png) [@rohit\_shinez](https://community.unix.com/u/rohit_shinez)\
**Post date:** [February 2, 2015, 1:00pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/10 "2015-02-02T13:00:53Z")

</div>

> [@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 ....

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 2, 2015, 1:23pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/11 "2015-02-02T13:23:14Z")

</div>

Try also

```nohighlight
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...

---

<div class="post-metadata">

**Author:** ![rohit\_shinez](https://community.unix.com/letter_avatar/rohit_shinez/32/6_449c533f586939b6e5f246632a46421f.png) [@rohit\_shinez](https://community.unix.com/u/rohit_shinez)\
**Post date:** [February 2, 2015, 1:25pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/12 "2015-02-02T13:25:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 2, 2015, 1:34pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/13 "2015-02-02T13:34:36Z")

</div>

Try

```nohighlight
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 ...!

---

<div class="post-metadata">

**Author:** ![rohit\_shinez](https://community.unix.com/letter_avatar/rohit_shinez/32/6_449c533f586939b6e5f246632a46421f.png) [@rohit\_shinez](https://community.unix.com/u/rohit_shinez)\
**Post date:** [February 2, 2015, 1:41pm UTC](https://community.unix.com/t/round-off-number-in-file/350092/14 "2015-02-02T13:41:24Z")

</div>

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

```nohighlight
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

```
