📄 477-480.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Configuration and Installation:Programming in Linux</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=1558285660//-->
<!--TITLE=Linux Configuration and Installation//-->
<!--AUTHOR=Patrick Volkerding//-->
<!--AUTHOR=Kevin Reichard//-->
<!--AUTHOR=Eric Foster//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=10//-->
<!--PAGES=477-480//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="474-477.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="480-482.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading22"></A><FONT COLOR="#000077">Gawk</FONT></H4>
<P>Developed by three Bell Labs researchers (Alfred Aho, Peter Weinberger, and Brian Kernighan—hence the acronym <I>awk</I>), <B>awk</B> is a programming language (with some strong similarities to the C programming language, discussed earlier in this chapter) but is used in much the same manner as other UNIX scripting tools. Hence its inclusion in this chapter.</P>
<P>Technically speaking, <B>awk</B> doesn’t ship with Linux; instead, the GNU version, <B>gawk</B>, ships with Linux. (By now you shouldn’t be surprised that Linux features software from the GNU Project.) Because <B>gawk</B> is virtually identical to other implementations of <B>awk</B> (there are a few extensions to <B>awk</B> in <B>gawk</B>, but you can ignore them if you choose), most users with experience with <B>awk</B> will have no problems with <B>awk</B>.</P>
<P><B>Gawk</B>’s primary value is in the manipulation of structured text files, where information is stored in columnar form and is separated by consistent characters (such as tabs or spaces). <B>Gawk</B> takes these structured files and manipulates them through editing, sorting, and searching.</P>
<P>Let’s use a data file named <B>workers</B> as an example:</P>
<!-- CODE SNIP //-->
<PRE>
Eric 286 555-6674 erc 8
Geisha 280 555-4221 geisha 10
Kevin 279 555-1112 kevin 2
Tom 284 555-2121 spike 12
</PRE>
<!-- END CODE SNIP //-->
<P>Let’s sink into the trap of abstraction for a minute and compare our example file output to a two-dimensional graph. Each row across is called a <I>record</I>, which in turn is made up of vertical fields or columns, almost like a database. <B>Gawk</B> allows us to manipulate the data in the file by either row or column. Using the <B>gawk</B> command is not a complicated process. The structure of the <B>gawk</B> command looks like:</P>
<!-- CODE SNIP //-->
<PRE>
$ gawk [<I>option</I>] <I>‘pattern</I> {<I>action</I>}'
</PRE>
<!-- END CODE SNIP //-->
<P>(The only options available with <B>gawk</B> are <I>-F</I>, which allows you to specify a field separator other than the default of white space; <I>-f</I>, which allows you to specify a filename full of <B>gawk</B> commands instead of placing a complex pattern and action on the Linux command line, and <I>-W</I>, which runs <B>gawk</B> in total compatibility with <B>awk</B>.) Here we should define our terms. A <I>pattern</I> can be an ASCII string (which <B>gawk</B> treats numerically; instead of seeing the character <I>e</I> as an <I>e</I>, it sees it as the ASCII equivalent), a numeral, a combination of numerals, or a wildcard, while <I>action</I> refers to an instruction we provide. Essentially, <B>gawk</B> works by having us tell it to search for a particular pattern; when it has found that pattern, <B>gawk</B> is to do something with it, such as printing the pattern to another file.</P>
<P>The simplest <B>gawk</B> program merely prints out all lines in the file:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:/$ gawk '{ print }' workers
Eric 286 555-6674 erc 8
Geisha 280 555-4221 geisha 10
Kevin 279 555-1112 kevin 2
Tom 284 555-2121 spike 12
</PRE>
<!-- END CODE SNIP //-->
<P>Continuing our example, let’s say we wanted to pull all records that began with the string <I>geisha</I>. We’d use the following:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:/$ gawk '$1 ~ /Geisha/ {print $0}' workers
</PRE>
<!-- END CODE SNIP //-->
<P>Here’s what the command means, part by part:
</P>
<DL>
<DD><B>•</B> <I>$1:</I> Tells <B>gawk</B> to use the first column for the basis of further action. <B>gawk</B> will perform some action on a file based on either records or fields; a number beginning with a <TT>$</TT> tells <B>gawk</B> to work on a specific field. In this case, <TT>$1</TT> refers to the first field.
<DD><B>•</B> <B>~</B>: Tells <B>gawk</B> to match the following string.
<DD><B>•</B> <B>/Geisha/</B>: The string to search for.
<DD><B>•</B> <B>{print $0}</B>: Tells <B>gawk</B> to print out the entire record containing the matched string. A special use of the <TT>$</TT> sign is with the character <I>0</I>, which tells <B>gawk</B> to use all the fields possible.
<DD><B>•</B> <B>workers</B>: The file to use.
</DL>
<P>In our case, <B>gawk</B> would print the following to the screen:</P>
<!-- CODE SNIP //-->
<PRE>
Geisha 280 555-4221 geisha 10
</PRE>
<!-- END CODE SNIP //-->
<P>Not every action has to be the result of matching a specific pattern, of course. In <B>gawk</B>, the tilde (<B>~</B>) acts as a relational operator, which sets forth a condition for <B>gawk</B> to use. There are a number of other relational operators available to <B>gawk</B> users that allow <B>gawk</B> to compare two patterns. (The relational operators are based on algebraic notation.) <B>Gawk</B> supports the same relational operators found in the C programming language; they are listed in Table 10.7.</P>
<TABLE WIDTH="100%"><CAPTION><B>Table 10.7</B> Gawk Relational Operators
<TR>
<TH COLSPAN="3"><HR>
<TR>
<TH WIDTH="20%" ALIGN="LEFT">Operator
<TH WIDTH="35%" ALIGN="LEFT">Meaning
<TH WIDTH="45%" ALIGN="LEFT">Usage
<TR>
<TD VALIGN="TOP"><
<TD VALIGN="TOP">Less than
<TD>$1 < "Eric" returns every pattern with an ASCII value less than “Eric”.
<TR>
<TD><=
<TD>Less than or equal to
<TD>$1 <= "Eric".
<TR>
<TD VALIGN="TOP">==
<TD VALIGN="TOP">Equals
<TD>$1 == "Eric" returns every instance of “Eric”.
<TR>
<TD VALIGN="TOP">!=
<TD VALIGN="TOP">Does not equal
<TD>$1 != "Eric" returns every field not containing the string “Eric”.
<TR>
<TD VALIGN="TOP">>=
<TD VALIGN="TOP">Greater than or equal to
<TD>$1 >= "Eric" returns every field equal to or greater than “Eric”.
<TR>
<TD VALIGN="TOP">>
<TD VALIGN="TOP">Greater than
<TD><TT>$1 > "Eric"</TT> returns every field greater than “Eric.”
<TR>
<TD COLSPAN="3"><HR>
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="474-477.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="480-482.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -