21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5: Expressions 79prints the value of field number n for all input records. Before the first file is read, thecommand line sets the variable n equal to four. This causes the fourth field to be printedin lines from the file ‘inventory-shipped’. After the first file has finished, but beforethe second file is started, n is set to two, so that the second field is printed in lines from‘BBS-list’:$ awk ’{ print $n }’ n=4 inventory-shipped n=2 BBS-list⊣ 15⊣ 24...⊣ 555-5553⊣ 555-3412...Command-line arguments are made available for explicit examination by the awk programin the ARGV array (see Section 6.5.3 [Using ARGC and ARGV], page 116). awk processesthe values of command-line assignments for escape sequences (see Section 2.2 [Escape Sequences],page 25).5.4 Conversion of Strings and NumbersStrings are converted to numbers and numbers are converted to strings, if the context of theawk program demands it. For example, if the value of either foo or bar in the expression‘foo + bar’ happens to be a string, it is converted to a number before the addition isperformed. If numeric values appear in string concatenation, they are converted to strings.Consider the following:two = 2; three = 3print (two three) + 4This prints the (numeric) value 27. The numeric values of the variables two and three areconverted to strings and concatenated together. The resulting string is converted back tothe number 23, to which 4 is then added.If, for some reason, you need to force a number to be converted to a string, concatenatethe empty string, "", with that number. To force a string to be converted to a number, addzero to that string. A string is converted to a number by interpreting any numeric prefixof the string as numerals: "2.5" converts to 2.5, "1e3" converts to 1000, and "25fix" hasa numeric value of 25. Strings that can’t be interpreted as valid numbers convert to zero.The exact manner in which numbers are converted into strings is controlled by theawk built-in variable CONVFMT (see Section 6.5 [Built-in Variables], page 110). Numbers areconverted using the sprintf function with CONVFMT as the format specifier (see Section 8.1.3[String-Manipulation Functions], page 132).CONVFMT’s default value is "%.6g", which prints a value with at most six significant digits.For some applications, you might want to change it to specify more precision. On mostmodern machines, 17 digits is enough to capture a floating-point number’s value exactly,most of the time. 2Strange results can occur if you set CONVFMT to a string that doesn’t tell sprintf howto format floating-point numbers in a useful way. For example, if you forget the ‘%’ in the2 Pathological cases can require up to 752 digits (!), but we doubt that you need to worry about this.

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

Saved successfully!

Ooh no, something went wrong!