21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

82 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>x % yx + yx - yespecially for C programmers, to forget that all numbers in awk are floatingpoint,and that division of integer-looking constants produces a real number,not an integer.)Remainder; further discussion is provided in the text, just after this list.Addition.Subtraction.Unary plus and minus have the same precedence, the multiplication operators all havethe same precedence, and addition and subtraction have the same precedence.When computing the remainder of x % y, the quotient is rounded toward zero to aninteger and multiplied by y. This result is subtracted from x; this operation is sometimesknown as “trunc-mod.” The following relation always holds:b * int(a / b) + (a % b) == aOne possibly undesirable effect of this definition of remainder is that x % y is negative ifx is negative. Thus:-17 % 8 = -1In other awk implementations, the signedness of the remainder may be machinedependent.NOTE: The POSIX standard only specifies the use of ‘^’ for exponentiation.For maximum portability, do not use the ‘**’ operator.5.6 String ConcatenationIt seemed like a good idea at the time.Brian KernighanThere is only one string operation: concatenation. It does not have a specific operatorto represent it. Instead, concatenation is performed by writing expressions next to oneanother, with no operator. For example:$ awk ’{ print "Field number one: " $1 }’ BBS-list⊣ Field number one: aardvark⊣ Field number one: alpo-net...Without the space in the string constant after the ‘:’, the line runs together.example:$ awk ’{ print "Field number one:" $1 }’ BBS-list⊣ Field number one:aardvark⊣ Field number one:alpo-net...Because string concatenation does not have an explicit operator, it is often necessaryto insure that it happens at the right time by using parentheses to enclose the items toconcatenate. For example, you might expect that the following code fragment concatenatesfile and name:file = "file"name = "name"For

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!