Awk: Difference between revisions
From Jedisaber Wiki
No edit summary |
No edit summary |
||
| Line 7: | Line 7: | ||
'''Example:''' | '''Example:''' | ||
<code> | <code>awk '{print}' file.txt</code> prints all of file.txt | ||
<code>awk '{print $3 "\t" $5}' file.txt</code> prints just the third, and fifth columns of file.txt, separated by a tab. | |||
Revision as of 17:13, 30 December 2016
awk parses reports, among other text manipulation
Usage:
awk pattern {action}
Example:
awk '{print}' file.txt prints all of file.txt
awk '{print $3 "\t" $5}' file.txt prints just the third, and fifth columns of file.txt, separated by a tab.
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
