Awk: Difference between revisions
Jump to navigation
Jump to search
Line 14: | Line 14: | ||
=Overview= | =Overview= | ||
<tt>awk</tt> | <tt>awk</tt> handles a stream of text as a sequence of '''records'''. The default record separator is the new line, so by default each line is handled as a record. Each record is broken up into a sequence of '''fields'''. By default, the field separator is white space. An <tt>awk</tt> program consists in '''condition-action''' statements, that are applied to the records, as they are fed into <tt>awk</tt>. Each record is scanned for the condition, which can be a pattern, among other things, and for each condition that matches, the associated action is executed. | ||
awk '<''program''>' <''file''> | |||
The '''program''' is a succession of: | |||
condition { action } | |||
Example: | |||
{{{ | |||
awk '{print $1}' ./sample.txt | |||
}}} | |||
For the above, the condition matches all records and the action prints out the first field. | |||
{{{ | |||
awk '{print $1}' | |||
}}} | |||
=Recipes= | =Recipes= |
Revision as of 22:36, 14 June 2018
External
- http://en.wikipedia.org/wiki/Awk
- Awk by Example
Internal
Overview
awk handles a stream of text as a sequence of records. The default record separator is the new line, so by default each line is handled as a record. Each record is broken up into a sequence of fields. By default, the field separator is white space. An awk program consists in condition-action statements, that are applied to the records, as they are fed into awk. Each record is scanned for the condition, which can be a pattern, among other things, and for each condition that matches, the associated action is executed.
awk '<program>' <file>
The program is a succession of:
condition { action }
Example:
{{{
awk '{print $1}' ./sample.txt
}}}
For the above, the condition matches all records and the action prints out the first field.
{{{
awk '{print $1}'
}}}