📄 guide to awk.htm
字号:
(Back to <A HREF = "#pattern"> pattern matching commands</A>)</P><P>Common regular expressions are composed of following way:<BR><PRE> text Text as written . Any one character [string] Any one character in the string "string" [a-k] Any one character from "a" to "k" * Zero or more repeats of previous regular expression ^ At the beginning of a regular expression limits it to the beginning of the line $ At the end of a regular expression limits it to the end of the line \ Removes metacharacter's special meaning \( \) Grouping brackets, as in mathematics</PRE></P><P>Please note, regular expressions can only be used in the pattern part of awk command. This makes pattern part more flexiblein finding character patterns inside words thand command part.If-instruction with substring-function may imitate regularexpressions to some degree.In practice regular expressions should be as simple aspossible. Complex regular expressions are difficult to debug.</P><P>The details of awk <A NAME="Instructions"> instructions are following.You separate instructions with semicolon ";" and form one compositeinstruction from several instructions by putting them withincurly brackets "{" and "}". See examples for more details.<STRONG>Assignment statement</STRONG> is:<PRE> variable1 = variable2 or constant</PRE>The content of variable2 or constant is copied into variable1, oldcontent of variable1 is lost. Variables and constanst can contain numbers or strings. Some examples:<PRE> first = "first" first = $1 first = 1 second = first</PRE></P><P>There are two <STRONG>print statements: </STRONG><PRE> print (variables or constants to be printed) printf ("format string", variables or constants to be printed)</PRE><I>print</I> is the usual print command. Variables and constants tobe printed are separated with commas "," and each print statementprints one line. Some examples:<PRE> print ( $0 ) print ( "This was input line: ", $0 ) print ( "This was first word: ", $1, " and this second: ", $2 ) print ( "The content of variable _first_ is: ", first )</PRE>You use <I>printf</I> for formatted printing. printf prints everythingon one line if you don't put newline charater into format string,constants to be printed or into variables to be printed. Formatstring contains text and conversion specifications. Conversionspecifications consist of percent character "%", zero or moreflags, field width, precision, conversion specifier and conversionqualifier. Percent character and conversion specifier must be there,other are optional. Conversion specifications correspond one byone to the variables or constants to be printed.Most common <I> conversion specifiers </I> are:<PRE> %d signed integer %e signed fractional with exponent %f signed fractional without exponent %s sequence of characters in corresponding variable or constant %% percent character</PRE> Escape sequences are used to format printf output.Most common <I>escape sequences</I> are:<PRE> \f form feed, new page \n new line (\012 or \015) \r carriage return, overprint \t horizontal tab \v vertical tab \' single quote \" double quote \\ backslash \0 null (character value 000) \a alert, bell \b backspace \040 space \ddd octal notation \xddd hexadecimal notation</PRE>Some printf examples:<PRE> printf("%d %e %f", A, B, C) printf("%s %d %% %s", "It is", proof, " alcohol") printf("First line, number %d \n and second line, number %d", n1, n2)</PRE></P><P><STRONG>Conditional statement, </STRONG> also known asif-else-statement is:<PRE> if (condition) command1 [ else command2 ]</PRE>If the <I>condition</I> is true, <I>command1</I> will beexecuted, if <I>condition</I> is false, <I>command2</I>will be executed. The whole else-part may be left out.Some examples:<PRE> if ( a==0 ) print("Zero") else print("Not zero") if ( a==0 ) { print("Zero") } else { print("Not zero")} if ( a==0 ) print("Zero")</PRE></P><P><STRONG>Repeating statements </STRONG> are:<PRE> while (condition) command for (command1; (condition); command2) command3</PRE><I>while </I> repeats <I> command </I> as long as <I> condition </I>is true. There must be something in <I> command </I> which changes<I> condition </I> false sooner or later, otherwise awk will keep repeating forever. If the <I> condition</I> is false when entering thisinstruction, <I> command </I> will not be executed at all.<I>for </I> statements sets initial values with <I> command1 </I>,terminating condition with <I> condition </I> and changes the valueswith <I> command2 </I>. <I> command3 </I> is executed as many times as<I> command1, condition </I> and <I> command2 </I> allow. <I> command3 </I>should not change any values set in <I> command1 </I> or <I> command2 </I>or tested in <I> condition </I>. Please remember the brackets in for-statement. With <I> while </I> statement you can not set any initial values, with <I> for </I> statement you can. You use <I> while </I> in indefinite cases and <I> for </I> when you know how many times thecommand shall be repeated. If that sounds complicated, see the examples:<PRE> while ( i < 10 ) i = i + 1 for ( i=0; ( i < 10 ); i=i+1 ) print i </PRE></P><P>Please refer to C-language books for more information on thesecommands. The commands are not exactly similar, but differencesare small.Good reference to C-language is P. J. Plauger & Jim Brodie: Standard C, ISBN 1-55615-158-6.</P><P>Typical command line to <STRONG> run awk </STRONG> is:<PRE> awk -f program.AWK inputfile >outputfile</PRE>There are several other ways to run awk programs, please seeawk manual page or awk help.</P><P>In case awk can not handle a task, there is a similar, but more advanced software tool called PERL (www.phlab.missouri.edu/perl/perlcourse.html). Most programming languages are much more flexible than awk and perl, but writing the program is also more difficult.</P><P>When you need more information on awk, Unix manual page forawk is a good source. Book <CITE> Programming language awk </CITE>is written by the persons who created awk. There is also alively newsgroup <CITE> comp.lang.awk </CITE>.There are some awk sample programs:<UL><LI> <A HREF="../prg/indent-to-html.html"> a program to change indented indexes into html </A><LI> <A HREF="../prg/wc-awk.html"> a word counting program like Unix wc </A><LI> <A HREF="../prg/cgw-awk.html"> a program to count given words </A></UL></P><P><B>Awk</B> is available for most Windows versions startingfrom Win 3.1 up to Win XP. Please check these URLs:<PRE>http://www.cygwin.com/http://unxutils.sourceforge.net/http://www.rtr.com/Ready-to-Run_Software/windows_readypak.htmhttp://www.math.utah.edu/~beebe/gnu-on-windows.htmlhttp://www.winaxe.com/unix-utilities-for-windows.htmlhttp://www.softintegration.com/demos/chstandard/unix.htmlhttp://people.ucsc.edu/~kannan/user/djgpp/http://www.data-miner.com/READMEdmsk.htmlhttp://ansys.net/ansys/?mycat=toolshttp://web.mit.edu/tytso/www/nt-civilized.htmlhttp://www.freewarehome.com/System_Utilities/UNIX_t.htmlhttp://www.softwareonline.org/products.htmlhttp://www.cansyswest.com/free.htmhttp://www.brunningonline.net/simon/blog/archives/000338.htmlhttp://softwaredownloadcenter.com/RegNow/Utilities.htmlhttp://www.crack.com/http://www.c2000.com/software/software.htm</PRE></P><HR><P><CENTER><SMALL><A HREF="http://www.canberra.edu.au"> University of Canberra </A> |<A HREF="http://www.canberra.edu.au/~sam/home.html"> UC home page </A> |<A HREF="http://www.users.bigpond.com/smattila"> Telstra Bigpond home page</A> |<A HREF="http://www.canberra.edu.au/uc/educ/crie/ieej_home.html"> IE-ej </A> |<A HREF="http://www.canberra.edu.au/~sam/sam-links1.html"> page map </A></SMALL></CENTER></P><P><PRE></PRE></P><ADDRESS><A HREF="mailto:Sakari.Mattila@canberra.edu.au">Sakari.Mattila@canberra.edu.au </A></ADDRESS></BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -