📄 awk
字号:
.fp 3 G....TM "78-1271-12, 78-1273-6" 39199 39199-11.ND "September 1, 1978"....TR 68.RP. \" macros here.tr _\(em.if t .tr ~\(ap.tr |\(or.tr *\(**.de UC\&\\$3\s-1\\$1\\s0\&\\$2...de IT.if n .ul\&\\$3\f2\\$1\fP\|\\$2...de UL.if n .ul\&\\$3\f3\\$1\fP\&\\$2...de P1.DS I 3n.nf.if n .ta 5 10 15 20 25 30 35 40 45 50 55 60.if t .ta .3i .6i .9i 1.2i.if t .tr -\-'\(fm*\(**.if t .tr _\(ul.ft 3.lg 0.ss 18. \"use first argument as indent if present...de P2.ps \\n(PS.vs \\n(VSp.ft R.ss 12.if n .ls 2.tr --''``^^!!.if t .tr _\(em.fi.lg.DE...hw semi-colon.hy 14. \"2=not last lines; 4= no -xx; 8=no xx-. \"special chars in programs.de WS.sp \\$1... \" end of macros.TLAwk \(em A Pattern Scanning and Processing Language.br(Second Edition).AU "MH 2C-522" 4862Alfred V. Aho.AU "MH 2C-518" 6021Brian W. Kernighan.AU "MH 2C-514" 7214Peter J. Weinberger.AI.MH.AB.IT Awkis a programming language whosebasic operationis to search a set of filesfor patterns, and to perform specified actions upon lines or fields of lines whichcontain instances of those patterns..IT Awkmakes certain data selection and transformation operations easy to express;for example, the.IT awkprogram.sp.ce.ft 3length > 72.ft.spprints all input lines whose length exceeds 72 characters;the program.ce.sp.ft 3NF % 2 == 0.ft R.spprints all lines with an even number of fields;and the program.ce.sp.ft 3{ $1 = log($1); print }.ft R.spreplaces the first field of each line by its logarithm..PP.IT Awkpatterns may include arbitrary boolean combinations of regular expressionsand of relational operators on strings, numbers, fields, variables, and array elements.Actions may include the same pattern-matching constructions as in patterns,as well asarithmetic and string expressions and assignments,.UL if-else ,.UL while ,.UL forstatements,and multiple output streams..PPThis report contains a user's guide, a discussion of the design and implementation of.IT awk ,and some timing statistics.....It supersedes TM-77-1271-5, dated September 8, 1977..AE.CS 6 1 7 0 1 4.if n .ls 2.nr PS 9.nr VS 11.NHIntroduction.if t .2C.PP.IT Awkis a programming language designed to makemany commoninformation retrieval and text manipulation taskseasy to state and to perform..PPThe basic operation of.IT awkis to scan a set of input lines in order,searching for lines which match any of a set of patternswhich the user has specified.For each pattern, an action can be specified;this action will be performed on each line that matches the pattern..PPReaders familiar with the.UXprogram.IT grep\|.[unix program manual.]will recognizethe approach, although in.IT awkthe patterns may be moregeneral than in.IT grep ,and the actions allowed are more involved than merelyprinting the matching line.For example, the.IT awkprogram.P1{print $3, $2}.P2prints the third and second columns of a tablein that order.The program.P1$2 ~ /A\||B\||C/.P2prints all input lines with an A, B, or C in the second field.The program.P1$1 != prev { print; prev = $1 }.P2prints all lines in which the first field is differentfrom the previous first field..NH 2Usage.PPThe command.P1awk program [files].P2executes the.IT awkcommands inthe string.UL programon the set of named files,or on the standard input if there are no files.The statements can also be placed in a file.UL pfile ,and executed by the command.P1awk -f pfile [files].P2.NH 2Program Structure.PPAn.IT awkprogram is a sequence of statements of the form:.P1.ft I pattern { action } pattern { action } ....ft 3.P2Each line of inputis matched againsteach of the patterns in turn.For each pattern that matches, the associated actionis executed.When all the patterns have been tested, the next lineis fetched and the matching starts over..PPEither the pattern or the action may be left out,but not both.If there is no action for a pattern,the matching line is simplycopied to the output.(Thus a line which matches several patterns can be printed several times.)If there is no pattern for an action,then the action is performed for every input line.A line which matches no pattern is ignored..PPSince patterns and actions are both optional,actions must be enclosed in bracesto distinguish them from patterns..NH 2Records and Fields.PP.IT Awkinput is divided into``records'' terminated by a record separator.The default record separator is a newline,so by default.IT awkprocesses its input a line at a time.The number of the current record is available in a variablenamed.UL NR ..PPEach input recordis considered to be divided into ``fields.''Fields are normally separated bywhite space \(em blanks or tabs \(embut the input field separator may be changed, as described below.Fields are referred to as.UL "$1, $2,"and so forth,where.UL $1is the first field,and.UL $0is the whole input record itself.Fields may be assigned to.The number of fields in the current recordis available in a variable named.UL NF ..PPThe variables.UL FSand.UL RSrefer to the input field and record separators;they may be changed at any time to any single character.The optional command-line argument\f3\-F\fIc\fRmay also be used to set.UL FSto the character.IT c ..PPIf the record separator is empty,an empty input line is taken as the record separator,and blanks, tabs and newlines are treated as field separators..PPThe variable.UL FILENAMEcontains the name of the current input file..NH 2Printing.PPAn action may have no pattern,in which case the action is executed foralllines.The simplest action is to print some or all of a record;this is accomplished by the.IT awkcommand.UL print .The.IT awkprogram.P1{ print }.P2prints each record, thus copying the input to the output intact.More useful is to print a field or fields from each record.For instance, .P1print $2, $1.P2prints the first two fields in reverse order.Items separated by a comma in the print statement will be separated by the current output field separatorwhen output.Items not separated by commas will be concatenated,so.P1print $1 $2.P2runs the first and second fields together..PPThe predefined variables.UL NFand.UL NRcan be used;for example.P1{ print NR, NF, $0 }.P2prints each record preceded by the record number and the number of fields..PPOutput may be diverted to multiple files;the program.P1{ print $1 >"foo1"; print $2 >"foo2" }.P2writes the first field,.UL $1 ,on the file.UL foo1 ,and the second field on file.UL foo2 .The.UL >>notation can also be used:.P1print $1 >>"foo".P2appends the output to the file.UL foo .(In each case,the output files arecreated if necessary.)The file name can be a variable or a field as well as a constant;for example,.P1print $1 >$2.P2uses the contents of field 2 as a file name..PPNaturally there is a limit on the number of output files;currently it is 10..PPSimilarly, output can be piped into another process(on.UC UNIXonly); for instance,.P1print | "mail bwk".P2mails the output to.UL bwk ..PPThe variables.UL OFSand.UL ORSmay be used to change the currentoutput field separator and outputrecord separator.The output record separator isappended to the output of the.UL printstatement..PP.IT Awkalso provides the.UL printfstatement for output formatting:.P1printf format expr, expr, ....P2formats the expressions in the listaccording to the specificationin.UL formatand prints them.For example,.P1printf "%8.2f %10ld\en", $1, $2.P2prints .UL $1as a floating point number 8 digits wide,with two after the decimal point,and.UL $2as a 10-digit long decimal number,followed by a newline.No output separators are produced automatically;you must add them yourself,as in this example.The version of.UL printfis identical to that used with C..[C programm language prentice hall 1978.].NH 1Patterns.PPA pattern in front of an action acts as a selectorthat determines whether the action is to be executed.A variety of expressions may be used as patterns:regular expressions,arithmetic relational expressions,string-valued expressions,and arbitrary booleancombinations of these..NH 2BEGIN and END.PPThe special pattern.UL BEGINmatches the beginning of the input,before the first record is read.The pattern.UL ENDmatches the end of the input,after the last record has been processed..UL BEGINand.UL ENDthus provide a way to gain control before and after processing,for initialization and wrapup..PPAs an example, the field separatorcan be set to a colon by.P1BEGIN { FS = ":" }.ft I\&... rest of program ....ft 3.P2Or the input lines may be counted by.P1END { print NR }.P2If.UL BEGINis present, it must be the first pattern;.UL ENDmust be the last if used..NH 2Regular Expressions.PPThe simplest regular expression is a literal string of charactersenclosed in slashes,like.P1/smith/.P2Thisis actually a complete.IT awkprogram whichwill print all lines which contain any occurrenceof the name ``smith''.If a line contains ``smith''as part of a larger word,it will also be printed, as in.P1blacksmithing.P2.PP.IT Awkregular expressions include the regular expressionforms found inthe.UC UNIXtext editor.IT ed\|.[unix program manual.]and.IT grep(without back-referencing).In addition,.IT awkallowsparentheses for grouping, | for alternatives,.UL +for ``one or more'', and.UL ?for ``zero or one'',all as in.IT lex .Character classesmay be abbreviated:.UL [a\-zA\-Z0\-9]is the set of all letters and digits.As an example,the.IT awkprogram.P1/[Aa]ho\||[Ww]einberger\||[Kk]ernighan/.P2will print all lines which contain any of the names``Aho,'' ``Weinberger'' or ``Kernighan,''whether capitalized or not..PPRegular expressions(with the extensions listed above)must be enclosed in slashes,just as in.IT edand.IT sed .Within a regular expression,blanks and the regular expressionmetacharacters are significant.To turn of the magic meaningof one of the regular expression characters,precede it with a backslash.An example is the pattern.P1/\|\e/\^.\^*\e//.P2which matches any string of charactersenclosed in slashes..PPOne can also specify that any field or variablematchesa regular expression (or does not match it) with the operators.UL ~and.UL !~ .The program.P1$1 ~ /[jJ]ohn/.P2prints all lines where the first field matches ``john'' or ``John.''Notice that this will also match ``Johnson'', ``St. Johnsbury'', and so on.To restrict it to exactly.UL [jJ]ohn ,use.P1$1 ~ /^[jJ]ohn$/.P2The caret ^ refers to the beginningof a line or field;the dollar sign.UL $refers to the end..NH 2Relational Expressions.PPAn.IT awkpattern can be a relational expressioninvolving the usual relational operators.UL < ,.UL <= ,.UL == ,.UL != ,.UL >= ,and.UL > .An example is.P1$2 > $1 + 100.P2which selects lines where the second fieldis at least 100 greater than the first field.Similarly,.P1NF % 2 == 0.P2prints lines with an even number of fields..PPIn relational tests, if neither operand is numeric,a string comparison is made;otherwise it is numeric.Thus,.P1$1 >= "s".P2selects lines that begin with an.UL s ,.UL t ,.UL u ,etc.In the absence of any other information,fields are treated as strings, sothe program.P1$1 > $2.P2will perform a string comparison..NH 2Combinations of Patterns.PPA pattern can be any boolean combination of patterns,using the operators.UL \||\||(or),.UL &&(and), and.UL !(not).For example,.P1$1 >= "s" && $1 < "t" && $1 != "smith".P2selects lines where the first field begins with ``s'', but is not ``smith''..UL &&and.UL \||\||guarantee that their operandswill be evaluatedfrom left to right;evaluation stops as soon as the truth or falsehoodis determined..NH 2Pattern Ranges.PPThe ``pattern'' that selects an action may alsoconsist of two patterns separated by a comma, as in.P1pat1, pat2 { ... }.P2In this case, the action is performed for each line betweenan occurrence of.UL pat1and the next occurrence of.UL pat2(inclusive).For example,.P1/start/, /stop/.P2prints all lines between.UL startand.UL stop ,while.P1NR == 100, NR == 200 { ... }.P2does the action for lines 100 through 200of the input..NH 1Actions.PPAn.IT awkaction is a sequence of action statementsterminated by newlines or semicolons.These action statements can be used to do a variety ofbookkeeping and string manipulating tasks..NH 2Built-in Functions.PP.IT Awkprovides a ``length'' functionto compute the length of a string of characters.This program prints each record,preceded by its length:.P1{print length, $0}.P2.UL lengthby itself is a ``pseudo-variable'' whichyields the length of the current record;.UL length(argument)is a function which yields the length of its argument,as inthe equivalent.P1{print length($0), $0}.P2The argument may be any expression..PP.IT Awkalsoprovides the arithmetic functions.UL sqrt ,.UL log ,.UL exp ,and.UL int ,forsquare root,base
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -