AWK-is a programming language tool used to process text.
Original Meaning: Aho, Weinberger, Kernighan
Definition: three creators, Aho, Weinberger, and Kernighan
AWK is similar to the shell programming language in many aspects, although AWK has its own syntax. When AWK was initially created, it was used for text processing, and the language was based on executing a series of commands as long as there was a pattern match in the input data. This utility scans each row of the file to find the pattern that matches the content given in the command line. If the Matching content is found, perform the next programming step. If no matching content is found, process the next row.
One of the reasons why awk has become a good programming language is the use of built-in functions. awk defines and supports a series of built-in functions, makes the functions provided by awk more comprehensive and powerful.
Common Syntax:
Awk [options] 'commands' testfiles
Options
-F defines the field separator. The default Delimiter is a consecutive space or tab.
In the order of $1, $2, and $3, it indicates different fields in each column separated by an interval symbol in each line of files.
-V defines changes and assigns values. You can also use the following method to introduce variables from shell variables.
Example:
[Root @ localhost test] # awk-F ":" '{print $1, $7}' testfile
Use a colon as the separator to print 1st and 7th Fields
[Root @ localhost test] # awk-F "[:/]" '{print $1, $7}' testfile
Print 1st 7th fields with a colon or slash/as the Separator
[Root @ localhost test] # awk-F ":/" '{print $1, $7}' testfile
Use the colon along with the slash ":/" as the Separator
-----------------------------------------------------------
Command
Read pre-processing
1. Pre-read BEGIN {awk_cmd1; awk_cmd2 ;}
2. Line Processing: Addressing command
Addressing method: regular expressions, variables, comparisons, relational operations
Regular Expressions need to be wrapped in //
Common Regular Expressions:
^ Beginning and end of the row $
Any single character except line breaks
* Zero or multiple leading characters
. * All characters
[] Any character in the character group
[^] Returns the inverse of each character in the character group (not matching each character in the character group)
^ [^] Rows starting with a character in a non-character group
[A-z] lowercase letters
[A-Z] capital letters
[A-Z] lowercase and uppercase letters
[0-9] Number
\ <Word header, separated by spaces or special characters. Continuous strings are treated as words
\> End of a word
Extended Regular Expressions
? Zero or one leading character
+ One or more leading characters
Abc | def abc or def
A (bc | de) f abcf or adef
X \ {m \} x appears m times
X \ {m, \} x appears m times at most (at least m times)
X \ {m, n \} x appears m to n times
NR variable addressing, NR indicates the number of lines read by AWK
FNR indicates the number of rows in the file where the row is read.
Logical operations-operations can be performed by directly referencing a domain
==>=<=! => <~ !~
For example: # awk 'nr = 1 {print} '/etc/passwd
Root: x: 0: 0: root:/bin/bash
Command example: {print $1, $2}
3. Post-read Processing
END {awk_cmd1; awk_cmd2 ;}
---------------------------------------------------------------
AWk Variables
Total number of rows processed by NR AWK
Number of lines of the current file processed by fnr awk, not all
The FS field delimiter. The default Delimiter is a consecutive space or tab. You can use multiple symbols as the delimiter, or use-F [:/] to specify the delimiter in options.
The default delimiter of OFS output characters is space.
For example: # awk-F: 'ofs = "***" {print $1, $2} '/etc/passwd
Root *** x
Number of fields in the row currently read by NF
The delimiter of the ORS output line. The default Delimiter is line feed.
# Awk-F: 'ors = "***" {print $1, $2} '/etc/passwd
Root x *** bin x ***
Current FILENAME file name
For more details, please continue to read the highlights on the next page:
Common sed and awk Functions
Common grep \ awk \ sed syntax in shell programming in Linux
Shell programming in Linux-awk Programming
Awk
Linux awk command usage