⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0571-0573.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:


<HTML

<HEAD>

<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:gawk Programming</TITLE>

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!-- ISBN=0672311739 //-->

<!-- TITLE=RED HAT LINUX 2ND EDITION //-->

<!-- AUTHOR=DAVID PITTS ET AL //-->

<!-- PUBLISHER=MACMILLAN //-->

<!-- IMPRINT=SAMS PUBLISHING //-->

<!-- PUBLICATION DATE=1998 //-->

<!-- CHAPTER=27 //-->

<!-- PAGES=0545-0582 //-->

<!-- UNASSIGNED1 //-->

<!-- UNASSIGNED2 //-->









<P><CENTER>

<a href="0568-0570.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0574-0577.html">Next</A>

</CENTER></P>



<A NAME="PAGENUM-571"><P>Page 571</P></A>





<!-- CODE SNIP //-->

<PRE>{ while (&quot;who -u&quot; | getline)

  {

      # process each line from the who command

   }

}

</PRE>

<!-- END CODE SNIP //-->









<P>The who command is executed once and each of its output lines is processed by

getline. You could also use the form &quot;command&quot; | getline

variable.

</P>









<P><B>Ending Input from a File or Command

</B></P>









<P>Whenever you use getline to get input from a specified file or command, you should close

it when you are done processing the data. There is a maximum number of open files allowed

to awk that varies with operating system version or individual account configuration (a

command output pipe counts as a file). By closing files when you are done with them, you reduce

the chances of hitting the limit.

</P>









<P>The syntax to close a file is simply

</P>



<!-- CODE SNIP //-->

<PRE>close (&quot;filename&quot;)

</PRE>

<!-- END CODE SNIP //-->









<P>where filename is the one specified on the

getline (which could also be stdin, a variable

that contains the filename, or the exact command used

with getline).

</P>









<H4><A NAME="ch27_ 36">





Output

</A></H4>









<P>There are a few advanced features for output: pretty formatting, sending output to files,

and piping output as input to other commands. The

printf command is used for pretty formatting&#151;instead of seeing the output in whatever default format

awk decides to use (which is often ugly), you can specify how it looks.

</P>









<H5><A NAME="ch27_ 37">

printf

</A></H5>









<P>The print statement produces simple output for you. If you want to be able to format the

data (producing fixed columns, for instance), you need to use

printf. The nice thing about awk printf is that it uses syntax that is very similar to the

printf() function in C.

</P>









<P>The general format of the awk printf is as follows (the parentheses are only required if a

relational expression is included):

</P>

<!-- CODE SNIP //-->

<PRE>printf format-specifier, variable1,variable2, variable3,..variablen

printf(format-specifier, variable1,variable2, variable3,..variablen)

</PRE>

<!-- END CODE SNIP //-->









<P>Personally, I use the second form because I am so used to coding in C.

</P>









<P>The variables are optional, but

format-specifier is mandatory. Often you will have

printf statements that only include

format-specifier (to print messages that contain no variables):

</P>

<!-- CODE SNIP //-->

<PRE>printf (&quot;Program Starting\n&quot;)

printf (&quot;\f&quot;)        # new page in output

</PRE>

<!-- END CODE SNIP //-->









<P>format-specifier can consist of text, escaped characters, or actual print specifiers. A print

specifier begins with the percent sign (%), followed by an optional numeric value that specifies

the

</P>



<A NAME="PAGENUM-572"><P>Page 572</P></A>











<P>

size of the field, then the format type follows (which describes the type of variable or

output format). If you want to print a percent sign in your output, you use

%%.</P>









<P>The field size can consist of two numbers separated by a decimal point

(.). For floating-point numbers, the first number is the size of the entire field (including the decimal point); the

second number is the number of digits to the right of the decimal. For other types of fields,

the first number is the minimum field size and the second number is the maximum field size

(number of characters to actually print); if you omit the first number, it takes the value of the

maximum field size.

</P>









<P>The print specifiers determine how the variable is printed; there are also modifiers that

change the behavior of the specifiers. Table 27.9 shows the print format specifiers.

</P>









<P>Table 27.9. Format specifiers for awk.

</P>



<TABLE WIDTH="360">

<TR><TD>

Format

</TD><TD>

Meaning

</TD></TR>



<TR><TD>

%c

</TD><TD>

ASCII character

</TD></TR>



<TR><TD>

%d

</TD><TD>

An integer (decimal number)

</TD></TR>



<TR><TD>

%i

</TD><TD>

An integer, just like %d

</TD></TR>



<TR><TD>

%e

</TD><TD>

