Rounding decimal values in a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rounding decimal values in a column
# 1  
Old 05-17-2013
Rounding decimal values in a column

Hi,
I wanted to round all the values in a column to nearest integer. I have multiple files with only two columns and I want to round values in column 2. e.g
input_file
Code:
                     A1   23.971578       A2   34.624976       A3   46.403446       A4   375       A5   1       A6   3       A7   351.475553       A8   18       A9   7.537485

I want outfile_file as:
Code:
                     A1   24       A2   35       A3   46       A4   375       A5   1       A6   3       A7   351       A8   18       A9   8

How can I achieve this using shell command or script? Any help will be greatly appreciated!

Thanks

Last edited by Scrutinizer; 05-18-2013 at 02:04 AM.. Reason: code tags
# 2  
Old 05-17-2013
An awk solution:
Code:
awk '{for(i=1;i<=NF;i++){if($i~/^[0-9.]+/){$i=sprintf("%0.f",$i)}}}1' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-17-2013
Thanks a lot, Yoda!
It works great!!!
# 4  
Old 05-18-2013
Using awk and no loop
Code:
awk 'NR>1 {$2=sprintf("%0.f",$2);print "A"$0}' RS="A" ORS=" "
A1 24 A2 35 A3 46 A4 375 A5 1 A6 3 A7 351 A8 18 A9 8

This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to grep for decimal values only in the second column.

Hi, I have a file in which I need to print all the lines that have decimal values in the second column. The below prints all the decimal values from the second column but I need the complete line to be printed. cat hello.out | sed 's/ */:/g' | cut -d : -f 2 | ggrep -Eo "+\.+" Can you... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. UNIX for Dummies Questions & Answers

Replace all decimal values in a column

Hi My input file looks String000002 GeneWise CW 48945 49354 . - 0 Pt=PEQU_00004; String000002 LEN NA 52125 52604 0.945751 - . PID=PEQU_00005;lvid_id=PEQ_28708; String000002 LEN CW 52125 52604 . - 0 ... (3 Replies)
Discussion started by: siya@
3 Replies

3. UNIX for Dummies Questions & Answers

Rounding off a decimal

How to round off a decimal number to higher whole number using ceil command in unix? Eg. 4.41 or 4.11 or 4.51 should be rounded off to 5. (11 Replies)
Discussion started by: SanjayKumar28
11 Replies

4. Shell Programming and Scripting

Bash Rounding to 2 decimal places

I have a number in a bash variable n, and want to round it to 2 decimal places. How can I do that? n=0.0867268 Need to have num=0.09 (1 Reply)
Discussion started by: kristinu
1 Replies

5. Shell Programming and Scripting

Rounding off decimal values

Hi Friends, This is my last post for today. My input file is chr1 100 200 chr1 123 300 chr1 300 400 chr1 420 520 chr10 132344343 132348674 When I try using this command awk '{v=($3+$2)/2; print $0"\t"v}' 1 This is my output chr1 100 200 150 chr1 123 300 211.5 (2 Replies)
Discussion started by: jacobs.smith
2 Replies

6. Shell Programming and Scripting

How to get decimal values ?

Hi All, In my script I've written like this- c=$( expr 100 / 3);echo $c The output coming is 33. but I want to see 33.33, decimal values too. How to get that? Thanks, Naresh (3 Replies)
Discussion started by: NARESH1302
3 Replies

7. UNIX for Dummies Questions & Answers

Rounding a decimal

Hi, I am currently using tcsh I am trying to round a decimal number to the ten-thousandths place For instance: 1.23456 is rounded up towards 1.2346 I am not looking for truncation, but for rounding. Anyone know how to do this with awk or expr? Thanks (2 Replies)
Discussion started by: miniwheats
2 Replies

8. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

9. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies
Login or Register to Ask a Question