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

📄 0549-0551.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:


<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="0545-0548.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0552-0554.html">Next</A>

</CENTER></P>



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













<P>One of the ways I use awk most commonly is to process the output of another command

by piping its output into awk. If I wanted to create a custom

listing of files that contained the filename and then the permissions only, I would execute a command like:

</P>



<!-- CODE SNIP //-->

<PRE>ls -l | gawk `{print $NF, &quot; &quot;, $1}'

</PRE>

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









<P>$NF is the last field (which is the filename; I am lazy&#151;I didn't want to count the fields to

figure out its number). $1 is the first field. The output of

ls -l is piped into awk, which processes it for me.

</P>









<P>If I put the awk script into a file (named

lser.awk) and redirected the output to the printer,

I would have a command that looks like:

</P>



<!-- CODE SNIP //-->

<PRE>ls -l | gawk -f lser.awk | lp

</PRE>

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









<P>I tend to save my awk scripts with the file type (suffix) of

.awk just to make it obvious when I am looking through a directory listing. If the program is longer than about 30 characters,

I make a point of saving it because there is no such thing as a &quot;one-time only&quot; program,

user request, or personal need.

</P>

<P>



<CENTER>

<TABLE BGCOLOR="#FFFF99">

<TR><TD><B>

CAUTION

</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

If you forget the -f option before a program filename, your program will be treated as if

it were data.<BR>

If you code your awk program on the command line but place it after the name of your

data file, it will also be treated as if it were data.<BR>

What you will get is odd results.

</BLOCKQUOTE></TD></TR>

</TABLE></CENTER>

</P>

<P>See the section &quot;Commands On-the-Fly&quot; later in this chapter for more examples of using

awk scripts to process piped data.

</P>









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





Patterns and Actions

</A></H4>









<P>Each awk statement consists of two parts: the pattern and the action. The pattern decides

when the action is executed and, of course, the action is what the programmer wants to occur.

Without a pattern, the action is always executed (the pattern can be said to &quot;default to true&quot;).

</P>









<P>There are two special patterns (also known as blocks):

BEGIN and END. The BEGIN code is executed before the first record is read from the file and is used to initialize variables and set

up things like control breaks. The END code is executed after end-of-file is reached and is used

for any cleanup required (like printing final totals on a report). The other patterns are tested

for each record read from the file.

</P>



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













<P>The general program format is to put the BEGIN block at the top, any pattern/action pairs,

and finally, the END block at the end. This is not a language requirement&#151;it is just the way

most people do it (mostly for readability reasons).

</P>









<P>BEGIN and END blocks are optional; if you use them, you should have a maximum of one

each. Don't code two BEGIN blocks, and don't code two

END blocks.

</P>









<P>The action is contained within curly braces ({

}) and can consist of one or many statements. If you omit the pattern portion, it defaults to true, which causes the action to be executed

for every line in the file. If you omit the action, it defaults to

print $0 (print the entire record).

</P>









<P>The pattern is specified before the action. It can be a regular expression (contained within

a pair of slashes [/ /]) that matches part of the input record or an expression that contains

comparison operators. It can also be compound or complex patterns which consists of

expressions and regular expressions combined or a range of

patterns.

</P>









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

Regular Expression Patterns

</A></H5>









<P>The regular expressions used by awk are similar to those used by

grep, egrep, and the UNIX editors ed, ex, and vi. They are the notation used to specify and match strings. A regular

expression consists of characters (like the letters

A, B, and c&#151;that match themselves in the

input) and metacharacters. Metacharacters are characters that have special (meta) meaning; they

do not match to themselves but perform some special function.

</P>









<P>Table 27.1 shows the metacharacters and their behavior.

</P>









<P>Table 27.1. Regular expression metacharacters in

awk.

</P>

<TABLE WIDTH="360">

<TR><TD>

Metacharacter

</TD><TD>

Meaning

</TD></TR>



<TR><TD>

\

</TD><TD>

Escape sequence (next character has special meaning,

\n is the <BR>

newline character and \t is the tab). Any escaped metacharacter

will <BR>

match to that character (as if it were not a metacharacter).

</TD></TR>



<TR><TD>

^

</TD><TD>

Starts match at beginning of string.

</TD></TR>



<TR><TD>

$

</TD><TD>

Matches at end of string.

</TD></TR>



<TR><TD>

.

</TD><TD>

Matches any single character.

</TD></TR>



<TR><TD>

[ABC]

</TD><TD>

Matches any one of A, B, or C.

</TD></TR>



<TR><TD>

[A-Ca-c]

</TD><TD>

Matches any one of A, B, C, a, b, or

c (ranges).

</TD></TR>



<TR><TD>

[^ABC]

</TD><TD>

Matches any character other than A,

B, and C.

</TD></TR>



<TR><TD>

Desk|Chair

</TD><TD>

Matches any one of Desk or Chair.

</TD></TR>



<TR><TD>

[ABC][DEF]

</TD><TD>

Concatenation. Matches any one of

A, B, or C that is followed by any <BR>

one of D, E, or

F.

</TD></TR>



<TR><TD>

*

</TD><TD>

[ABC]*&#151;Matches zero or more occurrences of

A, B, o

r C.

</TD></TR>

</TABLE>





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



<TABLE WIDTH="360">

<TR><TD>

Metacharacter

</TD><TD>

Meaning

</TD></TR>



<TR><TD>

+

</TD><TD>

[ABC]+&#151;Matches one or more occurrences of

A, B, or C.

</TD></TR>



<TR><TD>

?

</TD><TD>

[ABC]?&#151;Matches to an empty string or any one of

A, B, or C.

</TD></TR>



<TR><TD>

()

</TD><TD>

Combines regular expressions. For example,

(Blue|Black)berry <BR>

matches to Blueberry or

Blackberry.

</TD></TR>

</TABLE>











<P>All of these can be combined to form complex search strings. Typical search strings can be

used to search for specific strings (Report Date), strings in different formats

(may, MAY, May), or as groups of characters (any combination of upper- and lowercase characters that spell out

the month of May). These look like the following:

</P>

<!-- CODE SNIP //-->

<PRE>/Report Date/  { print &quot;do something&quot; }

/(may)|(MAY)|(May)/ { print &quot;do something else&quot; }

/[Mm][Aa][Yy]/ { print &quot;do something completely different&quot; }

</PRE>

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









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

Comparison Operators and Patterns

</A></H5>









<P>The comparison operators used by awk are similar to those used by C and the UNIX

shells. They are the notation used to specify and compare values (including strings). A regular

expression alone will match to any portion of the input record. By combining a comparison with

a regular expression, specific fields can be tested.

</P>









<P>Table 27.2 shows the comparison operators and their behavior.

</P>



<P>Table 27.2. Comparison operators in awk.

</P>

<TABLE WIDTH="360">

<TR><TD>

Operator

</TD><TD>

Meaning

</TD></TR>



<TR><TD>

==

</TD><TD>

Is equal to

</TD></TR>



<TR><TD>

&lt;

</TD><TD>

Less than

</TD></TR>



<TR><TD>

&gt;

</TD><TD>

Greater than

</TD></TR>



<TR><TD>

&lt;=

</TD><TD>

Less than or equal to

</TD></TR>



<TR><TD>

&gt;=

</TD><TD>

Greater than or equal to

</TD></TR>



<TR><TD>

!=

</TD><TD>

Not equal to

</TD></TR>



<TR><TD>

~

</TD><TD>

Matched by regular expression

</TD></TR>



<TR><TD>

!~

</TD><TD>

Not matched by regular expression

</TD></TR>

</TABLE>









<P>This enables you to perform specific comparisons on fields instead of the entire record.

Remember that you can also perform them on the entire record by using

$0 instead of a specific field.

</P>



<P><CENTER>

<a href="0545-0548.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0552-0554.html">Next</A>

</CENTER></P>









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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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