0552-0554.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 386 行

HTML
386
字号


<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="0549-0551.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0555-0557.html">Next</A>

</CENTER></P>



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













<P>Typical search strings can be used to search for a name in the first field

(Bob) and compare specific fields with regular expressions:

</P>

<!-- CODE SNIP //-->

<PRE>$1 == &quot;Bob&quot;   { print &quot;Bob stuff&quot; }

$2 ~ /(may)|(MAY)|(May)/ { print &quot;May stuff&quot; }

$3 !~ /[Mm][Aa][Yy]/ { print &quot;other May stuff&quot; }

</PRE>

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









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

Compound Pattern Operators

</A></H5>









<P>The compound pattern operators used by awk are similar to those used by C and the

UNIX shells. They are the notation used to combine other patterns (expressions or regular

expressions) into a complex form of logic.

</P>









<P>Table 27.3 shows the compound pattern operators and their behavior.

</P>









<P>Table 27.3. Compound pattern operators in awk.

</P>



<TABLE WIDTH="360">

<TR><TD>

Operator

</TD><TD>

Meaning

</TD></TR>



<TR><TD>

&amp;&amp;

</TD><TD>

Logical AND

</TD></TR>



<TR><TD>

||

</TD><TD>

Logical OR

</TD></TR>



<TR><TD>

!

</TD><TD>

Logical NOT

</TD></TR>



<TR><TD>

()

</TD><TD>

Parentheses&#151;used to group compound statements

</TD></TR>

</TABLE>









<P>If I wanted to execute some action (print a special message, for instance), if the first field

contained the value &quot;Bob&quot; and the fourth field contained the value

&quot;Street&quot;, I could use a compound pattern that looks

like:

</P>



<!-- CODE SNIP //-->

<PRE>$1 == &quot;Bob&quot; &amp;&amp; $4 == &quot;Street&quot; {print&quot;some message&quot;}

</PRE>

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









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

Range Pattern Operators

</A></H5>









<P>The range pattern is slightly more complex than the other types&#151;it is set true when the

first pattern is matched and remains true until the second pattern becomes true. The catch is

that the file needs to be sorted on the fields that the range pattern matches. Otherwise, it might

be set true prematurely or end early.

</P>









<P>The individual patterns in a range pattern are separated by a comma

(,). If you have twenty-six files in your directory with the names

A to Z, you can show a range of the files as shown

in Listing 27.2.

</P>









<P>Listing 27.2. Range pattern example.

</P>

<!-- CODE SNIP //-->

<PRE>$ ls | gawk `{$1 == &quot;B&quot;, $1 == &quot;D&quot;}'

B

C

D

</PRE>

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



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



<!-- CODE //-->

<PRE>

$ ls | gawk `{$1 == &quot;B&quot;, $1 &lt;= &quot;D&quot;}'

B

$ ls | gawk `{$1 == &quot;B&quot;, $1 &gt; &quot;D&quot;}'

B

C

D

E

$ _

</PRE>

<!-- END CODE //-->









<P>The first example is obvious&#151;all the records between

B and D are shown. The other examples are less intuitive, but the key to remember is that the pattern is done when the second

condition is true. The second gawk command only shows the

B because C is less than or equal to D (making the second condition true). The third

gawk shows B through E because E is the first one that is greater than

D (making the second condition true).

</P>









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





Handling Input

</A></H4>









<P>As each record is read by awk, it breaks it down into fields and then searches for matching

patterns and the related actions to perform. It assumes that each record occupies a single line

(the newline character, by definition, ends a record). Lines that are just blanks or are empty

(just the newline) count as records, just with very few fields (usually zero).

</P>









<P>You can force awk to read the next record in a file (cease searching for pattern matches) by

using the next statement. next is similar to the C

continue command&#151;control returns to the outermost loop. In

awk, the outermost loop is the automatic read of the file. If you decide

you need to break out of your program completely, you can use the

exit statement. exit will act like the end-of-file was reached and pass control to the

END block (if one exists). If exit is in the END block, the program will immediately exit.

</P>









<P>By default, fields are separated by spaces. It doesn't matter to

awk whether there is one or many spaces&#151;the next field begins when the first nonspace character is found. You can change

the field separator by setting the variable FS to that character. To set your field separator to

the colon (:), which is the separator in

/etc/passwd, code the following:

</P>



<!-- CODE SNIP //-->

<PRE>BEGIN { FS = &quot;:&quot; }

</PRE>

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









<P>The general format of the file looks something like the following:

</P>



<!-- CODE SNIP //-->

<PRE>david:!:207:1017:David B Horvath,CCP:/u/david:/bin/ksh

</PRE>

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









<P>If you want to list the names of everyone on the system, use the following:

</P>



<!-- CODE SNIP //-->

<PRE>gawk --field-separator=: `{ print $5 }' /etc/passwd

