Awk: Difference between revisions
No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
<code>awk <i>pattern</i> {<i>action</i>}</code> | <code>awk <i>pattern</i> {<i>action</i>}</code> | ||
'''Example:''' | |||
<code>example here, if I can figure it out...</code> | |||
== Structure == | == Structure == | ||
<code>BEGIN</code> The begin block runs at the start of the awk program, and runs only once. | '''BEGIN''' | ||
<code>BEGIN</code> The begin block runs at the start of the awk program, and runs only once. The body block is optional. | |||
'''BODY''' | |||
<code>/pattern/ {awk-commands}</code> The body block runs on every line of text (this can be restricted with patterns.) There is no keyword for this block. | |||
END | |||
<code>END {awk-commands}</code> The END command runs once at the end of the program. | |||
Line 13: | Line 28: | ||
<code> \ </code> The \ tells awk that a special character follows the slash, and to interpret it differently than regular text | <code> \ </code> The \ tells awk that a special character follows the slash, and to interpret it differently than regular text | ||
Example: | |||
Example: <code> \t </code> means a tab character | <code> \t </code> means a tab character | ||
<code> $ </code> field (column) reference | <code> $ </code> field (column) reference | ||
Example: | |||
<code>$2</code> tells awk to do something to the 2nd field (column) of the input. (Note that awk does not interpret variables inside strings.) | |||
<code> ' </code> (single-quote) Specify awk commands inside single quotes at the command line. | |||
Example: | |||
<code>awk '{print}' file.txt</code> prints the contents of file.txt to the screen | |||
<code></code> | |||
<code></code> | <code></code> |
Revision as of 16:54, 30 December 2016
awk parses reports, among other text manipulation
Usage:
awk pattern {action}
Example:
example here, if I can figure it out...
Structure
BEGIN
BEGIN
The begin block runs at the start of the awk program, and runs only once. The body block is optional.
BODY
/pattern/ {awk-commands}
The body block runs on every line of text (this can be restricted with patterns.) There is no keyword for this block.
END
END {awk-commands}
The END command runs once at the end of the program.
Operators
\
The \ tells awk that a special character follows the slash, and to interpret it differently than regular text
Example:
\t
means a tab character
$
field (column) reference
Example:
$2
tells awk to do something to the 2nd field (column) of the input. (Note that awk does not interpret variables inside strings.)
'
(single-quote) Specify awk commands inside single quotes at the command line.
Example:
awk '{print}' file.txt
prints the contents of file.txt to the screen