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
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
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
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
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
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
Defined variables
NF =>
NR =>
FS =>
OFS =>
ORS =>
NF => number of fields in a record
NR => number of records (lines) in the file
FS => Field Separator
OFS => Output Field Separator
ORS => Output Record Separator
NF (Defined variables)
number of fields in a record
NR
NR => number of records (lines) in the file