447-450.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 212 行
HTML
212 行
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:gawk</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=25//-->
<!--PAGES=447-450//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="444-447.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="450-453.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Strings and Numbers</FONT></H4>
<P>If you’ve used any other programming language, these concepts will be familiar to you. If you are new to programming, you will probably find them obvious, but it’s surprising how many people get things hopelessly muddled by using strings when they should have used numbers.
</P>
<P>A <I>string</I> is a set of characters that are to be interpreted literally by <TT>gawk</TT>. Strings are surrounded by quotation marks. Numbers are not surrounded by quotation marks and are treated as real values:</P>
<!-- CODE SNIP //-->
<PRE>
gawk ’$1 != “Tim” {print}’ testfile
</PRE>
<!-- END CODE SNIP //-->
<P>This command prints any line in <TT>testfile</TT> that doesn’t have the word <TT>Tim</TT> in the first column. If we had left out the quotation marks around <TT>Tim</TT>, <TT>gawk</TT> wouldn’t have processed the command properly. The following command displays any line that has the string <TT>50</TT> in it:</P>
<!-- CODE SNIP //-->
<PRE>
gawk ’$1 == “50” {print}’ testfile
</PRE>
<!-- END CODE SNIP //-->
<P>It does not attempt to see if the value stored in the first column is different than 50; it just does a character check. The string <TT>50</TT> is not equal to the number 50 as far as <TT>gawk</TT> is concerned.</P>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Formatting Output</FONT></H4>
<P>We’ve seen how to do simple actions in the commands we’ve already discussed, but you can do several things in an action:
</P>
<!-- CODE SNIP //-->
<PRE>
gawk ’$1 != “Tim” {print $1, $5, $6, $2}’ testfile
</PRE>
<!-- END CODE SNIP //-->
<P>The preceding command prints the first, fifth, sixth, and second columns of <TT>testfile</TT> for every line that doesn’t have the first column equal to <TT>“Tim”</TT>. You can place as many of these columns as you want in a print command.</P>
<P>Indeed, you can place strings in a print command, too:</P>
<!-- CODE SNIP //-->
<PRE>
gawk ’$1 != “Tim” {print “The entry for “, $1, “is not Tim. “, $2}’
testfile
</PRE>
<!-- END CODE SNIP //-->
<P>This command prints the strings and the columns as shown. Each section of the print command is separated by a comma. There are also spaces at the ends of the strings to ensure there is a space between the string and the value of the column that is printed.
</P>
<P>You can use additional formatting instructions to make <TT>gawk</TT> format the output properly. These instructions are borrowed from the C language, and they use the command <TT>printf</TT> (print formatted) instead of <TT>print</TT>.</P>
<P>The <TT>printf</TT> command uses a placeholder scheme, but the <TT>gawk</TT> language knows how to format the entry because of the placeholder and looks later in the command line to find out what to insert there. An example helps clarify this :</P>
<!-- CODE SNIP //-->
<PRE>
{printf “%5s likes this language\n”, $2}
</PRE>
<!-- END CODE SNIP //-->
<P>The <TT>%5s</TT> part of the line instructs <TT>gawk</TT> how to format the string, in this case using five string characters. The value to place in this position is given at the end of the line as the second column. The <TT>\n</TT> at the end of the quoted section is a newline character. If the second column of a four-line file holds names, <TT>printf</TT> formats the output like this:</P>
<!-- CODE SNIP //-->
<PRE>
Tim likes this language
Geoff likes this language
Mike likes this language
Joe likes this language
</PRE>
<!-- END CODE SNIP //-->
<P>Notice that the “<TT>%5s</TT>” format means to right-justify the column entry. This prevents awkward spacing.</P>
<P>The <TT>gawk</TT> language supports several format placeholders. They are shown in Table 25.4.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 25.4.</B> Format placeholders.
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="35%" ALIGN="LEFT">Placeholder
<TH WIDTH="65%" ALIGN="LEFT">Description
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD VALIGN="TOP"><TT>c</TT>
<TD>If a string, the first character of the string; if an integer, the character that matches the first value
<TR>
<TD><TT>d</TT>
<TD>An integer
<TR>
<TD><TT>e</TT>
<TD>A floating-point number in scientific notation
<TR>
<TD><TT>f</TT>
<TD>A floating-point number in conventional notation
<TR>
<TD VALIGN="TOP"><TT>g</TT>
<TD>A floating-point number in either scientific or conventional notation, whichever is shorter
<TR>
<TD><TT>o</TT>
<TD>An unsigned integer in octal format
<TR>
<TD><TT>s</TT>
<TD>A string
<TR>
<TD><TT>x</TT>
<TD>An unsigned integer in hexadecimal format
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>Whenever you use one of the format characters, you can place a number before the character to show how many digits or characters are to be used. Therefore, the format “<TT>6d</TT>” would have six digits of an integer. Many formats can be on a line, but each must have a value at the end of the line, as in this example:</P>
<!-- CODE SNIP //-->
<PRE>
{printf “%5s works for %5s and earns %2d an hour”, $1, $2, $3}
</PRE>
<!-- END CODE SNIP //-->
<P>Here, the first string is the first column, the second string is the second column, and the third set of digits is from the third column in a file. The output looks something like this:
</P>
<!-- CODE SNIP //-->
<PRE>
Joe works for Mike and earns 12 an hour
</PRE>
<!-- END CODE SNIP //-->
<P>A few little tricks are useful. Consider the following command:
</P>
<!-- CODE SNIP //-->
<PRE>
{printf “%5s likes this language\n”, $2}
</PRE>
<!-- END CODE SNIP //-->
<P>As shown in an earlier example, strings are right-justified, so this command results in the following output:
</P>
<!-- CODE SNIP //-->
<PRE>
Tim likes this language
Geoff likes this language
Mike likes this language
Joe likes this language
</PRE>
<!-- END CODE SNIP //-->
<P>To left-justify the names, place a minus sign in the format statement:
</P>
<!-- CODE SNIP //-->
<PRE>
{printf “%-5s likes this language\n”, $2}
</PRE>
<!-- END CODE SNIP //-->
<P>This results in the following output:
</P>
<!-- CODE SNIP //-->
<PRE>
Tim likes this language
Geoff likes this language
Mike likes this language
Joe likes this language
</PRE>
<!-- END CODE SNIP //-->
<P>Notice that the name is justified on the left instead of on the right.
</P>
<P>When dealing with numbers, you can specify the precision to be used:</P>
<!-- CODE SNIP //-->
<PRE>
{printf “%5s earns $%.2f an hour”, $3, $6}
</PRE>
<!-- END CODE SNIP //-->
<P>The preceding command uses the string in column three and puts five characters from it in the first placeholder, and then takes the value in the sixth column and places it in the second placeholder with two digits after the decimal point. The output of the command looks like this:
</P>
<!-- CODE SNIP //-->
<PRE>
Joe earns $12.17 an hour
</PRE>
<!-- END CODE SNIP //-->
<P>The dollar sign is inside the quotation marks in the <TT>printf</TT> command and is not generated by the system. It has no special meaning inside the quotation marks. If you want to limit the number of digits to the right of the period, you can do that, too:</P>
<!-- CODE SNIP //-->
<PRE>
{printf “%5s earns $%6.2f an hour”, $3, $6}
</PRE>
<!-- END CODE SNIP //-->
<P>This command puts six digits before the period and two after.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="444-447.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="450-453.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?