Truncate or Roundoff a calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Truncate or Roundoff a calculation
# 1  
Old 05-18-2012
Truncate or Roundoff a calculation

Hi friends,

I have a file of this kind

Code:
chr1	3180929	3181465	48
chr1	3196182	3196586	26
chr1	3343498	3343814	25
chr1	3351126	3351383	16

I did some calculations on the 4th column using the following command.

Quote:
awk '{(v=($4/16289685)10^6); {print $0"\t"v}}' input.txt > output.txt
Code:
chr1	3180929	3181465	48	2.94665e-061000000
chr1	3196182	3196586	26	1.5961e-061000000
chr1	3343498	3343814	25	1.53471e-061000000
chr1	3351126	3351383	16	9.82217e-071000000
chr1	3410973	3411501	97	5.95469e-061000000

I would like to truncate my fifth column to 5 digits after the decimal. But, I want that output directly from the calculation not after printing the 5th column and truncating digits in the output file.

This is because, if we do the truncation after calculation, the value after e- is different and might yield different results.

All thoughts appreciated.
# 2  
Old 05-18-2012
[edit] maybe not. Thinking.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-18-2012
Code:
{ v=sprintf("%f", v); split(v, a, "e"); v=a[1]; print $0"\t"v; }

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 05-18-2012
Thanks for your solution, Corona!

It print 0.00000 in the 5th column.
# 5  
Old 05-18-2012
Code:
 awk '{(v=($4/16289685)10^6); {split(v, a, "e"); v=a[1]; print $0"\t"v}}'

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Truncate terminal output

Hello Using BASH under Ubuntu 10.4 lts + XMonad My script is almost perfect but i'm stuck at the last hurdle: I need a sort of modified 'echo' which truncates its output to the width of the terminal I can find the terminal's width with 'stty size' but i dont know how to then convert input... (2 Replies)
Discussion started by: scyptnex
2 Replies

2. Shell Programming and Scripting

Truncate string variable

Hi, I want to truncate a string variable, returned in the script. In perl I used the below and it worked. BRNo=BR12345 $BR = substr($BRNo, 2, 7) How can I do it in sh. Thanks ! (8 Replies)
Discussion started by: script2010
8 Replies

3. Shell Programming and Scripting

Truncate file name to 40 characters

Hello all. I would like to make a script (or two shell scripts) that will do the following. I need the maximum file name and directory name to be 38 characters long. As well, if shortening the file name ends up making all of the files in that directory have the same name, then I would like... (9 Replies)
Discussion started by: marcozd
9 Replies

4. Shell Programming and Scripting

Truncate table

Hi In unix able to connect to oracle database and create table ,when rerun ,if table exist ,truncate that table.Any idea how to do that a.sh ---- sqlplus -s datadmin/password <<EOF create table xx(col1 number, col2... ); exit; EOF I... (1 Reply)
Discussion started by: mohan705
1 Replies

5. UNIX for Dummies Questions & Answers

How can i truncate filenmes?

I am using FC6 just in case it matters, though i hope it doesn't. If i have a file or some files that i want to truncate the filename of, so that it is only a certain number of characters in length, how would i do that on the command line? Also, just to make it more interesting, say i... (11 Replies)
Discussion started by: Calum
11 Replies

6. UNIX for Advanced & Expert Users

*** Truncate certain field ***

I have a file in which I need to truncate 15th field to have only one character like Put --> P and if i have no value in 15th field, it should be "O" (Other) would really appreciate the reponses, thnx in advance:b: (2 Replies)
Discussion started by: sannmayaz
2 Replies

7. Shell Programming and Scripting

Truncate File contain

I have one file which first line is blank and second line has some data. $cat filename output: 30-MAY-07 I want to store 30-MAY-07 value in one variable. for that I wrote var="`head -2 filename`" It will give that result but I want to truncate the first line which is blank. plz help. (2 Replies)
Discussion started by: rinku
2 Replies

8. Shell Programming and Scripting

How to truncate as filesize?

Hello everybody it's me again. I have a procces that is writing in a 'file1' automatically but i want to truncate 'file1' to a filesize 'x' that mean if the 'file1' size is 'x' i want to delete the first lines while the last lines are being writed, that have sence? in the process are an... (1 Reply)
Discussion started by: Lestat
1 Replies

9. UNIX for Dummies Questions & Answers

truncate wtmp

I have AIX5.1 I have been trying to learn how to truncate the /var/adm/wtmp file. I have seen several things on google actually but don't quite understand. I also searched your forums but couldn't find it. one says this ">/var/adm/wtmp Is that all I do? I have a seperate question also. I was... (1 Reply)
Discussion started by: rocker40
1 Replies

10. UNIX for Dummies Questions & Answers

Truncate what is It?

what does this command do ? as in does this command just make sure everything in the file is executed? or does it flush the file? Actually this is used on a file in a progress database but I believe it is a unix command? (2 Replies)
Discussion started by: rocker40
2 Replies
Login or Register to Ask a Question