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

📄 0558-0560.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="0555-0557.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0561-0563.html">Next</A>

</CENTER></P>



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













<P>You can control the case sensitivity of gawk regular expressions with the

IGNORECASE variable. When set to the default, zero, pattern matching checks the case in regular expressions. If

you set it to a nonzero value, case is ignored. (The letter

A will match to the letter a.)

</P>









<P>The variable NF is set after each record is read and contains the number of fields. The fields

are determined by the FS or FIELDWIDTHS variables.

</P>









<P>The variable NR contains the total number of records read. It is never less than

FNR, which is reset to zero for each file.

</P>









<P>The default output format for numbers is stored in

OFMT and defaults to the format string

&quot;%.6g&quot;. See the section &quot;printf&quot; for more information on the meaning of the format string.

</P>









<P>The output field separator is contained in OFS with a default of space. This is the character <BR>

or string that is output whenever you use a comma with the print statement, such as the <BR>

following:

</P>



<!-- CODE SNIP //-->

<PRE>{print $1, $2, $3;}

</PRE>

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









<P>This statement print the first three fields of a file separated by spaces. If you want to

separate them by colons (like the /etc/passwd file), you simply set

OFS to a new value: OFS=&quot;:&quot;.

</P>









<P>You can change the output record separator by setting

ORS to a new value. ORS defaults to the newline character

(\n).

</P>









<P>The length of any string matched by the match() function call is stored in

RLENGTH. This is used in conjunction with the

RSTART predefined variable to extract the matched string.

</P>









<P>You can change the input record separator by setting

RS to a new value. RS defaults to the newline character

(\n).

</P>









<P>The starting position of any string matched by the

match() function call is stored in RSTART. This is used in conjunction with the

RLENGTH predefined variable to extract the matched string.

</P>









<P>The SUBSEP variable contains the value used to separate subscripts for multidimension

arrays. The default value is &quot;\034&quot;, which is the double quote character

(&quot;).

</P>

<P>



<TABLE BGCOLOR="#FFFF99">

<TR><TD><B>

NOTE

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

<TR><TD>

<BLOCKQUOTE>

If you change a field ($1, $2, and so on) or the input record

($0), you will cause other predefined variables to change. If your original input record had two fields and you

set $3=&quot;third one&quot;, then NF would be changed

from 2 to 3.

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

</TABLE>

</P>

<H5>

Strings

</H5>









<P>awk supports two general types of variables: numeric (which can consist of the characters

0 through 9, + or -, and the decimal [.]) and character (which can contain any character).

Variables

</P>



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











<P>

that contain characters are generally referred to as strings. A

character string can contain a valid number, text like words, or even a formatted phone number. If the string contains a

valid number, awk can automatically convert and use it as if it were a numeric variable; if you

attempt to use a string that contains a formatted phone number as a numeric variable,

awk will attempt to convert and use it as it were a numeric variable&#151;that contains the value zero.

</P>









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

String Constants

</A></H5>









<P>A string constant is always enclosed within the double quotes

(&quot;&quot;) and can be from zero (an empty string) to many characters long. The exact maximum varies by version of UNIX;

personally, I have never hit the maximum. The double quotes aren't stored in memory. A

typical string constant might look like the following:

</P>



<!-- CODE SNIP //-->

<PRE>&quot;UNIX Unleashed, Second Edition&quot;

</PRE>

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









<P>You have already seen string constants used earlier in this chapter&#151;with comparisons and

the print statement.

</P>









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

String Operators

</A></H5>









<P>There is really only one string operator and that is concatenation. You can combine

multiple strings (constants or variables in any combination) by just putting them together. Listing

27.1 does this with the print statement where the string

&quot;: &quot; is prepended to the input record

($0).

</P>









<P>Listing 27.3 shows a couple ways to concatenate strings.

</P>









<P>Listing 27.3. Concatenating strings example.

</P>

<!-- CODE //-->

<PRE>gawk `BEGIN{x=&quot;abc&quot;&quot;def&quot;; y=&quot;ghi&quot;; z=x y; z2 = &quot;A&quot;x&quot;B&quot;y&quot;C&quot;; print x, y, z, z2}'

abcdef ghi abcdefghi AabcdefBghiC

</PRE>

<!-- END CODE //-->









<P>Variable x is set to two concatenated strings; it prints as

abcdef. Variable y is set to one string for use with the variable

z. Variable z is the concatenation of two string variables printing

as abcdefghi. Finally, the variable z2 shows the concatenation of string constants and string

variables printing as AabcdefBghiC.

</P>









<P>If you leave the comma out of the print statement, all the strings will be concatenated

together and will look like the following:

</P>



<!-- CODE SNIP //-->

<PRE>abcdefghiabcdefghiAabcdefBghiC

</PRE>

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









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

Built-in String Functions

</A></H5>









<P>In addition to the one string operation (concatenation),

gawk provides a number of functions for processing strings.

</P>









<P>Table 27.5 summarizes the built-in string functions in

gawk. Earlier versions of awk don't support all these functions.

</P>



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













<P>Table 27.5. gawk built-in string functions.

</P>



<TABLE WIDTH="360">

<TR><TD>

Function

</TD><TD>

Purpose

</TD></TR>



<TR><TD>

gsub(reg, string, target)

</TD><TD>

Substitutes

string in target string every time the <BR>

regular expression

reg is matched

</TD></TR>



<TR><TD>

index(search, string)

</TD><TD>

Returns the position of the

search string in string

</TD></TR>



<TR><TD>

length(string)

</TD><TD>

The number of characters in string

</TD></TR>



<TR><TD>

match(string, reg)

</TD><TD>

Returns the position in

string that matches the <BR>

regular expression reg

</TD></TR>



<TR><TD>

printf(format, variables)

</TD><TD>

Writes formatted data based on

format; variables is <BR>

the data you want printed

</TD></TR>



<TR><TD>

split(string, store, delim)

</TD><TD>

Splits string into array elements of

store based on <BR>

the delimiter delim

</TD></TR>



<TR><TD>

sprintf(format, variables)

</TD><TD>

Returns a string containing formatted data based

on <BR>

format; variables is the data you want placed in

the <BR>

string

</TD></TR>



<TR><TD>

strftime(format, timestamp)

</TD><TD>

Returns a formatted date or time

string based on <BR>

format; timestamp is the <BR>

time returned by

the systime() function

</TD></TR>



<TR><TD>

sub(reg, string, target)

</TD><TD>

Substitutes string in

target string the first time the <BR>

regular expression

reg is matched

</TD></TR>



<TR><TD>

substr(string, position, len)

</TD><TD>

Returns a substring beginning at

position for len <BR>

number of characters

</TD></TR>



<TR><TD>

tolower(string)

</TD><TD>

Returns the characters in

string as their lowercase <BR>

equivalent

</TD></TR>



<TR><TD>

toupper(string)

</TD><TD>

Returns the characters in

string as their uppercase <BR>

equivalent

</TD></TR>

</TABLE>











<P>The gsub(reg, string, target) function allows you to globally substitute one set of

characters for another (defined in the form of the regular expression

reg) within string. The number of substitutions is returned by the function. If

target is omitted, the input record, $0, is the target. This is patterned after the

substitute command in the ed text editor.

</P>









<P>The index(search, string) function returns the first position (counting from the left) of

the search string within string. If string is omitted,

0 is returned.

</P>









<P>The length(string) function returns a count of the number of characters in

string. awk keeps track of the length of strings internally.

</P>



<P><CENTER>

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

</CENTER></P>









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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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