📄 gawk.hlp
字号:
command or a variable with a string value representing such a command. To output into a file other that the primary output, use print x,... > "file" (or >> "file") or printf(f,x,...) > "file" (or >> "file") similar to the 'getline' example above. '>>' causes output to be appended to an existing file if it exists, or create the file if it doesn't already exist. '>' always creates a new file. The alternate redirection method of '>$' (for RMS text file attributes) is *only* available on the command line, not with 'print' or 'printf' in the current release. To output an error message, use 'print' or 'printf' and redirect the output to file "/dev/stderr" (or equivalently to "SYS$ERROR:" on VMS). 'stderr' will normally be the user's terminal, even if ordinary output is being redirected into a file. To feed awk output into another command, use print x,... | "command" (similarly for 'printf') similar to the second 'getline' example. In this case, output from awk will be passed as input to the specified operating system command. The command must be capable of reading input from 'stdin' ("SYS$INPUT:" on VMS) in order to receive data in this manner. The 'close' function operates on the "file" or "command" argument specified here (either a literal string or a variable or expression resulting in a string value). It completely closes the file or pipe so that further references to the same file or command string would re-open that file or command at the beginning. Closing a pipe or redirection also releases some file-oriented resources. Note: the VMS implementation of GAWK uses temporary files to simulate pipes, so a command must finish before 'getline' can get any input from it, and 'close' must be called for an output pipe before any data can be passed to the specified command.5 formats Formatting characters used by the 'printf' and 'sprintf' functions (each is introduced in the format string by preceding it with a percent sign (%)) % include a literal percent sign (%) in the result c format the next argument as a single ASCII character (prints first character of string argument, or corresponding ASCII character if numeric argument, e.g. 65 is 'A') s format the next argument as a string (numeric arguments are converted into strings on demand) d decimal number (ie, integer value in base 10) i integer (equivalent to decimal) o octal number (integer in base 8) x hexadecimal number (integer in base 16) [lowercase] X hexadecimal number [digits 'A' thru 'E' in uppercase] f floating point number (digits, decimal point, fraction digits) e exponential (scientific notation) number (digit, decimal point, fraction digits, letter 'e', sign '+' or '-', exponent digits) g 'fractional' number in either 'e' or 'f' format, whichever produces shorter result Three optional modifiers can be placed between the initiating percent sign and the format character (doesn't apply to %%). - left justify (only matters when width specifier is present) NN width ['NN' represents 1 or more decimal digits]; actually minimum width to use, longer items will not be truncated; a leading 0 will cause right-justified numbers to be padded on the left with zeroes instead of spaces when they're aligned .MM precision [decimal point followed by 1 or more digits]; used as maximum width for strings (causing truncation if they're actually longer) or as number of fraction digits for 'f' or 'e' numeric formats, or number of significant digits for 'g' numeric format4 user_defined_functions User-defined functions may be created as needed to simplify awk programs or to collect commonly used code into one place. The general syntax of a user-defined function is the 'function' keyword followed by unique function name, followed by a comma-separated parameter list enclosed in parentheses, followed by statement(s) enclosed within braces ({}). A 'return' statement is customary but is not required. function FuncName(arg1,arg2) { # arbitrary statements return (arg1 + arg2) / 2 } If a function does not use 'return' to specify an output value, the result received by the caller will be unpredictable. Functions may be placed in an awk program before, between, or after the pattern-action rules. The abbreviation 'func' may be used in place of 'function', unless POSIX compatibility mode is in effect.3 regular_expressions A regular expression is a shorthand way of specifying a 'wildcard' type of string comparison. Regular expression matching is very fundamental to awk's operation. Meta symbols ^ matches beginning of line or beginning of string; note that embedded newlines ('\n') create multi-line strings, so beginning of line is not necessarily beginning of string $ matches end of line or end of string . any single character (except newline) [ ] set of characters; [ABC] matches either 'A' or 'B' or 'C'; a dash (other than first or last of the set) denotes a range of characters: [A-Z] matches any upper case letter; if the first character of the set is '^', then the sense of match is reversed: [^0-9] matches any non-digit; several characters need to be quoted with backslash (\) if they occur in a set: '\', ']', '-', and '^' | alternation (similar to boolean 'or'); match either of two patterns [for example "^start|stop$" matches leading 'start' or trailing 'stop'] ( ) grouping, alter normal precedence [for example, "^(start|stop)$" matches lines reading either 'start' or 'stop'] * repeated matching; when placed after a pattern, indicates that the pattern should match any number of times [for example, "[a-z][0-9]*" matches a lower case letter followed by zero or more digits] + repeated matching; when placed after a pattern, indicates that the pattern should match one or more times ["[0-9]+" matches any non-empty sequence of digits] ? optional matching; indicates that the pattern can match zero or one times ["[a-z][0-9]?" matches lower case letter alone or followed by a single digit] \ quote; prevent the character which follows from having special meaning A regular expression which matches a string or line will match against the first (left-most) substring which meets the pattern and include the longest sequence of characters which still meets that pattern.3 comments Comments in awk programs are introduced with '#'. Anything after '#' on a line is ignored by GAWK. It's a good idea to include an explanation of what an awk program is doing and also who wrote it and when.3 further_information For complete documentation on GAWK, see "The_GAWK_Manual" from FSF. Source text for it is present in the file GAWK.TEXINFO. A postscript version is available via anonymous FTP from host prep.ai.mit.edu in directory pub/gnu/. For additional documentation on awk--above and beyond that provided in The_GAWK_Manual--see "The_AWK_Programming_Language" by Aho, Weinberger, and Kernighan (2nd edition, 1988), published by Addison-Wesley. It is both a reference on the awk language and a tutorial on awk's use, with many sample programs.3 authors The awk programming language was originally created by Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan in 1977. The language was revised and enhanced in a new version which was released in 1985. GAWK, the GNU implementation of awk, was written in 1986 by Paul Rubin and Jay Fenlason, with advice from Richard Stallman, and with contributions from John Woods. In 1988 and 1989, David Trueman and Arnold Robbins revised GAWK for compatibility with the newer awk. GAWK version 2.11.1 was ported to VMS by Pat Rankin in November, 1989, with further revisions in the Spring of 1990. The VMS port was incorporated into the official GNU distribution of version 2.13 in Spring 1991. (Version 2.12 was never publically released.)2 release_notes GAWK 2.14 tested under VMS V5.5, July, 1992; compatible with VMS versions V4.6 and later. Current source code compatible with DEC's VAXC v3.x and v2.4 or v2.3; also compiles successfully with GNUC (GNU's gcc). VMS POSIX uses c89 and requires VAXC V3.x.3 AWK_LIBRARY GAWK uses a built in search path when looking for a program file specified by the -f option (or the /input qualifier) when that file name does not include a device and/or directory. GAWK will first look in the current default directory, then if the file wasn't found it will look in the directory specified by the translation of logical name "AWK_LIBRARY". Not applicable under VMS POSIX.3 known_problems There are several known problems with GAWK running on VMS. Some can be ignored, others require work-arounds. Note: GAWK in the VMS POSIX environment does not have these problems.4 command_line_parsing The command gawk "program text" will pass the first phase of DCL parsing (the single required parameter is present), then it will give an error that a required element (either /input=awk_file or /commands="program text") is missing. If what was intended (as is most likely) is to pass the program text to the UN*X-style command interface, the following variation is required gawk -- "program text" The presence of "--", which is normally optional, will inhibit the attempt to use DCL parsing (as will any '-' option or redirection).4 file_formats If a file having the RMS attribute "Fortran carriage control" is read as input, it will generate an empty first record if the first actual record begins with a space (leading space becomes a newline). Also, the last record of the file will give a "record not terminated" warning. Both of these minor problems are due to the way that the C Run-Time Library (VAXCRTL) converts record attributes. Another poor feature without a work-around is that there's no way to specify "append if possible, create with RMS text attributes if not" with the current command line I/O redirection. '>>$' isn't supported.4 RS_peculiarities Changing the record separator to something other than newline ('\n') will produce anomalous results for ordinary files. For example, using RS = "\f" and FS = "\n" with the following input |rec 1, line 1 |rec 1, line 2 |^L (form feed) |rec 2, line 1 |rec 2, line 2 |^L (form feed) |rec 3, line 1 |rec 3, line 2 |(end of file) will produce two fields for record 1, but three fields each for records 2 and 3. This is because the form-feed record delimiter is on its own line, so awk sees a newline after it. Since newline is now a field separator, records 2 and 3 will have null first fields. The following awk code will work-around this problem by inserting a null first field in the first record, so that all records can be handled the same by subsequent processing. # fix up for first record (RS != "\n") FNR == 1 { if ( $0 == "" ) #leading separator next #skip its null record else #otherwise, $0 = FS $0 #realign fields } There is a second problem with this same example. It will always trigger a "record not terminated" warning when it reaches the end of file. In the sample shown, there is no final separator; however, if a trailing form-feed were present, it would produce a spurious final record with two null fields. This occurs because the I/O system sees an implicit newline at the end of the last record, so awk sees a pair of null fields separated by that newline. The following code fragment will fix that provided there are no null records (in this case, that would be two consecutive lines containing just form-feeds). # fix up for last record (RS != "\n") $0 == FS { next } #drop spurious final record Note that the "record not terminated" warning will persist.4 cmd_inconsistency The DCL qualifier /OUTPUT is internally equivalent to '>$' output redirection, but the qualifier /INPUT corresponds to the -f option rather than to '<' input redirection.4 exit The exit statement can optionally pass a final status value to the operating system. GAWK expects a UN*X-style value instead of a VMS status value, so 0 indicates success and non-zero indicates failure. The final exit status will be 1 (VMS success) if 0 is used, or even (VMS non-success) if non-zero is used.3 changes Changes between version 2.14 and 2.13.2: General 'next file' construct added 'continue' outside of any loop is treated as 'next' Assorted bug fixes and efficiency improvements _The_GAWK_Manual_ updated Test suite expanded VMS-specific VMS POSIX support added Disk I/O throughput enhanced Pipe emulation improved and incorrect interaction with user-mode redefinition of SYS$OUTPUT eliminated3 prior_changes Changes between version 2.13 and 2.11.1: (2.12 was not released) General CONVFMT and FIELDWIDTHS builtin control variables added systime() and strftime() date/time functions added 'lint' and 'posix' run-time options added '-W' command line option syntax supercedes '-c', '-C', and '-V' '-a' and '-e' regular expression options made obsolete Various bug fixes and efficiency improvements More platforms supported ('officially' including VMS) VMS-specific %g printf format fixed Handling of '\' on command line modified; no longer necessary to double it up Problem redirecting stderr (>&efile) at same time as stdin (<ifile) or stdout (>ofile) has been fixed ``2>&1'' and ``1>&2'' redirection constructs added Interaction between command line I/O redirection and gawk pipes fixed; also, name used for pseudo-pipe temporary file expanded3 license GAWK is covered by the "GNU General Public License", the gist of which is that if you supply this software to a third party, you are expressly forbidden to prevent them from supplying it to a fourth party, and if you supply binaries you must make the source code available to them at no additional cost. Any revisions or modified versions are also covered by the same license. There is no warranty, express or implied, for this software. It is provided "as is." [Disclaimer: This is just an informal summary with no legal basis; refer to the actual GNU General Public License for specific details.]!2 examples!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -