Sponsored Content
Full Discussion: Need help with AWK math
Top Forums Shell Programming and Scripting Need help with AWK math Post 302630965 by Corona688 on Thursday 26th of April 2012 02:21:33 PM
Old 04-26-2012
printf is a function found in many different programming language for convenient formatting of numbers and strings. Its first argument describes how to format all the arguments afterwards when printing them.

%d means 'print as integer', so it will never show a number with decimal points, sufficiently small numbers will simply become zero.

%f means 'print as floating point'.

%.2f means 'print as floating point to two decimal places'.

There's lots more. See 'man 3 printf' for details on how the arguments generally work.
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk logic and math help

Hi, My file has 2 fields and millions of lines. variableStep chrom=Uextra span=25 201 0.5952 226 0.330693 251 0.121004 276 0.0736858 301 0.0646982 326 0.0736858 401 0.2952 426 0.230693 451 0.221004 476 0.2736858 Each field either has a... (6 Replies)
Discussion started by: wyarosh
6 Replies

2. Shell Programming and Scripting

awk math operation on two files

Hi, I need your help. I've got two files and i need to add 2nd line after occurrence of "Group No X" from data2.txt to 3rd line (after occurrence of "Group No X") from data1.txt. There is the same number of "Groups" in both files and the numbers of groups have the same pattern. data1.txt Group... (2 Replies)
Discussion started by: killerbee
2 Replies

3. Shell Programming and Scripting

awk math and csv output

Hi I have this list 592;1;Z:\WB\DOCS;/FS3_100G/FILER112/BU/MPS/DOCS;;;;\\FILER112\BUMPS-DOCS\;580,116,544,878 Bytes;656,561 ;77,560 592;2;Z:\WB\FOCUS;/FS3_100G/FILER112/BU/MPS/FOCUS;;;;\\FILER112\BUMPS-FOCUS\;172,430 Bytes;6 ;0 ... (12 Replies)
Discussion started by: nakaedu
12 Replies

4. Shell Programming and Scripting

awk in horizontal and vertical math

Based on input ail,UTT,id1_0,COMBO,21,24,21,19,85 al,UTHAST,id1_0,COMBO,342,390,361,361,1454 and awk code as awk -F, '{ K=0; for(i=NF; i>=(NF-4); i--) { K=K+$i; J=J+$i;} { print K } } END { for ( l in J ) printf("%s ",J); }' I'm trying to add columns and lines in single line. line... (6 Replies)
Discussion started by: busyboy
6 Replies

5. Shell Programming and Scripting

awk evaluating a string as a math expression

