21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

102 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>/foo/ { } match foo, do nothing --- empty action/foo/match foo, print the record --- omitted actionThe following types of statements are supported in awk:ExpressionsCall functions or assign values to variables (see Chapter 5 [Expressions],page 75). Executing this kind of statement simply computes the value of theexpression. This is useful when the expression has side effects (see Section 5.7[Assignment Expressions], page 83).Control statementsSpecify the control flow of awk programs. The awk language gives you C-likeconstructs (if, for, while, and do) as well as a few special ones (see Section 6.4[Control Statements in Actions], page 102).Compound statementsConsist of one or more statements enclosed in curly braces. A compound statementis used in order to put several statements together in the body of an if,while, do, or for statement.Input statementsUse the getline command (see Section 3.8 [Explicit Input with getline],page 52). Also supplied in awk are the next statement (see Section 6.4.8 [Thenext Statement], page 108), and the nextfile statement (see Section 6.4.9[Using gawk’s nextfile Statement], page 109).Output statementsSuch as print and printf. See Chapter 4 [Printing Output], page 58.Deletion statementsFor deleting array elements. See Section 7.6 [The delete Statement], page 123.6.4 Control Statements in ActionsControl statements, such as if, while, and so on, control the flow of execution in awkprograms. Most of the control statements in awk are patterned on similar statements in C.All the control statements start with special keywords, such as if and while, to distinguishthem from simple expressions. Many control statements contain other statements.For example, the if statement contains another statement that may or may not be executed.The contained statement is called the body. To include more than one statementin the body, group them into a single compound statement with curly braces, separatingthem with newlines or semicolons.6.4.1 The if-else StatementThe if-else statement is awk’s decision-making statement. It looks like this:if (condition) then-body [else else-body]The condition is an expression that controls what the rest of the statement does. If thecondition is true, then-body is executed; otherwise, else-body is executed. The else partof the statement is optional. The condition is considered false if its value is zero or the nullstring; otherwise, the condition is true. Refer to the following:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!