Changes

Jump to: navigation, search

Tutorial11: Sed & Awk Utilities

60 bytes added, 10:12, 4 March 2021
Using the awk Utility
* The awk command reads all lines in the input file and will be exposed to the expression (contained within quotes) for processing.
* The expression (contained in quotes) represents selection criteria, and action to execute contained within braces '''{}'''
* If selection criteria is matched, then action (between braces) is executed
* You can use a regular expression, enclosed within slashes, as a pattern. For example: '''/pattern/'''
* The ~ operator tests whether a field or variable matches a regular expression. For example: '''$1 ~ /^[0-9]/'''
* The '''!~ ''' operator tests for no match. For example: '''$2 !~ /line/'''
* You can perform both numeric and string comparisons using relational operators ( '''>''' , '''>=''' , '''<''' , '''<=''' , '''==''' , '''!=''' ).
* You can combine any of the patterns using the Boolean operators '''||''' (OR) and '''&&''' (AND).
* The '''print''' command can be used to display text (fields).
* You can use parameters which represent fields within records (lines) within the expression of the awk utility.
* The parameter '''$0 ''' represents all of the fields contained in the record (line).* The parameters '''$1''', '''$2''','''$3 ''' '''$9 ''' represent the first, second and third to the 9th fields contained within the record. * Parameters greater than nine requires the value of the parameter to be placed within braces (for example: '''${10}''','''${11}''','''${12}''', etc.)
* There are built-in variables that can be used in the awk expression (for example: '''NR''', '''NF''', '''FILENAME''', etc.)
* You can use the '''-F''' option with the awk command to specify the field delimiter.
13,420
edits

Navigation menu