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

📄 ch26.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 5 页
字号:


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 put there. An example will help make this obvious:<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">{printf &quot;%5s likes this language\n&quot;, $2}



</FONT></PRE>



<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 field of a four-line file



held names, <TT>printf</TT> would format the output like this:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">  Tim likes this language



Geoff likes this language



 Mike likes this language



  Joe likes this language



</FONT></PRE>



<P>You will 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 26.4. <BR>







<CENTER>



<P><FONT SIZE="4"><B>Table 26.4. Format placeholders. </B></FONT>



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><I>Placeholder</I></TD>



		<TD ALIGN="LEFT"><I>Description</I></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>c</TT></TD>



		<TD ALIGN="LEFT">If a string, the first character of the string; if an integer, the character that



			matches the first value</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>d</TT></TD>



		<TD ALIGN="LEFT">An integer</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>e</TT></TD>



		<TD ALIGN="LEFT">A floating-point number in scientific notation</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>f</TT></TD>



		<TD ALIGN="LEFT">A floating-point number in conventional notation</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>g</TT></TD>



		<TD ALIGN="LEFT">A floating-point number in either scientific or conventional notation, whichever



			is shorter</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>o</TT></TD>



		<TD ALIGN="LEFT">An unsigned integer in octal format</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>s</TT></TD>



		<TD ALIGN="LEFT">A string</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>x</TT></TD>



		<TD ALIGN="LEFT">An unsigned integer in hexadecimal format</TD>



	</TR>



</TABLE>







</CENTER>



<P><BR>



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:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">{printf &quot;%5s works for %5s and earns %2d an hour&quot;, $1, $2, $3}



</FONT></PRE>



<P>Here, the first string is the first field, the second string is the second field,



and the third set of digits is from the third field in a file. The output would be



something like this:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">Joe works for Mike and earns 12 an hour



</FONT></PRE>



<P>A few little tricks are useful. As you saw in an earlier example, strings are



right-justified, so the command<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">{printf &quot;%5s likes this language\n&quot;, $2}



</FONT></PRE>



<P>results in the output<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">  Tim likes this language



Geoff likes this language



 Mike likes this language



  Joe likes this language



</FONT></PRE>



<P>To left-justify the names, place a minus sign in the format statement:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">{printf &quot;%-5s likes this language\n&quot;, $2}



</FONT></PRE>



<P>This will result in the output<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">Tim   likes this language



Geoff likes this language



Mike  likes this language



Joe   likes this language



</FONT></PRE>



<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, so that the



command<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">{printf &quot;%5s earns $%.2f an hour&quot;, $3, $6}



</FONT></PRE>



<P>will use the third field and put five characters from it in the first placeholder,



and then take the value in the sixth field and place it in the second placeholder



with two digits after the decimal point. The output of the command would be like



this:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">Joe earns $12.17 an hour



</FONT></PRE>



<P>The dollar sign was inside the quotation marks in the <TT>printf</TT> command,



and was 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. The command<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">{printf &quot;%5s earns $%6.2f an hour&quot;, $3, $6}



</FONT></PRE>



<P>will put six digits before the period and two after.</P>



<P>Finally, we can impose some formatting on the output lines themselves. In an earlier



example, you saw the use of <TT>\n</TT> to add a newline character. These are called



escape codes, because the backslash is interpreted by <TT>gawk</TT> to mean something



different than a backslash. Table 26.5 shows the important escape codes that <TT>gawk</TT>



supports. <BR>







<CENTER>



<P><FONT SIZE="4"><B>Table 26.5. Escape codes. </B></FONT>



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="59" ALIGN="LEFT"><I>Code</I></TD>



		<TD ALIGN="LEFT"><I>Description</I></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="59" ALIGN="LEFT"><TT>\a</TT></TD>



		<TD ALIGN="LEFT">Bell</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="59" ALIGN="LEFT"><TT>\b</TT></TD>



		<TD ALIGN="LEFT">Backspace</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="59" ALIGN="LEFT"><TT>\f</TT></TD>



		<TD ALIGN="LEFT">Formfeed</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="59" ALIGN="LEFT"><TT>\n</TT></TD>



		<TD ALIGN="LEFT">Newline</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="59" ALIGN="LEFT"><TT>\r</TT></TD>



		<TD ALIGN="LEFT">Carriage return</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="59" ALIGN="LEFT"><TT>\t</TT></TD>



		<TD ALIGN="LEFT">Tab</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="59" ALIGN="LEFT"><TT>\v</TT></TD>



		<TD ALIGN="LEFT">Vertical tab</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="59" ALIGN="LEFT"><TT>\ooo</TT></TD>



		<TD ALIGN="LEFT">Octal character <TT>ooo</TT></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="59" ALIGN="LEFT"><TT>\xdd</TT></TD>



		<TD ALIGN="LEFT">Hexadecimal character <TT>dd</TT></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="59" ALIGN="LEFT"><TT>\c</TT></TD>



		<TD ALIGN="LEFT">Any character <TT>c</TT></TD>



	</TR>



