awk: setting decimal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: setting decimal
# 1  
Old 03-28-2011
awk: setting decimal

hello,

when I type the following command

Code:
awk -v varc="$i"  '{ new=($1*varc)} { print new }'

it gives outputs in 5 decimal. How can I set my outputs to 9 decimal by using awk.

thanks

Last edited by Scott; 03-28-2011 at 09:13 AM.. Reason: Code tags
# 2  
Old 03-28-2011
Hi.

Use printf instead of print.

i.e.
Code:
{printf "%.9f\n", new}

I imagine it only gives you 5 because that's all the precision there is?
This User Gave Thanks to Scott For This Post:
# 3  
Old 03-28-2011
hi scottn thank you for your quick reply. My last question is that

for two variable, following line does not work
Code:
awk -v varc="$i"  '{ a1new=($1*varc); a2new=($2*varc)} { printf "%.9f\n, a1new" "a2new " }'

what is wrong ?

Last edited by Scott; 03-28-2011 at 09:38 AM.. Reason: Please use code tags
# 4  
Old 03-28-2011
If you have two values you need two format parameters
Code:
{ printf "%.9f %.9f\n", a1new, a2new }

This User Gave Thanks to Scott For This Post:
# 5  
Old 03-28-2011
it works. many thanks
# 6  
Old 03-28-2011
or:
Code:
awk -v varc="$i" -v OFMT='%.9f' '{ a1new=($1*varc); a2new=($2*varc)} { print a1new, a2new}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Hexdecimal to Decimal in Solaris awk

Hi there, i want to use this Linux-script on a Solaris System to check the fragmentation Level of ZFS-DataSets: #!/bin/sh zdb -ddddd ${1} | awk --non-decimal-data \ ' /Indirect blocks/ { file_number++; next_block = 0; } /L0/ { split($3, fields, ":"); this_block =... (10 Replies)
Discussion started by: Ragesm
10 Replies

2. UNIX for Dummies Questions & Answers

How to set decimal place in awk?

Dear all, I have a data test.txt as below. X22.30799720_T cg03868770 -0.5645412582127 2.4084685750406e-175 X22.30781182_A cg03868770 -0.5620426397492 3.5818034129169e-172 X22.30780724_C cg03868770 -0.5616890165605 2.9765569717858e-168 what I want is: X22.30799720_T cg03868770... (3 Replies)
Discussion started by: forevertl
3 Replies

3. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

4. Shell Programming and Scripting

awk decimal point numbers matching

Hi All, Can some one help me in identifying the significance of character "$" ,Which is playing critical role in matching decimal point numbers as below. $ echo "01#.01"|awk '{if ($0 ~ /^+(\.*)?$/) print}' $ echo "01#.01"|awk '{if ($0 ~ /^+(\.*)?/) print}' 01#.01 $ Regards, Rmkganesh. (3 Replies)
Discussion started by: rmkganesh
3 Replies

5. Shell Programming and Scripting

Awk - Summation in Proper decimal Format

Hi I am executing below command to do summation on 46th coloumn. cat File1| awk -F"|" '{p += $46} END { printf"Column Name | SUM | " p}' I am getting output as Column Name | SUM | 1.01139e+10 Here I want output in Proper decimal format. Can someone tell me what change is required for same? (1 Reply)
Discussion started by: sanranad
1 Replies

6. Shell Programming and Scripting

using awk to strip out decimal point and trailing integers

Hey guys, I've got an awk string that does a simple calculation which works well. However, I'd really like to be able to strip the decimal point and the trailing ints from it - any ideas on how I'd change the awk string to be able to do that ? I've changed it countless times and each time it... (14 Replies)
Discussion started by: jimbob01
14 Replies

7. UNIX for Dummies Questions & Answers

awk for scientific notion and decimal combined data

Dea all, I have a question. I have a column of numbers with scientific notion and decimal combined data. I want to print it only if the number <0.05 using awk. however the very small numbers with scientific notion is not selected. Do any one know how to solve it? Thanks! example as below: ... (4 Replies)
Discussion started by: forevertl
4 Replies

8. Shell Programming and Scripting

Four decimal places with awk

i have a script in which awk prints "($2-1700)/10000" and the answer is -0.07,but i want the answer in 4 decimal places. that is -0.0700. How can i sue awk to get my results in four decimal places (4 Replies)
Discussion started by: tomjones
4 Replies

9. Shell Programming and Scripting

awk/nawk returning decimal values?

Hi Running a specific nawk statement over a 17m lines files returns the following: /bin/nawk: not enough args in ..... input record number 1,25955e+06, file test.1 source line number 1 I'd like to report the line number (in bold above) in decimal not floating so that i can spot it out. ... (1 Reply)
Discussion started by: moutaye
1 Replies

10. Solaris

awk/nawk returning decimal values?

Hi Running a specific nawk statement over a 17m lines files returns the following: /bin/nawk: not enough args in ..... input record number 1,25955e+06, file test.1 source line number 1 I'd like to report the line number (in bold above) in decimal not floating so that i can spot it out. ... (1 Reply)
Discussion started by: moutaye
1 Replies
Login or Register to Ask a Question