awk Flashcards
awk definition
pattern matching, record manipulation, programming.
awk processes files where the content is regarded as a set of records containing a consistent set of fields (like a table).
awk syntax
awk options ‘selection_criteria {action}’ file(s)
ex.
awk /Dairy/ {print $1,$3} gdbase
command Options SelectionCriteria Action Filename(s)
“For each matching ___, perform the given ___.”
Line
Action
If no action is specified, the default action is ___. In this case awk acts like ___.
Print
grep
The action print without field specifiers (such as $1 and $3) outputs
each line matched
If no selection criteria are specified, all lines are selected for processing. In this case awk acts like?
cat
awk’s key distinction is that specific pieces of a record (or line) can be ___ & ___ ?
selected and processed.
grep just selects the line, and does no processing.
awk is only possible if the input is organized in
some regular manner
awk ‘/Fish/ {print $1}’ gdbase
awk ‘/Fish/ {print $1}’ gdbase
- This prints the first field of lines that contain the string “Fish”.
- $ is the field operator – it indicates the field of the record to consider. For example, $1 is
interpreted as the value of the record’s first field.
Note: $0 is a convenience variable that represents the entire line.
awk ‘/Fish/ {print $1, $3, $2}’ gdbase
awk ‘/Fish/ {print $1, $3, $2}’ gdbase
- Prints fields 1, 3, 2 for each record, with a space between each. The comma indicates that a space will separate the fields.
- Note: awk views a string as a variable unless it is inside double quotes or inside the slashes / /.
- Strings inside / / are regular expression strings for pattern matching.
- For example, numbers are regarded as a string in this context:
awk ‘{print 1234 $1} gdb
Selecting Lines by Field Value
awk ‘$5 == ”y” {print $1, $3, $2}’ gdbase
Selecting Lines by Field Value
awk ‘$5 == ”y” {print $1, $3, $2}’ gdbase
- Lines where field 5 contains only the character y, are selected for processing.
- Processing here is print field 1 followed by field 3 then field 2
Use Other Relational Operators for Selecting Lines by Field Value
awk ‘$5 == ”y” && $3<4.00 {print $1, $3, $2}’ gdbase
Use Other Relational Operators for Selecting Lines by Field Value
awk ‘$5 == ”y” && $3<4.00 {print $1, $3, $2}’ gdbase
- Use && as logical AND and || as logical OR.
- Enclose expressions in brackets () to group them like in Java.
- The NOT operator is !
You can use these operators between target strings:
awk ‘/Dairy/||/Meat/ && $3<4.00 {print $1, $3, $2}’ gdbase
You can use these operators between target strings:
awk ‘/Dairy/||/Meat/ && $3<4.00 {print $1, $3, $2}’ gdbase
- This will print fields 1, 3 and 2 for lines that contain “Dairy” or lines that contain “Meat” where the 3rd field is less than 4.00.
accumulator variable
printing sum
Can Search a Field for a Target
awk ‘$3~/.89$/’ gdbase
Can Search a Field for a Target
awk ‘$3~/.89$/’ gdbase
- Use the ~ operator to search the 3rd field for the string .89 at the end of the field, and select those records for processing.