A floating-point number using scientific notation (1.00000E+01)

</TD></TR>



<TR><TD>

%f

</TD><TD>

A floating-point number (10.43)

</TD></TR>



<TR><TD>

%g

</TD><TD>

awk chooses between %e or %f display format (whichever is

shorter) <BR>

suppressing nonsignificant zeros.

</TD></TR>



<TR><TD>

%o

</TD><TD>

An unsigned octal (base 8) number (integer)

</TD></TR>



<TR><TD>

%s

</TD><TD>

A string of characters

</TD></TR>



<TR><TD>

%x

</TD><TD>

An unsigned hexadecimal (base 16) number (integer)

</TD></TR>



<TR><TD>

%X

</TD><TD>

Same as %x but using ABCDEF instead of abcdef

</TD></TR>

</TABLE>





<TABLE BGCOLOR=#FFFF99><TR><TD><B>NOTE</B></TD></TR><TR><TD><BLOCKQUOTE>

If you attempt to print a numeric value or variable using

%c, it will be printed as a character (the ASCII character for that value will print).

</BLOCKQUOTE></TD></TR></TABLE>









<P>The format modifiers change the default behavior of the format specifiers. Listing 27.4

shows the use of various specifiers and modifiers.

</P>



<A NAME="PAGENUM-573"><P>Page 573</P></A>













<P>Listing 27.4. printf format specifiers and modifiers.

</P>

<!-- CODE //-->

<PRE>printf(&quot;%d %3.3d %03.3d %.3d %-.3d %3d %-3d\n&quot;, 64, 64, 64, 64, 64, 64, 64)

printf(&quot;%c %c %2.2c %-2.2c %2c %-2c\n&quot;, 64, &quot;abc&quot;, &quot;abc&quot;, &quot;abc&quot;, &quot;abc&quot;, &quot;abc&quot;)

printf(&quot;%s %2s %-2s %2.2s %-2.2s %.2s %-.2s\n&quot;,

       &quot;abc&quot;, &quot;abc&quot;, &quot;abc&quot;, &quot;abc&quot;, &quot;abc&quot;, &quot;abc&quot;, &quot;abc&quot;)

printf(&quot;%f %6.1f %06.1f %.1f %-.1f %6f\n&quot;,

       123.456, 123.456, 123.456, 123.456, 123.456, 123.456)



64 064 064 064 064  64 64

@ a  a a   a a

abc abc abc ab ab ab ab

123.456000  123.5 0123.5 123.5 123.5 123.456000

</PRE>

<!-- END CODE //-->









<P>When using the integer or decimal (%d) specifier, the field size defaults to the size of the

value being printed (2 digits for the value 64). If you specify a field maximum size that is larger

than that, you automatically get the field zero filled. All numeric fields are right-justified unless

you use the minus sign (-) modifier, which causes them to be left-justified. If you specify only

the field minimum size and want the rest of the field zero filled, you have to use the zero

modifier (before the field minimum size).

</P>









<P>When using the character (%c) specifier, only one character prints from the input no

matter what size you use for the field minimum or maximum sizes and no matter how many

characters are in the value being printed. Note that the value

64 printed as a character shows up as @.

</P>









<P>When using the string (%s) specifier, the entire string prints unless you specify the field

maximum size. By default, strings are left-justified unless you use the minus sign

(-) modifier, which causes them to be right-justified.

</P>









<P>When using the floating (%f) specifier, the field size defaults

.6 (as many digits to the left of the decimal and 6 digits to the right). If you specify a number after the decimal in the format,

that many digits will print to the right of the decimal and

awk will round the number. All numeric fields are right-justified unless you use the minus sign

(-) modifier, which causes them to be left-justified. If you want the field zero filled, you have to use the zero modifier (before

the field minimum size).

</P>









<P>The best way to determine printing results is to work with it. Try out the various

modifiers and see what makes your output look best.

</P>









<H5><A NAME="ch27_ 38">

Output to a File

</A></H5>









<P>You can send your output (from print or printf) to a file. The following creates a new

(or empties out an existing) file containing the printed message:

</P>



<!-- CODE SNIP //-->

<PRE>printf (&quot;hello world\n&quot;) &gt; &quot;datafile&quot;

</PRE>

<!-- END CODE SNIP //-->









<P>If you execute this statement multiple times or other statements that redirect output to

datafile, the output will remain in the file. The file creation/emptying out only occurs the first time

the file is used in the program.

</P>



<P><CENTER>

<a href="0568-0570.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0574-0577.html">Next</A>

</CENTER></P>









</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -