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

📄 gawk.texi

📁 早期freebsd实现
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@end example@noindentwe get the following output:@exampleaardvark     555-5553     1200/300          Balpo-net     555-3412     2400/1200/300     Abarfly       555-7685     1200/300          Abites        555-1675     2400/1200/300     Acore         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          Csabafoo      555-2127     1200/300          CJan  21  36  64 620Apr  21  70  74 514@end example@noindentNote how the line in @file{BBS-list} beginning with @samp{sabafoo}was printed twice, once for each rule.@node More Complex, Running gawk, Two Rules, Getting Started@comment  node-name,  next,  previous,  up@section A More Complex ExampleHere is an example to give you an idea of what typical @code{awk}programs do.  This example shows how @code{awk} can be used tosummarize, select, and rearrange the output of another utility.  It usesfeatures that haven't been covered yet, so don't worry if you don'tunderstand all the details.@examplels -l | awk '$5 == "Nov" @{ sum += $4 @}             END @{ print sum @}'@end exampleThis command prints the total number of bytes in all the files in thecurrent directory that were last modified in November (of any year).(In the C shell you would need to type a semicolon and then a backslashat the end of the first line; in a @sc{posix}-compliant shell, such as theBourne shell or the Bourne-Again shell, you can type the example as shown.)The @w{@samp{ls -l}} part of this example is a command that gives you a listing of the files in a directory, including file size and date.Its output looks like this:@refill@example-rw-r--r--  1 close        1933 Nov  7 13:05 Makefile-rw-r--r--  1 close       10809 Nov  7 13:03 gawk.h-rw-r--r--  1 close         983 Apr 13 12:14 gawk.tab.h-rw-r--r--  1 close       31869 Jun 15 12:20 gawk.y-rw-r--r--  1 close       22414 Nov  7 13:03 gawk1.c-rw-r--r--  1 close       37455 Nov  7 13:03 gawk2.c-rw-r--r--  1 close       27511 Dec  9 13:07 gawk3.c-rw-r--r--  1 close        7989 Nov  7 13:03 gawk4.c@end example@noindentThe first field contains read-write permissions, the second field containsthe number of links to the file, and the third field identifies the owner ofthe file.  The fourth field contains the size of the file in bytes.  Thefifth, sixth, and seventh fields contain the month, day, and time,respectively, that the file was last modified.  Finally, the eighth fieldcontains the name of the file.The @code{$5 == "Nov"} in our @code{awk} program is an expression thattests whether the fifth field of the output from @w{@samp{ls -l}}matches the string @samp{Nov}.  Each time a line has the string@samp{Nov} in its fifth field, the action @samp{@{ sum += $4 @}} isperformed.  This adds the fourth field (the file size) to the variable@code{sum}.  As a result, when @code{awk} has finished reading all theinput lines, @code{sum} is the sum of the sizes of files whoselines matched the pattern.  (This works because @code{awk} variablesare automatically initialized to zero.)@refillAfter the last line of output from @code{ls} has been processed, the@code{END} rule is executed, and the value of @code{sum} isprinted.  In this example, the value of @code{sum} would be 80600.@refillThese more advanced @code{awk} techniques are covered in later sections(@pxref{Actions, ,Overview of Actions}).  Before you can move on to moreadvanced @code{awk} programming, you have to know how @code{awk} interpretsyour input and displays your output.  By manipulating fields and using@code{print} statements, you can produce some very useful and spectacularlooking reports.@refill@node Running gawk, Comments, More Complex, Getting Started@section How to Run @code{awk} Programs@ignoreDate: Mon, 26 Aug 91 09:48:10 +0200From: gatech!vsoc07.cern.ch!matheys (Jean-Pol Matheys (CERN - ECP Division))To: uunet.UU.NET!skeeve!arnoldSubject: RE: status checkThe introduction of Chapter 2 (i.e. before 2.1) should includethe whole of section 2.4  -  it's better to tell people how to run awk programsbefore giving any examplesADR --- he's right.  but for now, don't do this because the rest of thechapter would need some rewriting.@end ignore@cindex command line formats@cindex running @code{awk} programsThere are several ways to run an @code{awk} program.  If the program isshort, it is easiest to include it in the command that runs @code{awk},like this:@exampleawk '@var{program}' @var{input-file1} @var{input-file2} @dots{}@end example@noindentwhere @var{program} consists of a series of patterns and actions, asdescribed earlier.When the program is long, it is usually more convenient to put it in a fileand run it with a command like this:@exampleawk -f @var{program-file} @var{input-file1} @var{input-file2} @dots{}@end example@menu* One-shot::                    Running a short throw-away @code{awk} program.* Read Terminal::               Using no input files (input from                                 terminal instead).* Long::                        Putting permanent @code{awk} programs in files.* Executable Scripts::          Making self-contained @code{awk} programs.@end menu@node One-shot, Read Terminal, Running gawk, Running gawk@subsection One-shot Throw-away @code{awk} ProgramsOnce you are familiar with @code{awk}, you will often type simpleprograms at the moment you want to use them.  Then you can write theprogram as the first argument of the @code{awk} command, like this:@exampleawk '@var{program}' @var{input-file1} @var{input-file2} @dots{}@end example@noindentwhere @var{program} consists of a series of @var{patterns} and@var{actions}, as described earlier.@cindex single quotes, why neededThis command format instructs the shell to start @code{awk} and use the@var{program} to process records in the input file(s).  There are singlequotes around @var{program} so that the shell doesn't interpret any@code{awk} characters as special shell characters.  They also cause theshell to treat all of @var{program} as a single argument for@code{awk} and allow @var{program} to be more than one line long.@refillThis format is also useful for running short or medium-sized @code{awk}programs from shell scripts, because it avoids the need for a separatefile for the @code{awk} program.  A self-contained shell script is morereliable since there are no other files to misplace.@node Read Terminal, Long, One-shot, Running gawk@subsection Running @code{awk} without Input Files@cindex standard input@cindex input, standardYou can also run @code{awk} without any input files.  If you type thecommand line:@refill@exampleawk '@var{program}'@end example@noindentthen @code{awk} applies the @var{program} to the @dfn{standard input},which usually means whatever you type on the terminal.  This continuesuntil you indicate end-of-file by typing @kbd{Control-d}.For example, if you execute this command:@exampleawk '/th/'@end example@noindentwhatever you type next is taken as data for that @code{awk}program.  If you go on to type the following data:@exampleKathyBenTomBethSethKarenThomas@kbd{Control-d}@end example@noindentthen @code{awk} prints this output:@exampleKathyBethSeth@end example@noindent@cindex case sensitivity@cindex pattern, case sensitiveas matching the pattern @samp{th}.  Notice that it did not recognize@samp{Thomas} as matching the pattern.  The @code{awk} language is@dfn{case sensitive}, and matches patterns exactly.  (However, you canoverride this with the variable @code{IGNORECASE}.@xref{Case-sensitivity, ,Case-sensitivity in Matching}.)@node Long, Executable Scripts, Read Terminal, Running gawk@subsection Running Long Programs@cindex running long programs@cindex @samp{-f} option@cindex program file@cindex file, @code{awk} programSometimes your @code{awk} programs can be very long.  In this case it ismore convenient to put the program into a separate file.  To tell@code{awk} to use that file for its program, you type:@refill@exampleawk -f @var{source-file} @var{input-file1} @var{input-file2} @dots{}@end exampleThe @samp{-f} instructs the @code{awk} utility to get the @code{awk} programfrom the file @var{source-file}.  Any file name can be used for@var{source-file}.  For example, you could put the program:@refill@example/th/@end example@noindentinto the file @file{th-prog}.  Then this command:@exampleawk -f th-prog@end example@noindentdoes the same thing as this one:@exampleawk '/th/'@end example@noindentwhich was explained earlier (@pxref{Read Terminal, ,Running @code{awk} without Input Files}).Note that you don't usually need single quotes around the file name that youspecify with @samp{-f}, because most file names don't contain any of the shell'sspecial characters.  Notice that in @file{th-prog}, the @code{awk}program did not have single quotes around it.  The quotes are only neededfor programs that are provided on the @code{awk} command line.If you want to identify your @code{awk} program files clearly as such,you can add the extension @file{.awk} to the file name.  This doesn'taffect the execution of the @code{awk} program, but it does make``housekeeping'' easier.@node Executable Scripts,  , Long, Running gawk@c node-name, next, previous, up@subsection Executable @code{awk} Programs@cindex executable scripts@cindex scripts, executable@cindex self contained programs@cindex program, self contained@cindex @samp{#!}Once you have learned @code{awk}, you may want to write self-contained@code{awk} scripts, using the @samp{#!} script mechanism.  You can dothis on many Unix systems @footnote{The @samp{#!} mechanism works onUnix systems derived from Berkeley Unix, System V Release 4, and some SystemV Release 3 systems.} (and someday on GNU).@refillFor example, you could create a text file named @file{hello}, containingthe following (where @samp{BEGIN} is a feature we have not yetdiscussed):@example#! /bin/awk -f# a sample awk programBEGIN    @{ print "hello, world" @}@end example@noindentAfter making this file executable (with the @code{chmod} command), youcan simply type:@examplehello@end example@noindentat the shell, and the system will arrange to run @code{awk} @footnote{Theline beginning with @samp{#!} lists the full pathname of an interpreterto be run, and an optional initial command line argument to pass to thatinterpreter.  The operating system then runs the interpreter with the givenargument and the full argument list of the executed program.  The first argumentin the list is the full pathname of the @code{awk} program.  The rest of theargument list will either be options to @code{awk}, or data files,or both.} as if you had typed:@refill@example

⌨️ 快捷键说明

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