</PRE>

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









<P>You will then see a list of everyone's name. In this example, I set the field separator

variable (FS) from the command line using the gawk format command-line options

(--field-<BR>

separator=:). I could also use -F :, which is supported by all versions of

awk.

</P>



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













<P>The first field is $1, the second is $2, and so on. The entire record is contained in

$0. You can get the last field (if you are lazy like me and don't want to count) by referencing

$NF. NF is the number of fields in a record.

</P>









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





Coding Your Program

</A></H4>









<P>The nice thing about awk is that, with a few exceptions, it is free format&#151;like the C

language. Blank lines are ignored. Statements can be placed on the same line or split up in any form

you like. awk recognizes whitespace, much like C does. The following two lines are essentially

the same:

</P>

<!-- CODE SNIP //-->

<PRE>$1==&quot;Bob&quot;{print&quot;Bob stuff&quot;}

$1    ==    &quot;Bob&quot;       {     print    &quot;Bob stuff&quot;     }

</PRE>

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









<P>Spaces within quotes are significant because they will appear in the output or are used in

a comparison for matching. The other spaces are not. You can also split up the action (but

you have to have the opening curly brace on the same line as the pattern):

</P>

<!-- CODE SNIP //-->

<PRE>$1    ==    &quot;Bob&quot;       {

                           print    &quot;Bob stuff&quot;

                        }

</PRE>

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









<P>You can have multiple statements within an action. If you place them on the same line,

you need to use semicolons (;) to separate them (so

awk can tell when one ends and the next begins). Printing multiple lines looks like the following:

</P>

<!-- CODE //-->

<PRE>$1    ==    &quot;Bob&quot;       {

                           print    &quot;Bob stuff&quot;; print    &quot;more stuff&quot;;

                           &Acirc;print    &quot;last stuff&quot;;

                        }

</PRE>

<!-- END CODE //-->









<P>You can also put the statements on separate lines. When you do that, you don't need to

code the semicolons, and the code looks like the following:

</P>

<!-- CODE //-->

<PRE>$1    ==    &quot;Bob&quot;       {

                           print    &quot;Bob stuff&quot;

                           print    &quot;more stuff&quot;

                           print    &quot;last stuff&quot;

                        }

</PRE>

<!-- END CODE //-->









<P>Personally, I am in the habit of coding the semicolon after each statement because that is

the way I have to do it in C. To awk, the following example is just like the previous (but you can

see the semicolons):

</P>

<!-- CODE //-->

<PRE>$1    ==    &quot;Bob&quot;       {

                           print    &quot;Bob stuff&quot;;

                           print    &quot;more stuff&quot;;

                           print    &quot;last stuff&quot;;

                        }

</PRE>

<!-- END CODE //-->









<P>Another thing you should make use of is comments. Anything on a line after the pound sign

or octothorpe (#) is ignored by awk. These are notes designed for the programmer to read and

aid in the understanding of the program code. In general, the more comments you place in a

program, the easier it is to maintain.

</P>



<P><CENTER>

<a href="0549-0551.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0555-0557.html">Next</A>

</CENTER></P>









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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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