</TABLE>







</CENTER>



<P><BR>



You can, for example, escape a quotation mark by using the sequence <TT>\&quot;</TT>,



which will place a quotation mark in the string without interpreting it to mean something



special. For example:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">{printf &quot;I said \&quot;Hello\&quot; and he said &quot;\Hello\&quot;.&quot;



</FONT></PRE>



<P>Awkward-looking, perhaps, but necessary to avoid problems. You'll see lots more



escape characters used in examples later in this chapter. To use a literal backslash,



use <TT>\\</TT> in your program.



<CENTER>



<H4><A NAME="Heading13<FONT COLOR="#000077">Changing Field Separators</FONT></H4>



</CENTER>



<P>As I mentioned earlier, the default field separator is always a whitespace character



(spaces or tabs). This is not often convenient, as we found with the <TT>/etc/passwd</TT>



file. You can change the field separator on the <TT>gawk</TT> command line by using



the <TT>-F</TT> option followed by the separator you want to use:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">gawk -F&quot;:&quot; `/tparker/{print}' /etc/passwd



</FONT></PRE>



<P>This command changes the field separator to a colon and searches the <TT>/etc/passwd</TT>



file for the lines containing the string <TT>tparker</TT>. The new field separator



is put in quotation marks to avoid any confusion. Also, the <TT>-F</TT> option (it



must be a capital F) is before the first quote character enclosing the pattern-action



pair. If it came after, it wouldn't be applied.



<CENTER>



<H4><A NAME="Heading14<FONT COLOR="#000077">Metacharacters</FONT></H4>



</CENTER>



<P>Earlier I mentioned that <TT>gawk</TT> is particular about its pattern-matching



habits. The string <TT>cat</TT> will match anything with the three letters on the



line. Sometimes you want to be more exact in the matching. If you only want to match



the word &quot;cat&quot; but not &quot;concatenate,&quot; you should put spaces on



either side of the pattern:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">/ cat / {print}



</FONT></PRE>



<P>What about matching different cases? That's where the <TT>or</TT> instruction,



represented by a vertical bar, comes in. For example,<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">/ cat | CAT / {print}



</FONT></PRE>



<P>will match &quot;cat&quot; or &quot;CAT&quot; on a line. However, what about &quot;Cat?&quot;



That's where we also need to specify options within a pattern. With <TT>gawk</TT>,



we use square brackets for this. To match any combination of &quot;cat&quot; in upper-



or lowercase, we must write the pattern like this:<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">/ [Cc][Aa][Tt] / {print}



</FONT></PRE>



<P>This can get pretty awkward, but it's seldom necessary. To match just &quot;Cat&quot;



and &quot;cat,&quot; for example, we would use the pattern<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">/ [Cc]at / {print}



</FONT></PRE>



<P>A useful matching operator is the tilde (<TT>~</TT>). This is used when you want



to look for a match in a particular field in a record. For example, the pattern<FONT



COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">$5 ~ /tparker/



</FONT></PRE>



<P>will match any records where the fifth field is <TT>tparker</TT>. It is similar



to the <TT>==</TT> operator. The matching operator can be negated, so<FONT COLOR="#0066FF"></FONT>



<PRE><FONT COLOR="#0066FF">$5 !~ /tparker/



</FONT></PRE>



<P>will find any record where the fifth field is not equal to <TT>tparker</TT>.</P>



<P>A few characters (called metacharacters) have special meaning to <TT>gawk</TT>.



Many of these metacharacters will be familiar to shell users, because they are carried



over from UNIX shells. The metacharacters shown in Table 26.6 can be used in <TT>gawk</TT>



patterns. <BR>







<CENTER>



<P><FONT SIZE="4"><B>Table 26.6. Metacharacters. </B></FONT>



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><I>Metacharacter</I></TD>



		<TD WIDTH="102" ALIGN="LEFT"><I>Meaning</I></TD>



		<TD ALIGN="LEFT"><I>Example</I></TD>



		<TD ALIGN="LEFT"><I>Meaning of Example</I></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>~</TT></TD>



		<TD WIDTH="102" ALIGN="LEFT">The beginning</TD>



		<TD ALIGN="LEFT"><TT>$3 ~ /^b/</TT></TD>



		<TD ALIGN="LEFT">Matches if the third field</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"></TD>



		<TD WIDTH="102" ALIGN="LEFT">of the field</TD>



		<TD ALIGN="LEFT"></TD>



		<TD ALIGN="LEFT">starts with b</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><TT>$</TT></TD>



		<TD WIDTH="102" ALIGN="LEFT">The end of the</TD>



		<TD ALIGN="LEFT"><TT>$3 ~ /b$/</TT></TD>



		<TD ALIGN="LEFT">Matches if the third field</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



⌨️ 快捷键说明

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