Hi, I am writing a script in awk trying to replace strings that are math expressions with their result. For example, I have a file that looks like this: 5-1 32/8-1 4*12 17+1-3 I would like to get the following output: 4 3 48 15 I tried doing it the following way (using the "bc"... (8 Replies)
Discussion started by: avi.levi
8 Replies

6. Shell Programming and Scripting

awk --> math-operation in a array

Hi main object is categorize the difference of data-values (TLUFT02B - TLUFT12B). herefor i read out data-files which are named acording to the timeformat yyyymmddhhmm. WR030B 266.48 Grad 0 WR050B 271.46 Grad 0 WR120B 268.11 Grad 0 WV030B 2.51 m/s ... (6 Replies)
Discussion started by: IMPe
6 Replies

7. Shell Programming and Scripting

awk --> math-operation in data-record and joining with second file data

Hi! I have a pretty complex job - at least for me! i have two csv-files with meassurement-data: fileA ...... (2 Replies)
Discussion started by: IMPe
2 Replies

8. Shell Programming and Scripting

Count math using awk

Hi expert, I have log : TOTAL-TIME : 2125264636 DATA-BYTES-DOWN : 3766111307032 DATA-BYTES-UP : 455032157567 DL = (3766111307032/2125264636)/1024 = 1.73 UL = (455032157567/2125264636)/1024 = 0.21 I want the result : TOTAL = 1.94 ... (4 Replies)
Discussion started by: justbow
4 Replies

9. Shell Programming and Scripting

Math count %memory using awk

Hi expert, i have log this: Memory: 74410384 Memory: 75831176 Memory: 77961232 Memory: 77074656 Memory: 76086160 Memory: 77128592 Memory: 78045384 Memory: 76696040 Memory: 72401176 Memory: 72520016 Memory: 72137016 Memory: 73175832 Memory: 73034528 Memory: 71770736 Memory:... (4 Replies)
Discussion started by: justbow
4 Replies

10. Shell Programming and Scripting

[awk] Math & Bold-Font?

Heya There is a script which has presets stored in a tab-seperated file. That script also has $help_text, which will be shown when called with invalid arguments or -h. So i do need to have that file ready, so the help text can get the values out of the file, and print it with the $help_text.... (17 Replies)
Discussion started by: sea
17 Replies
PERLNUMBER(1)						 Perl Programmers Reference Guide					     PERLNUMBER(1)

NAME
perlnumber - semantics of numbers and numeric operations in Perl SYNOPSIS
$n = 1234; # decimal integer $n = 0b1110011; # binary integer $n = 01234; # octal integer $n = 0x1234; # hexadecimal integer $n = 12.34e-56; # exponential notation $n = "-12.34e56"; # number specified as a string $n = "1234"; # number specified as a string DESCRIPTION
This document describes how Perl internally handles numeric values. Perl's operator overloading facility is completely ignored here. Operator overloading allows user-defined behaviors for numbers, such as operations over arbitrarily large integers, floating points numbers with arbitrary precision, operations over "exotic" numbers such as mod- ular arithmetic or p-adic arithmetic, and so on. See overload for details. Storing numbers Perl can internally represent numbers in 3 different ways: as native integers, as native floating point numbers, and as decimal strings. Decimal strings may have an exponential notation part, as in "12.34e-56". Native here means "a format supported by the C compiler which was used to build perl". The term "native" does not mean quite as much when we talk about native integers, as it does when native floating point numbers are involved. The only implication of the term "native" on integers is that the limits for the maximal and the minimal supported true integral quantities are close to powers of 2. However, "native" floats have a most fundamental restriction: they may represent only those numbers which have a relatively "short" representation when converted to a binary fraction. For example, 0.9 cannot be represented by a native float, since the binary fraction for 0.9 is infinite: binary0.1110011001100... with the sequence 1100 repeating again and again. In addition to this limitation, the exponent of the binary number is also restricted when it is represented as a floating point number. On typical hardware, floating point values can store numbers with up to 53 binary dig- its, and with binary exponents between -1024 and 1024. In decimal representation this is close to 16 decimal digits and decimal exponents in the range of -304..304. The upshot of all this is that Perl cannot store a number like 12345678901234567 as a floating point number on such architectures without loss of information. Similarly, decimal strings can represent only those numbers which have a finite decimal expansion. Being strings, and thus of arbitrary length, there is no practical limit for the exponent or number of decimal digits for these numbers. (But realize that what we are dis- cussing the rules for just the storage of these numbers. The fact that you can store such "large" numbers does not mean that the opera- tions over these numbers will use all of the significant digits. See "Numeric operators and numeric conversions" for details.) In fact numbers stored in the native integer format may be stored either in the signed native form, or in the unsigned native form. Thus the limits for Perl numbers stored as native integers would typically be -2**31..2**32-1, with appropriate modifications in the case of 64-bit integers. Again, this does not mean that Perl can do operations only over integers in this range: it is possible to store many more integers in floating point format. Summing up, Perl numeric values can store only those numbers which have a finite decimal expansion or a "short" binary expansion. Numeric operators and numeric conversions As mentioned earlier, Perl can store a number in any one of three formats, but most operators typically understand only one of those for- mats. When a numeric value is passed as an argument to such an operator, it will be converted to the format understood by the operator. Six such conversions are possible: native integer --> native floating point (*) native integer --> decimal string native floating_point --> native integer (*) native floating_point --> decimal string (*) decimal string --> native integer decimal string --> native floating point (*) These conversions are governed by the following general rules: o If the source number can be represented in the target form, that representation is used. o If the source number is outside of the limits representable in the target form, a representation of the closest limit is used. (Loss of information) o If the source number is between two numbers representable in the target form, a representation of one of these numbers is used. (Loss of information) o In "native floating point --> native integer" conversions the magnitude of the result is less than or equal to the magnitude of the source. ("Rounding to zero".) o If the "decimal string --> native integer" conversion cannot be done without loss of information, the result is compatible with the conversion sequence "decimal_string --> native_floating_point --> native_integer". In particular, rounding is strongly biased to 0, though a number like "0.99999999999999999999" has a chance of being rounded to 1. RESTRICTION: The conversions marked with "(*)" above involve steps performed by the C compiler. In particular, bugs/features of the com- piler used may lead to breakage of some of the above rules. Flavors of Perl numeric operations Perl operations which take a numeric argument treat that argument in one of four different ways: they may force it to one of the inte- ger/floating/ string formats, or they may behave differently depending on the format of the operand. Forcing a numeric value to a particu- lar format does not change the number stored in the value. All the operators which need an argument in the integer format treat the argument as in modular arithmetic, e.g., "mod 2**32" on a 32-bit architecture. "sprintf "%u", -1" therefore provides the same result as "sprintf "%u", ~0". Arithmetic operators The binary operators "+" "-" "*" "/" "%" "==" "!=" ">" "<" ">=" "<=" and the unary operators "-" "abs" and "--" will attempt to convert arguments to integers. If both conversions are possible without loss of precision, and the operation can be performed without loss of precision then the integer result is used. Otherwise arguments are converted to floating point format and the floating point result is used. The caching of conversions (as described above) means that the integer conversion does not throw away fractional parts on float- ing point numbers. ++ "++" behaves as the other operators above, except that if it is a string matching the format "/^[a-zA-Z]*[0-9]*z/" the string incre- ment described in perlop is used. Arithmetic operators during "use integer" In scopes where "use integer;" is in force, nearly all the operators listed above will force their argument(s) into integer format, and return an integer result. The exceptions, "abs", "++" and "--", do not change their behavior with "use integer;" Other mathematical operators Operators such as "**", "sin" and "exp" force arguments to floating point format. Bitwise operators Arguments are forced into the integer format if not strings. Bitwise operators during "use integer" forces arguments to integer format. Also shift operations internally use signed integers rather than the default unsigned. Operators which expect an integer force the argument into the integer format. This is applicable to the third and fourth arguments of "sysread", for example. Operators which expect a string force the argument into the string format. For example, this is applicable to "printf "%s", $value". Though forcing an argument into a particular form does not change the stored number, Perl remembers the result of such conversions. In particular, though the first such conversion may be time-consuming, repeated operations will not need to redo the conversion. AUTHOR
Ilya Zakharevich "ilya@math.ohio-state.edu" Editorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com> Updates for 5.8.0 by Nicholas Clark <nick@ccl4.org> SEE ALSO
overload, perlop perl v5.8.9 2007-11-17 PERLNUMBER(1)
All times are GMT -4. The time now is 10:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy