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

📄 480-482.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 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=480-482//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="477-480.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="482-484.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<P>We could increase the sophistication of <B>gawk</B> searches in a number of ways. Firstly, we could incorporate the use of compound searches, which use three logical operators:</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;&#38;&#38;, which works the same as the logical AND

<DD><B>&#149;</B>&nbsp;&nbsp;||, which works the same as the logical OR

<DD><B>&#149;</B>&nbsp;&nbsp;!, which returns anything NOT equaling the original

</DL>

<P>For example, let&#146;s say we wanted to know how many workers had a value in the fifth field that is greater than or equal to 10:

</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:/$ gawk '$5 &gt;= 10 &#123; print $0 &#125; ' workers

     Geisha  280     555-4221        geisha  10

     Tom     284     555-2121        spike   12

</PRE>

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

<P>We can also combine tests, to print out, for example, all workers who have the fifth field less than 10 and the second field greater than 280:

</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:/$ gawk '$5 &lt; 10 &#38;&#38; $2 &gt; 280 &#123; print $0 &#125; ' workers

     Eric    286     555-6674        erc     8

</PRE>

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

<P>While these examples are obviously contrived, you can use <B>gawk</B> to help pull out all entries that share certain postal (ZIP) codes or all employees who have a salary in a certain range. We&#146;re just scratching the surface with <B>gawk</B>.</P>

<P><B>gawk</B> can also be used to return entire sections of data, as long as you can specify patterns that begin and end the section. To return the records of Eric and Kevin and all between, use the following:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:/$ gawk '$1 ~ /Eric/,/Kevin/ &#123;print $0&#125;' workers

     Eric     286   555-6674   erc       8

     Geisha   280   555-4221   geisha   10

     Kevin    279   555-1112   kevin     2

</PRE>

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

<P>If we don&#146;t want to print the whole record, we can print just a few of the fields, as in the following example, which prints out fields 2 and 1:

</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:/$ gawk '$1 ~ /Eric/,/Kevin/ &#123;print $2, $1&#125;' workers

     286 Eric

     280 Geisha

     279 Kevin

</PRE>

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

<P>As with other UNIX commands, <B>gawk</B> can be used in pipes, and its output can be directed to other files or directly to the printer. For example, if we were looking through a large file and expecting many matches to a particular string (such as salary ranges or employment starting dates), we might want to direct that output to a file or to a printer.</P>

<P>To use <B>gawk</B> with the Linux <B>sort</B> utility, we can sort the output of the last example:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:/$ gawk '$1 ~ /Eric/,/Kevin/ &#123;print $2, $1&#125;' workers | sort

     279 Kevin

     280 Geisha

     286 Eric

</PRE>

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

<P>(Please note that this is sorting on the leading number.)

</P>

<P><B>Gawk</B> also provides some summary abilities. The <I>NR</I> symbol in a <B>gawk</B> command returns the number of records, for example.</P>

<P>We can combine this with <B>gawk</B>&#146;s ability to total fields in an <B>gawk</B> program.</P>

<P><FONT SIZE="+1"><B>Gawk Programs</B></FONT></P>

<P>You&#146;re not limited to what fits on the command line with <B>gawk</B>. You can also store a series of <B>gawk</B> commands in a file and then use <B>gawk</B> to execute the file.</P>

<P>For example, we can store our simplest <B>gawk</B> command, &#123;<B>print</B>&#125;, in a separate file and use the following <B>gawk</B> command:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:/$ gawk -f gawk.1 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>In this case, we&#146;re assuming that the file <B>gawk.1</B> contains our very simple <B>gawk</B> program:</P>

<!-- CODE SNIP //-->

<PRE>

     &#123; print &#125;

</PRE>

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

<P>You can combine this with the <B>gawk BEGIN</B>, <B>END</B>, and <B>NR</B> commands to make a more complex <B>gawk</B> program. When working with this, it&#146;s good to remember that <B>gawk</B> applies each <B>gawk</B> command to every record, that is, every line of text, in the input file. A commandlike <B>&#123;print&#125;</B> says that each line in the input file should be printed.</P>

<P>The <B>gawk BEGIN</B> command lists what to do before reading each line of text. For example:</P>

<!-- CODE SNIP //-->

<PRE>

     BEGIN &#123; print "Workers for Spacely Sprockets"; print "" &#125;

     &#123; print &#125;

</PRE>

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

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="477-480.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="482-484.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 + -