rhl26.htm
来自「linux的初学电子书」· HTM 代码 · 共 2,804 行 · 第 1/5 页
HTM
2,804 行
<BR>
<PRE>
<FONT COLOR="#000080">Tim likes this language
Geoff likes this language
Mike likes this language
Joe likes this language</FONT></PRE>
<P>You will notice that the %5s format means to right-justify the column entry. This prevents awkward spacing.
<BR>
<P>The gawk language supports several format placeholders. They are shown in Table 26.4.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 26.4. Format placeholders.</B></FONT></CENTER>
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Placeholder
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Description
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
c
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
If a string, the first character of the string; if an integer, the character that matches the first value
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
d
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
An integer
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
e
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
A floating-point number in scientific notation
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
f
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
A floating-point number in conventional notation
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
g
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
A floating-point number in either scientific or conventional notation, whichever is shorter
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
o
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
An unsigned integer in octal format
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
s
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
A string
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
x
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
An unsigned integer in hexadecimal format</FONT>
</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 6d 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
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">{printf "%5s works for %5s and earns %2d an hour", $1, $2, $3}</FONT></PRE>
<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 would be something like:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">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
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">{printf "%5s likes this language\n", $2}</FONT></PRE>
<P>results in the output
<BR>
<PRE>
<FONT COLOR="#000080">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:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">{printf "%-5s likes this language\n", $2}</FONT></PRE>
<P>This will result in the output
<BR>
<PRE>
<FONT COLOR="#000080">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.
<BR>
<P>When dealing with numbers, you can specify the precision to be used, so that the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">{printf "%5s earns $%.2f an hour", $3, $6}</FONT></PRE>
<P>will use the string in column three and put five characters from it in the first placeholder, and then take the value in the sixth column and place it in the second placeholder with two digits after the decimal point. The output of the command would be
like this:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">Joe earns $12.17 an hour</FONT></PRE>
<P>The dollar sign was inside the quotation marks in the printf 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
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">{printf "%5s earns $%6.2f an hour", $3, $6}</FONT></PRE>
<P>will put six digits before the period and two after.
<BR>
<P>Finally, we can impose some formatting on the output lines themselves. In an earlier example, you saw the use of \n to add a newline character. These are called escape codes, because the backslash is interpreted by gawk to mean something different than
a backslash. Table 26.5 shows the important escape codes that gawk supports.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 26.5. Escape codes.</B></FONT></CENTER>
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Code</I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Description</I>
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\a
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Bell
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\b
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Backspace
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\f
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Formfeed
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\n
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Newline
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\r
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Carriage return
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\t
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Tab
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\v
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Vertical tab
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\ooo
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Octal character ooo
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\xdd
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Hexadecimal character dd
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\c
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Any character c</FONT>
</TABLE><P>You can, for example, escape a quotation mark by using the sequence \", which will place a quotation mark in the string without interpreting it to mean something special. For example:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">{printf "I said \"Hello\" and he said "\Hello\"."</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.
<BR>
<BR>
<A NAME="E69E322"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Changing Field Separators</B></FONT></CENTER></H4>
<BR>
<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 /etc/passwd file. You can change the field separator on the gawk command line by using the -F
option followed by the separator you want to use:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gawk -F":" '/tparker/{print}' /etc/passwd</FONT></PRE>
<P>This command changes the field separator to a colon and searches the etc/passwd file for the lines containing the string tparker. The new field separator is put in quotation marks to avoid any confusion. Also, the -F 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.
<BR>
<BR>
<A NAME="E69E323"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Metacharacters</B></FONT></CENTER></H4>
<BR>
<P>Earlier I mentioned that gawk is particular about its pattern-matching habits. The string cat 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 "cat"
but not "concatenate," you should put spaces on either side of the pattern:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">/ cat / {print}</FONT></PRE>
<P>What about matching different cases? That's where the or instruction, represented by a vertical bar, comes in. For example
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">/ cat | CAT / {print}</FONT></PRE>
<P>will match "cat" or "CAT" on a line. However, what about "Cat?" That's where we also need to specify options within a pattern. With gawk, we use square brackets for this. To match any combination of "cat" in
upper- or lowercase, we must write the pattern like this:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">/ [Cc][Aa][Tt] / {print}</FONT></PRE>
<P>This can get pretty awkward, but it's seldom necessary. To match just "Cat" and "cat," for example, we would use the pattern
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">/ [Cc]at / {print}</FONT></PRE>
<P>A useful matching operator is the tilde (~). This is used when you want to look for a match in a particular field in a record. For example, the pattern
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">$5 ~ /tparker/</FONT></PRE>
<P>will match any records where the fifth field is tparker. It is similar to the == operator. The matching operator can be negated, so
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">$5 !~ /tparker/</FONT></PRE>
<P>will find any record where the fifth field is not equal to tparker.
<BR>
<P>A few characters (called metacharacters) have special meaning to gawk. 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 gawk patterns.
<BR>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?