rhl26.htm
来自「linux的初学电子书」· HTM 代码 · 共 2,804 行 · 第 1/5 页
HTM
2,804 行
<HTML>
<HEAD>
<TITLE>Red Hat Linux Unleashed rhl26.htm </TITLE>
<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm">
<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/htindex.htm">
<LINK REL="Next" HREF="rhl27.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl27.htm">
<LINK REL="Previous" HREF="rhl25.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl25.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A>
<H2>Red Hat Linux Unleashed rhl26.htm</H2>
<P ALIGN=LEFT>
<A HREF="rhl25.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl25.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="rhl27.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl27.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E215" >What Is the awk Language?</A>
<LI>
<A HREF="#E68E216" >Files, Records, and Fields</A>
<LI>
<A HREF="#E68E217" >Pattern-Action Pairs</A>
<UL>
<LI>
<A HREF="#E69E318" >Simple Patterns</A>
<LI>
<A HREF="#E69E319" >Comparisons and Arithmetic</A>
<LI>
<A HREF="#E69E320" >Strings and Numbers</A>
<LI>
<A HREF="#E69E321" >Formatting Output</A>
<LI>
<A HREF="#E69E322" >Changing Field Separators</A>
<LI>
<A HREF="#E69E323" >Metacharacters</A></UL>
<LI>
<A HREF="#E68E218" >Calling gawk Programs</A>
<UL>
<LI>
<A HREF="#E69E324" >BEGIN and END</A>
<LI>
<A HREF="#E69E325" >Variables</A>
<LI>
<A HREF="#E69E326" >Built-In Variables</A></UL>
<LI>
<A HREF="#E68E219" >Control Structures</A>
<UL>
<LI>
<A HREF="#E69E327" >The if Statement</A>
<LI>
<A HREF="#E69E328" >The while Loop</A>
<LI>
<A HREF="#E69E329" >The for Loop</A>
<LI>
<A HREF="#E69E330" >next and exit</A>
<LI>
<A HREF="#E69E331" >Arrays</A></UL>
<LI>
<A HREF="#E68E220" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E26"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>26</B></FONT></CENTER></H1>
<BR>
<A NAME="E67E26"></A>
<H2 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>gawk</B></FONT></CENTER></H2>
<BR>
<P>The awk programming language was created by the three people who gave their last-name initials to the language: Alfred Aho, Peter Weinberger, and Brian Kernighan. The gawk program included with Linux is the GNU implementation of that programming
language.
<BR>
<P>The awk language is more than just a programming language; it is an almost indispensable tool for many system administrators and UNIX programmers. The language itself is easy to learn, easy to master, and amazingly flexible. Once you get the hang of
using awk, you'll be surprised how often you can use it for routine tasks on your system.
<BR>
<P>To help you understand gawk, I will follow a simple order of introducing the elements of the programming language, as well as showing good examples. You are encouraged, or course, to experiment as the chapter progresses.
<BR>
<P>I can't cover all the different aspects and features of gawk in this chapter, but we will look at the basics of the language and show you enough, hopefully, to get your curiosity working.
<BR>
<BR>
<A NAME="E68E215"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>What Is the </B><B>awk</B><B> Language?</B></FONT></CENTER></H3>
<BR>
<P>awk is designed to be an easy-to-use programming language that lets you work with information either stored in files or piped to it. The main strengths of awk are its abilities to do the following:
<BR>
<UL>
<LI>Display some or all the contents of a file, selecting rows, columns, or fields as necessary.
<BR>
<BR>
<LI>Analyze text for frequency of words, occurrences, and so on.
<BR>
<BR>
<LI>Prepare formatted output reports based on information in a file.
<BR>
<BR>
<LI>Filter text in a very powerful manner.
<BR>
<BR>
<LI>Perform calculations with numeric information from a file.
<BR>
<BR>
</UL>
<P>awk isn't difficult to learn. In many ways, awk is the ideal first programming language because of its simple rules, basic formatting, and standard usage. Experienced programmers will find awk refreshingly easy to use.
<BR>
<BR>
<A NAME="E68E216"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Files, Records, and Fields</B></FONT></CENTER></H3>
<BR>
<P>Usually, gawk works with data stored in files. Often this is numeric data, but gawk can work with character information, too. If data is not stored in a file, it is supplied to gawk through a pipe or other form of redirection. Only ASCII files (text
files) can be properly handled with gawk. Although it does have the ability to work with binary files, the results are often unpredictable. Since most information on a Linux system is stored in ASCII, this isn't a problem.
<BR>
<P>As a simple example of a file that gawk works with, consider a telephone directory. It is composed of many entries, all with the same format: last name, first name, address, telephone number. The entire telephone directory is a database of sorts,
although without a sophisticated search routine. Indeed, the telephone directory relies on a pure alphabetical order to enable users to search for the data they need.
<BR>
<P>Each line in the telephone directory is a complete set of data on its own and is called a record. For example, the entry in the telephone directory for "Smith, John," which includes his address and telephone number, is a record.
<BR>
<P>Each piece of information in the record—the last name, the first name, the address, and the telephone number—is called a field. For the gawk language, the field is a single piece of information. A record, then, is a number of fields that
pertain to a single item. A set of records makes up a file.
<BR>
<P>In most cases, fields are separated by a character that is used only to separate fields, such as a space, a tab, a colon, or some other special symbol. This character is called a field separator. A good example is the file /etc/passwd, which looks like
this:
<BR>
<PRE>
<FONT COLOR="#000080">tparker:t36s62hsh:501:101:Tim Parker:/home/tparker:/bin/bash
etreijs:2ys639dj3h:502:101:Ed Treijs:/home/etreijs:/bin/tcsh
ychow:1h27sj:503:101:Yvonne Chow:/home/ychow:/bin/bash</FONT></PRE>
<P>If you look carefully at the file, you will see that it uses a colon as the field separator. Each line in the /etc/passwd file has seven fields: the user name, the password, the user ID, the group ID, a comment field, the home directory, and the startup
shell. Each field is separated by a colon. Colons exist only to separate fields. A program looking for the sixth field in any line needs only count five colons across (because the first field doesn't have a colon before it).
<BR>
<P>That's where we find a problem with the gawk definition of fields as they pertain to the telephone directory example. Consider the following lines from a telephone directory:
<BR>
<PRE>
<FONT COLOR="#000080">Smith, John 13 Wilson St. 555-1283
Smith, John 2736 Artside Dr, Apt 123 555-2736
Smith, John 125 Westmount Cr 555-1726</FONT></PRE>
<P>We "know" there are four fields here: the last name, the first name, the address, and the telephone number. But gawk doesn't see it that way. The telephone book uses the space character as a field separator, so on the first line it sees
"Smith" as the first field, "John" as the second, "13" as the third, "Wilson" as the fourth, and so on. As far as gawk is concerned, the first line when using a space character as a field separator has six fields.
The second line has eight fields.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>When working with a programming language, you must consider data the way the language will see it. Remember that programming languages take things literally.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>To make sense of the telephone directory the way we want to handle it, we have to find another way of structuring the data so that there is a field separator between the sections. For example, the following uses the slash character as the field
separator:
<BR>
<PRE>
<FONT COLOR="#000080">Smith/John/13 Wilson St./555-1283
Smith/John/2736 Artside Dr, Apt 123/555-2736
Smith/John/125 Westmount Cr/555-1726</FONT></PRE>
<P>By default, gawk uses blank characters (spaces or tabs) as field separators unless instructed to use another character. If gawk is using spaces, it doesn't matter how many are in a row; they are treated as a single block for purposes of finding fields.
Naturally, there is a way to override this behavior, too.
<BR>
<BR>
<A NAME="E68E217"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Pattern-Action Pairs</B></FONT></CENTER></H3>
<BR>
<P>The gawk language has a particular format for almost all instructions. Each command is composed of two parts: a pattern and a corresponding action. Whenever the pattern is matched, gawk executes the action that matches that pattern.
<BR>
<P>Pattern-action pairs can be thought of in more common terms to show how they work. Consider instructing someone how to get to the post office. You might say, "Go to the end of the street and turn right. At the stop sign, turn left. At the end of
the street, go right." You have created three pattern-action pairs with these instructions:
<BR>
<PRE>
<FONT COLOR="#000080">end of street: turn right
stop sign: turn left
end of street: turn right</FONT></PRE>
<P>When these patterns are met, the corresponding action is taken. You wouldn't turn right before you reached the end of the street, and you don't turn right until you get to the end of the street, so the pattern must be matched precisely for the action to
be performed. This is a bit simplistic, but it gives you the basic idea.
<BR>
<P>With gawk, the patterns to be matched are enclosed in a pair of slashes, and the actions are in a pair of curly braces:
<BR>
<PRE>
<FONT COLOR="#000080">/pattern1/{action1}
/pattern2/{action2}
/pattern3/{action3}</FONT></PRE>
<P>This format makes it quite easy to tell where the pattern starts and ends, and when the action starts and ends. All gawk programs are sets of these pattern-action pairs, one after the other. Remember these pattern-action pairs are working on text files,
so a typical set of patterns might be matching a set of strings, and the actions might be to print out parts of the line that matched.
<BR>
<P>Suppose there isn't a pattern? In that case, the pattern matches every time and the action is executed every time. If there is no action, gawk copies the entire line that matched without change.
<BR>
<P>Here are some simple examples. The gawk command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gawk '/tparker/' /etc/passwd</FONT></PRE>
<P>will look for each line in the /etc/passwd file that contains the pattern tparker and display it (there is no action, only a pattern). The output from the command will be the one line in the /etc/passwd file that contains the string tparker. If there is
more than one line in the file with that pattern, they all will be displayed. In this case, gawk is acting exactly like the grep utility!
<BR>
<P>This example shows you two important things about gawk: It can be invoked from the command line by giving it the pattern-action pair to work with and a filename, and it likes to have single quotes around the pattern-action pair in order to differentiate
them from the filename.
<BR>
<P>The gawk language is literal in its matching. The string cat will match any lines with cat in them, whether the word "cat" by itself or part of another word such as "concatenate." To be exact, put spaces on either side of the word.
Also, case is important. We'll see how to expand the matching in the section "Metacharacters" a little later in the chapter.
<BR>
<P>Jumping ahead slightly, we can introduce a gawk command. The command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gawk '{print $3}' file2.data</FONT></PRE>
<P>has only an action, so it performs that action on every line in the file file2.data. The action is print $3, which tells gawk to print the third field of every line. The default field separator, a space, is used to tell where fields begin and end. If we
had tried the same command on the /etc/passwd file, nothing would have been displayed because the field separator used in that file is the colon.
<BR>
<P>We can combine the two commands to show a complete pattern-action pair:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gawk '/UNIX/{print $2}' file2.data</FONT></PRE>
<P>This command will search file2.data line by line, looking for the string UNIX. If it finds UNIX, it prints the second column of that line (record).
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The quotes around the entire pattern-action pair are very important and should not be left off. Without them, the command might not execute properly. Make sure the quotes match (don't use a single quote at the beginning and a double quote at the
end).</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>You can combine more than one pattern-action pair in a command. For example, the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gawk '/scandal/{print $1} /rumor/{print $2}' gossip_file</FONT></PRE>
<P>scans gossip_file for all occurrences of the pattern "scandal" and prints the first column, and then starts at the top again and searches for the pattern "rumor" and prints the second column. The scan starts at the top of the file
each time there is a new pattern-action pair.
<BR>
<BR>
<A NAME="E69E318"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Simple Patterns</B></FONT></CENTER></H4>
<BR>
<P>As you might have figured out, gawk numbers all of the fields in a record. The first field is $1, the second is $2, and so on. The entire record is called $0. As a short form, gawk allows you to ignore the $0 in simple commands, so the instructions
<BR>
<PRE>
<FONT COLOR="#000080">gawk '/tparker/{print $0}' /etc/passwd
gawk '/tparker/{print}' /etc/passwd
gawk '/tparker/' /etcpasswd</FONT></PRE>
<P>result in the same output (the latter one because no action causes the entire line to be printed).
<BR>
<P>Sometimes you want to do more than match a simple character string. The gawk language has many powerful features, but I'll just introduce a few at the moment. We can, for example, make a comparison of a field with a value. The command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gawk '$2 == "foo" {print $3}' testfile</FONT></PRE>
<P>instructs gawk to compare the second string ($2) of each record in testfile and check to see if it is equal to the string foo. If it is, gawk prints the third column ($3).
<BR>
<P>This command demonstrates a few important points. First, there are no slashes around the pattern because we are not matching a pattern but are evaluating something. Slashes are used only for character matches. Second, the == sign means "is equal
to." We must use two equal signs, because the single equal sign is used for assignment of values, as you will see shortly. Finally, we put double quotations around foo because we want gawk to interpret it literally. Only strings of characters that are
to be literally interpreted must be quoted in this manner.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Don't confuse the quotes used for literal characters with those used to surround the pattern-action pair on the command line. If you use the same quote marks for both, gawk will be unable to process the command properly.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E319"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Comparisons and Arithmetic</B></FONT></CENTER></H4>
<BR>
<P>An essential component of any programming language is the ability to compare two strings or numbers and evaluate whether they are equal or different. The gawk program has several comparisons, including ==, which you just saw in an example. Table 26.1
shows the important comparisons.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 26.1. The important comparisons.</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>Comparison</I>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?