AWK rounding up numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK rounding up numbers
# 1  
Old 08-12-2010
AWK rounding up numbers

Hi,

I have managed to round up numbers by using the following command:

Code:
echo "5.54" | awk '{printf "%.0f\n", $1}'

result

Code:
6

How can I round up all the numbers in a column in a file and print the lines with the new calculated totals?

Thanks,
# 2  
Old 08-12-2010
Code:
$> cat infile
0.22 0.33 0.44
2.22 3.33 4.44
$> awk '{a+=$1; b+=$2; c+=$3} END{printf("%0.f %0.f %0.f\n", a,b,c)}' infile
2 4 5

# 3  
Old 08-12-2010
Thanks.

My file has 15 columns and quite a few lines. I would like to round up the numbers of column 14 only. The other columns don't contain numbers.

The file is like this:

Code:
,fred,a,b,c,e,f,g,h,i,j,k,l,m,4.7,z,
,henry,a,b,c,e,f,g,h,i,j,k,l,m,1.4,z,
,bob,a,b,c,e,f,g,h,i,j,k,l,m,3.2,z,

The output should be:

Code:
,fred,a,b,c,e,f,g,h,i,j,k,l,m,5,z,
,henry,a,b,c,e,f,g,h,i,j,k,l,m,2,z,
,bob,a,b,c,e,f,g,h,i,j,k,l,m,4,z,

Thanks,
# 4  
Old 08-12-2010
Quote:
Originally Posted by keenboy100
Thanks.

My file has 15 columns and quite a few lines. I would like to round up the numbers of column 14 only. The other columns don't contain numbers.

The file is like this:

Code:
,fred,a,b,c,e,f,g,h,i,j,k,l,m,4.7,z,
,henry,a,b,c,e,f,g,h,i,j,k,l,m,1.4,z,
,bob,a,b,c,e,f,g,h,i,j,k,l,m,3.2,z,

The output should be:

Code:
,fred,a,b,c,e,f,g,h,i,j,k,l,m,5,z,
,henry,a,b,c,e,f,g,h,i,j,k,l,m,2,z,
,bob,a,b,c,e,f,g,h,i,j,k,l,m,4,z,

Thanks,
Try this:
Code:
awk -F, '{$15=$15==int($15)?int($15):int($15)+1}1' OFS=","

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[awk] rounding a float number?

Heyas Trying to calculate the total size of a file by reading its bitrate. Code snippet: fs_expected() { # # Returns the expected filesize in bytes # pr_str() { ff=$(cat $TMP.info) d="${ff#*bitrate: }" echo "${d%%,*}" | $AWK '{print $1}' | head -n 1 } t_BYTERATE=$((... (9 Replies)
Discussion started by: sea
9 Replies

2. Shell Programming and Scripting

printf (awk,perl,shell) float rounding issue

Hi guys, could someone throw some light on the following behaviour of printf (I'll start with info about the system and the tool/shell/interpreter versions)?: $ uname -a Linux linux-86if.site 3.1.0-1.2-desktop #1 SMP PREEMPT Thu Nov 3 14:45:45 UTC 2011 (187dde0) x86_64 x86_64 x86_64... (9 Replies)
Discussion started by: elixir_sinari
9 Replies

3. Shell Programming and Scripting

awk calculation automatically rounding off the output

I have some calculation in my script which is similar to the below example . I find that sometimes when using large decimal digits, the output gets automatically rounded off and it is affecting the program. I am not able to understand what is happening here.. awk '{ a=6.32498922 a1=6.324... (5 Replies)
Discussion started by: wanderingmind16
5 Replies

4. Shell Programming and Scripting

AWK series of numbers

Hi, I have a dataset say 1 2 3 4 5 5 6 7 6 7 8 9 I was wondering if there is a way to add another column with the following style... 1 2 3 4 xyz_1 5 5 6 7 xyz_2 6 7 8 9 xyz_3 It would be greatly appreciated if I can have an option of specifying what to write instead of xyz,... (8 Replies)
Discussion started by: jacobs.smith
8 Replies

5. Shell Programming and Scripting

awk, floating point and rounding

I had a person bring an interesting problem to me that appears to involve some sort of rounding inside awk. I've verified this with awk and nawk on Solaris as well as with gawk 3.1.5 on a Linux box. The original code fragment he brought me was thus: for (index=0; index < 1; index=index+.1) ... (4 Replies)
Discussion started by: mmyer2
4 Replies

6. Shell Programming and Scripting

Rounding issue with awk

Hi Friends, I am trying to round following number. 0.07435000 echo "0.07435000"|awk '{printf "%s\n",$1*100}'|awk '{printf "%.2f\n",$1}' It returns: 7.435 It should return: 7.44 Any suggestion please? Thanks, Prashant (2 Replies)
Discussion started by: ppat7046
2 Replies

7. UNIX for Dummies Questions & Answers

Annoying rounding issue in awk

Hello I am getting this very annoying issue in awk: awk '{a=12825;b=a*1.25; print b}' test 16031.2 Thing is the multiplication result is wrong... Result should be 16031.25. I think the issue only happens on bigger numbers. What can I do to get passed this? Thanks by advance (3 Replies)
Discussion started by: Indalecio
3 Replies

8. Shell Programming and Scripting

to count their numbers using awk

suppose u have a file AAG AAG GYG HYH GTG AAG AAG HYH GYG HUH JUJ JUJ AAG so output shud have AAG = 5 GYG=2 HYH=2 HUH=1JUJ=2 (3 Replies)
Discussion started by: cdfd123
3 Replies

9. Shell Programming and Scripting

Rounding off using BC.

Hello again. I'm trying to use BC to calculate some numbers in a shell script. I want to have the numbers rounded off to 1 decimal place. for example: initsize=1566720 zipsize=4733 I'm trying to get the ratio between them. the equation is: (($initsize-$zipsize)/$initsize)*100 so... (3 Replies)
Discussion started by: noodlesoup
3 Replies

10. Shell Programming and Scripting

match numbers (awk)

i would like to enter (user input) a bunch of numbers seperated by space: 10 15 20 25 and use awk to print out any lines in a file that have matching numbers so output is: 22 44 66 55 (10) 77 (20) (numbers 10 and 20 matched for example) is this possible in awk . im using gawk for... (5 Replies)
Discussion started by: tanku
5 Replies
Login or Register to Ask a Question

Featured Tech Videos