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

📄 awk.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Awk</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="A Sed and Awk Micro-Primer"HREF="sedawk.html"><LINKREL="PREVIOUS"TITLE="A Sed and Awk Micro-Primer"HREF="sedawk.html"><LINKREL="NEXT"TITLE="Exit Codes With Special Meanings"HREF="exitcodes.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="sedawk.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Appendix C. A Sed and Awk Micro-Primer</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="exitcodes.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="AWK"></A>C.2. Awk</H1><P><ANAME="AWKREF"></A></P><P><ICLASS="FIRSTTERM">Awk</I> is a full-featured text processing        language with a syntax reminiscent of <ICLASS="FIRSTTERM">C</I>.       While it possesses an extensive set of operators and capabilities,       we will cover only a few of these here - the ones most useful       in shell scripts.</P><P>Awk breaks each line of input passed to it into       <ICLASS="FIRSTTERM">fields</I>. By default, a field       is a string of consecutive characters delimited by <AHREF="special-chars.html#WHITESPACEREF">whitespace</A>, though there are options       for changing this. Awk parses and operates on each separate       field. This makes it ideal for handling structured text files       -- especially tables -- data organized into consistent chunks,       such as rows and columns.</P><P><AHREF="variables.html#SNGLQUO">Strong quoting</A> and <AHREF="special-chars.html#CODEBLOCKREF">curly brackets</A> enclose blocks of       awk code within a shell script.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# $1 is field #1, $2 is field #2, etc.   2&nbsp;   3&nbsp;echo one two | awk '{print $1}'   4&nbsp;# one   5&nbsp;   6&nbsp;echo one two | awk '{print $2}'   7&nbsp;# two   8&nbsp;   9&nbsp;# But what is field #0 ($0)?  10&nbsp;echo one two | awk '{print $0}'  11&nbsp;# one two  12&nbsp;# All the fields!  13&nbsp;  14&nbsp;  15&nbsp;awk '{print $3}' $filename  16&nbsp;# Prints field #3 of file $filename to stdout.  17&nbsp;  18&nbsp;awk '{print $1 $5 $6}' $filename  19&nbsp;# Prints fields #1, #5, and #6 of file $filename.  20&nbsp;  21&nbsp;awk '{print $0}' $filename  22&nbsp;# Prints the entire file!  23&nbsp;# Same effect as:   cat $filename . . . or . . . sed '' $filename</PRE></TD></TR></TABLE></P><P>We have just seen the awk <ICLASS="FIRSTTERM">print</I> command       in action. The only other feature of awk we need to deal with       here is variables. Awk handles variables similarly to shell       scripts, though a bit more flexibly.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;{ total += ${column_number} }</PRE></TD></TR></TABLE>       This adds the value of <TTCLASS="PARAMETER"><I>column_number</I></TT> to       the running total of <TTCLASS="PARAMETER"><I>total</I></TT>&#62;. Finally, to print       <SPANCLASS="QUOTE">"total"</SPAN>, there is an <BCLASS="COMMAND">END</B> command       block, executed after the script has processed all its input.       <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;END { print total }</PRE></TD></TR></TABLE></P><P>Corresponding to the <BCLASS="COMMAND">END</B>, there is a       <BCLASS="COMMAND">BEGIN</B>, for a code block to be performed before awk       starts processing its input.</P><P>The following example illustrates how <BCLASS="COMMAND">awk</B> can       add text-parsing tools to a shell script.</P><DIVCLASS="EXAMPLE"><HR><ANAME="LETTERCOUNT2"></A><P><B>Example C-1. Counting Letter Occurrences</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#! /bin/sh   2&nbsp;# letter-count2.sh: Counting letter occurrences in a text file.   3&nbsp;#   4&nbsp;# Script by nyal [nyal@voila.fr].   5&nbsp;# Used in ABS Guide with permission.   6&nbsp;# Recommented and reformatted by ABS Guide author.   7&nbsp;# Version 1.1: Modified to work with gawk 3.1.3.   8&nbsp;#              (Will still work with earlier versions.)   9&nbsp;  10&nbsp;  11&nbsp;INIT_TAB_AWK=""  12&nbsp;# Parameter to initialize awk script.  13&nbsp;count_case=0  14&nbsp;FILE_PARSE=$1  15&nbsp;  16&nbsp;E_PARAMERR=65  17&nbsp;  18&nbsp;usage()  19&nbsp;{  20&nbsp;    echo "Usage: letter-count.sh file letters" 2&#62;&#38;1  21&nbsp;    # For example:   ./letter-count2.sh filename.txt a b c  22&nbsp;    exit $E_PARAMERR  # Too few arguments passed to script.  23&nbsp;}  24&nbsp;  25&nbsp;if [ ! -f "$1" ] ; then  26&nbsp;    echo "$1: No such file." 2&#62;&#38;1  27&nbsp;    usage                 # Print usage message and exit.  28&nbsp;fi   29&nbsp;  30&nbsp;if [ -z "$2" ] ; then  31&nbsp;    echo "$2: No letters specified." 2&#62;&#38;1  32&nbsp;    usage  33&nbsp;fi   34&nbsp;  35&nbsp;shift                      # Letters specified.  36&nbsp;for letter in `echo $@`    # For each one . . .  37&nbsp;  do  38&nbsp;  INIT_TAB_AWK="$INIT_TAB_AWK tab_search[${count_case}] = \  39&nbsp;  \"$letter\"; final_tab[${count_case}] = 0; "   40&nbsp;  # Pass as parameter to awk script below.  41&nbsp;  count_case=`expr $count_case + 1`  42&nbsp;done  43&nbsp;  44&nbsp;# DEBUG:  45&nbsp;# echo $INIT_TAB_AWK;  46&nbsp;  47&nbsp;cat $FILE_PARSE |  48&nbsp;# Pipe the target file to the following awk script.  49&nbsp;  50&nbsp;# ---------------------------------------------------------------------  51&nbsp;# Earlier version of script:  52&nbsp;# awk -v tab_search=0 -v final_tab=0 -v tab=0 -v \  53&nbsp;# nb_letter=0 -v chara=0 -v chara2=0 \  54&nbsp;  55&nbsp;awk \  56&nbsp;"BEGIN { $INIT_TAB_AWK } \  57&nbsp;{ split(\$0, tab, \"\"); \  58&nbsp;for (chara in tab) \  59&nbsp;{ for (chara2 in tab_search) \  60&nbsp;{ if (tab_search[chara2] == tab[chara]) { final_tab[chara2]++ } } } } \  61&nbsp;END { for (chara in final_tab) \  62&nbsp;{ print tab_search[chara] \" =&#62; \" final_tab[chara] } }"  63&nbsp;# ---------------------------------------------------------------------  64&nbsp;#  Nothing all that complicated, just . . .  65&nbsp;#+ for-loops, if-tests, and a couple of specialized functions.  66&nbsp;  67&nbsp;exit $?  68&nbsp;  69&nbsp;# Compare this script to letter-count.sh.</PRE></TD></TR></TABLE><HR></DIV><P>For simpler examples of awk within shell scripts, see:       <OLTYPE="1"><LI><P><AHREF="internal.html#EX44">Example 14-13</A></P></LI><LI><P><AHREF="redircb.html#REDIR4">Example 19-8</A></P></LI><LI><P><AHREF="filearchiv.html#STRIPC">Example 15-31</A></P></LI><LI><P><AHREF="wrapper.html#COLTOTALER">Example 33-5</A></P></LI><LI><P><AHREF="ivr.html#COLTOTALER2">Example 9-25</A></P></LI><LI><P><AHREF="internal.html#COLTOTALER3">Example 14-20</A></P></LI><LI><P><AHREF="procref1.html#PIDID">Example 27-2</A></P></LI><LI><P><AHREF="procref1.html#CONSTAT">Example 27-3</A></P></LI><LI><P><AHREF="loops.html#FILEINFO">Example 10-3</A></P></LI><LI><P><AHREF="extmisc.html#BLOTOUT">Example 15-58</A></P></LI><LI><P><AHREF="randomvar.html#SEEDINGRANDOM">Example 9-31</A></P></LI><LI><P><AHREF="moreadv.html#IDELETE">Example 15-4</A></P></LI><LI><P><AHREF="string-manipulation.html#SUBSTRINGEX">Example 9-15</A></P></LI><LI><P><AHREF="assortedtips.html#SUMPRODUCT">Example 33-16</A></P></LI><LI><P><AHREF="loops.html#USERLIST">Example 10-8</A></P></LI><LI><P><AHREF="wrapper.html#PRASC">Example 33-4</A></P></LI><LI><P><AHREF="mathc.html#HYPOT">Example 15-51</A></P></LI></OL>      </P><P>That's all the awk we'll cover here, folks, but there's lots       more to learn. See the appropriate references in the <AHREF="biblio.html"><I>Bibliography</I></A>.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sedawk.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="exitcodes.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">A Sed and Awk Micro-Primer</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="sedawk.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Exit Codes With Special Meanings</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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