📄 gawk.texi
字号:
along with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.@end smallexampleAlso add information on how to contact you by electronic and paper mail.If the program is interactive, make it output a short notice like thiswhen it starts in an interactive mode:@smallexampleGnomovision version 69, Copyright (C) 19@var{yy} @var{name of author}Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.This is free software, and you are welcome to redistribute itunder certain conditions; type `show c' for details.@end smallexampleThe hypothetical commands @samp{show w} and @samp{show c} should showthe appropriate parts of the General Public License. Of course, thecommands you use may be called something other than @samp{show w} and@samp{show c}; they could even be mouse-clicks or menu items---whateversuits your program.You should also get your employer (if you work as a programmer) or yourschool, if any, to sign a ``copyright disclaimer'' for the program, ifnecessary. Here is a sample; alter the names:@smallexampleYoyodyne, Inc., hereby disclaims all copyright interest in the program`Gnomovision' (which makes passes at compilers) written by James Hacker.@var{signature of Ty Coon}, 1 April 1989Ty Coon, President of Vice@end smallexampleThis General Public License does not permit incorporating your program intoproprietary programs. If your program is a subroutine library, you mayconsider it more useful to permit linking proprietary applications with thelibrary. If this is what you want to do, use the GNU Library GeneralPublic License instead of this License.@node This Manual, Getting Started, Copying, Top@chapter Using this Manual@cindex manual, using this@cindex using this manual@cindex language, @code{awk}@cindex program, @code{awk}@cindex @code{awk} language@cindex @code{awk} programThe term @code{awk} refers to a particular program, and to the language youuse to tell this program what to do. When we need to be careful, we callthe program ``the @code{awk} utility'' and the language ``the @code{awk}language.'' The term @code{gawk} refers to a version of @code{awk} developedas part the GNU project. The purpose of this manual is to explainboth the@code{awk} language and how to run the @code{awk} utility.@refillWhile concentrating on the features of @code{gawk}, the manual will alsoattempt to describe important differences between @code{gawk} and other@code{awk} implementations. In particular, any features that are notin the @sc{posix} standard for @code{awk} will be noted. @refillThe term @dfn{@code{awk} program} refers to a program written by you inthe @code{awk} programming language.@refill@xref{Getting Started, ,Getting Started with @code{awk}}, for the bareessentials you need to know to start using @code{awk}. Some useful ``one-liners'' are included to give you a feel for the@code{awk} language (@pxref{One-liners, ,Useful ``One-liners''}).@ignore@strong{I deleted four paragraphs here because they would confuse thebeginner more than help him. They mention terms such as ``field,''``pattern,'' ``action,'' ``built-in function'' which the beginnerdoesn't know.}@strong{If you can find a way to introduce several of these concepts here,enough to give the reader a map of what is to follow, that mightbe useful. I'm not sure that can be done without taking up morespace than ought to be used here. There may be no way to win.}@strong{ADR: I'd like to tackle this in phase 2 of my editing.}@end ignoreA sample @code{awk} program has been provided for you(@pxref{Sample Program}).@refillIf you find terms that you aren't familiar with, try looking themup in the glossary (@pxref{Glossary}).@refillThe entire @code{awk} language is summarized for quick reference in@ref{Gawk Summary, ,@code{gawk} Summary}. Look there if you just needto refresh your memory about a particular feature.@refillMost of the time complete @code{awk} programs are used as examples, but insome of the more advanced sections, only the part of the @code{awk} programthat illustrates the concept being described is shown.@refill@menu* Sample Data Files:: Sample data files for use in the @code{awk} programs illustrated in this manual.@end menu@node Sample Data Files, , This Manual, This Manual@section Data Files for the Examples@cindex input file, sample@cindex sample input file@cindex @file{BBS-list} fileMany of the examples in this manual take their input from two sampledata files. The first, called @file{BBS-list}, represents a list ofcomputer bulletin board systems together with information about those systems.The second data file, called @file{inventory-shipped}, containsinformation about shipments on a monthly basis. Each line of thesefiles is one @dfn{record}.In the file @file{BBS-list}, each record contains the name of a computerbulletin board, its phone number, the board's baud rate, and a code forthe number of hours it is operational. An @samp{A} in the last columnmeans the board operates 24 hours a day. A @samp{B} in the lastcolumn means the board operates evening and weekend hours, only. A@samp{C} means the board operates only on weekends.@exampleaardvark 555-5553 1200/300 Balpo-net 555-3412 2400/1200/300 Abarfly 555-7685 1200/300 Abites 555-1675 2400/1200/300 Acamelot 555-0542 300 Ccore 555-2912 1200/300 Cfooey 555-1234 2400/1200/300 Bfoot 555-6699 1200/300 Bmacfoo 555-6480 1200/300 Asdace 555-3430 2400/1200/300 Asabafoo 555-2127 1200/300 C@end example@cindex @file{inventory-shipped} fileThe second data file, called @file{inventory-shipped}, representsinformation about shipments during the year. Each record contains the month of the year, the numberof green crates shipped, the number of red boxes shipped, the number oforange bags shipped, and the number of blue packages shipped,respectively. There are 16 entries, covering the 12 months of one yearand 4 months of the next year.@refill@exampleJan 13 25 15 115Feb 15 32 24 226Mar 15 24 34 228Apr 31 52 63 420May 16 34 29 208Jun 31 42 75 492Jul 24 34 67 436Aug 15 34 47 316Sep 13 55 37 277Oct 29 54 68 525Nov 20 87 82 577Dec 17 35 61 401Jan 21 36 64 620Feb 26 58 80 652Mar 24 75 70 495Apr 21 70 74 514@end example@ifinfoIf you are reading this in GNU Emacs using Info, you can copy the regionsof text showing these sample files into your own test files. This way youcan try out the examples shown in the remainder of this document. You dothis by using the command @kbd{M-x write-region} to copy text from the Infofile into a file for use with @code{awk}(@xref{Misc File Ops, , , emacs, GNU Emacs Manual},for more information). Using this information, create your own@file{BBS-list} and @file{inventory-shipped} files, and practice what youlearn in this manual.@end ifinfo@node Getting Started, Reading Files, This Manual, Top@chapter Getting Started with @code{awk}@cindex script, definition of@cindex rule, definition of@cindex program, definition of@cindex basic function of @code{gawk}The basic function of @code{awk} is to search files for lines (or otherunits of text) that contain certain patterns. When a line matches oneof the patterns, @code{awk} performs specified actions on that line.@code{awk} keeps processing input lines in this way until the end of theinput file is reached.@refillWhen you run @code{awk}, you specify an @code{awk} @dfn{program} whichtells @code{awk} what to do. The program consists of a series of@dfn{rules}. (It may also contain @dfn{function definitions}, but thatis an advanced feature, so we will ignore it for now.@xref{User-defined, ,User-defined Functions}.) Each rule specifies onepattern to search for, and one action to perform when that pattern is found.Syntactically, a rule consists of a pattern followed by an action. Theaction is enclosed in curly braces to separate it from the pattern.Rules are usually separated by newlines. Therefore, an @code{awk}program looks like this:@example@var{pattern} @{ @var{action} @}@var{pattern} @{ @var{action} @}@dots{}@end example@menu* Very Simple:: A very simple example.* Two Rules:: A less simple one-line example with two rules.* More Complex:: A more complex example.* Running gawk:: How to run @code{gawk} programs; includes command line syntax.* Comments:: Adding documentation to @code{gawk} programs.* Statements/Lines:: Subdividing or combining statements into lines.* When:: When to use @code{gawk} and when to use other things.@end menu@node Very Simple, Two Rules, Getting Started, Getting Started@section A Very Simple Example@cindex @samp{print $0}The following command runs a simple @code{awk} program that searches theinput file @file{BBS-list} for the string of characters: @samp{foo}. (Astring of characters is usually called, a @dfn{string}.The term @dfn{string} is perhaps based on similar usage in English, suchas ``a string of pearls,'' or, ``a string of cars in a train.'')@exampleawk '/foo/ @{ print $0 @}' BBS-list@end example@noindentWhen lines containing @samp{foo} are found, they are printed, because@w{@samp{print $0}} means print the current line. (Just @samp{print} byitself means the same thing, so we could have written thatinstead.)You will notice that slashes, @samp{/}, surround the string @samp{foo}in the actual @code{awk} program. The slashes indicate that @samp{foo}is a pattern to search for. This type of pattern is called a@dfn{regular expression}, and is covered in more detail later(@pxref{Regexp, ,Regular Expressions as Patterns}). There aresingle-quotes around the @code{awk} program so that the shell won'tinterpret any of it as special shell characters.@refillHere is what this program prints:@example@groupfooey 555-1234 2400/1200/300 Bfoot 555-6699 1200/300 Bmacfoo 555-6480 1200/300 Asabafoo 555-2127 1200/300 C@end group@end example@cindex action, default@cindex pattern, default@cindex default action@cindex default patternIn an @code{awk} rule, either the pattern or the action can be omitted,but not both. If the pattern is omitted, then the action is performedfor @emph{every} input line. If the action is omitted, the defaultaction is to print all lines that match the pattern.Thus, we could leave out the action (the @code{print} statement and the curlybraces) in the above example, and the result would be the same: alllines matching the pattern @samp{foo} would be printed. By comparison,omitting the @code{print} statement but retaining the curly braces makes anempty action that does nothing; then no lines would be printed.@node Two Rules, More Complex, Very Simple, Getting Started@section An Example with Two Rules@cindex how @code{awk} worksThe @code{awk} utility reads the input files one line at atime. For each line, @code{awk} tries the patterns of each of the rules.If several patterns match then several actions are run, in the order inwhich they appear in the @code{awk} program. If no patterns match, thenno actions are run.After processing all the rules (perhaps none) that match the line,@code{awk} reads the next line (however,@pxref{Next Statement, ,The @code{next} Statement}). This continuesuntil the end of the file is reached.@refillFor example, the @code{awk} program:@example/12/ @{ print $0 @}/21/ @{ print $0 @}@end example@noindentcontains two rules. The first rule has the string @samp{12} as thepattern and @samp{print $0} as the action. The second rule has thestring @samp{21} as the pattern and also has @samp{print $0} as theaction. Each rule's action is enclosed in its own pair of braces.This @code{awk} program prints every line that contains the string@samp{12} @emph{or} the string @samp{21}. If a line contains bothstrings, it is printed twice, once by each rule.If we run this program on our two sample data files, @file{BBS-list} and@file{inventory-shipped}, as shown here:@exampleawk '/12/ @{ print $0 @} /21/ @{ print $0 @}' BBS-list inventory-shipped
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -