Mawk for Linux commands
Pattern scanning and text processing language
Syntax:
Mawk [-F value] [-v var = value] [--] 'gram text' [file...]
Mawk [-F value] [-v var = value] [-f program-file] [--] [file...]
Description:
Awk is a text data processing language, and mawk is an interpreter for this language.
The awk program consists of the pattern {action} sequence and function definition. Input data is divided into records one by one based on RS (Record seperator, default = '\ n'). Each Record is compared with pattern. If it matches, the corresponding action is executed.
Option:
-F
Set field seperator and FS. Based on this value, record is divided into field
-F
Awk program file
-V var = value
Set program variables
--
Indicates that the mawk Command Option description is complete.
The following describes the AWK language.
1. Program Structure
As mentioned above, the awk program consists of the pattern {action} sequence or function definition.
Where, pattern can be
BEGIN
END
Expression
Expression, expression
If action is omitted, print is implicitly executed. If pattern is omitted, the matching is correct.
The statement is ended Based on the line break and semicolon.
Use # to annotate
Control Flow includes
If (expr) statement
If (expr) statement else statement
While (expr) statement
Do statement while (expr)
For (opt_expr; opt_expr) statement
For (var in array) statement
Continue
Break
2. Data Type, conversion and Comparison
There are two numeric and string types. All numbers are represented and computed using floating point numbers.
3. Regular Expression
Expr ~ /R/
The regular expression is enclosed by a slash. If expr is subject to this regular expression, it is true. Otherwise, it is false. "! ~" Is used for disobedience. .
/R/{action} and $0 ~ /R/{action} is equivalent.
4. Records and Fields
Read a row at a time, that is, a Records, and divide it into Fields according to FS. $0 indicates the entire Records, $1, $2 ,..., $ NF represents the corresponding Field. The built-in variable NF is the total number of Fields, and the Fields value higher than NF is set to "".
NR and FNR increase by 1 each time. The meaning is described later.
Assigning values to the preceding built-in variables will change the related variables.
5. Expressions and operators
Most operators are the same as those in C. Special operators include in (array membership ),~ (!~, Matching), $ (field ).
6. Array
Awk provides A one-dimensional array that uses A [1] or A ["1"] to access elements.
Delete array [expr] deletes the corresponding element.
If (I, j) in A) print A [I, j]
7. built-in Variables
CONVFMT: the internal conversion format from a numeric value to a string. The default value is "%. 6g"
ENVIRON: environment variable array, var = value is stored as ENVIRON [var] = value
FILENAME: name of the current input file
FNR: the number of the current record in the FILENAME File
FS: field separator, which can be a regular expression
NF: Total number of fields in the current record
NR: record number in all input streams
OFMT: Format of the output value. The default value is "%. 6g"
OFS: field Separator Used for output. The default value is space.
ORS: Specifies the record delimiter for output. The default Delimiter is a line break.
RS: The record delimiter used for input. The default value is line feed.
SUBSEP: used to build multiple array subscripts. The default value is "\ 034"
8. built-in functions
(1) string processing functions
Gsub (r, s, t) gsub (r, s)
Replace the character matching r in the t variable with s. If t is not specified, the hidden character is $0. Finally, the number of replicas is returned.
Index (s, t)
Returns the position where t appears for the first time in s. Otherwise, 0 is returned. The first character subscript of s is 1.
Length (s)
Returns the length of s.
Match (s, r)
Returns the longest matching subscript of r in s, and returns 0 if no matching exists.
Split (s, A, r) split (s,)
Divides s into A according to r, and returns the number of fields. if r is not set, use FS
Sprintf (format, expr-list)
Construct a string based on the format
Sub (r, s, t) sub (r, s)
Replace Once
Substr (s, I, n) substr (s, I)
Returns the substring of s from I to n.
Tolower (s)
Toupper (s)
(2) numeric processing functions
Atan2 (y, x)
Cos (x)
Exp (x)
Int (x)
Log (x)
Rand ()
Sin (x)
Sqrt ()
Srand (expr) srand ()
9. Input and Output
Print and printf are output.
Print
Output $0
Print expr1, expr2,..., exprn
Printf format, expr-list
The input is getline.
Getline
Read a row to $0
Getline <file
Read $0 from file
Getline var
Read the next record to var
Getline var <file
Read the next record from file to var
Command | getline
Execute command and read a record from the pipeline to $0
Command | getline var
Execute command and read a record from the pipeline to var
When getline encounters end-of-file, 0 is returned,-1 is returned, and 1 is returned.
Close (expr)
Close the file or pipe associated with expr
Fflush (expr)
System (expr) executes expr and returns the status
10. User-Defined Functions
Syntax:
Function name (args) {statements}
Can contain return opt_expr
11. Split strings, records, and files
12. Multi-line records processing
13. Program Execution
The BEGIN command is executed first.
Then process each record one by one based on the intermediate pattern {action}
You can use
Next
Exit opt_expr
Change the execution sequence of the program at the pattern level, read the next record directly upon next, and continue execution after in. exit immediately calls the actions corresponding to END, and opt_expr is the exit code.
Last run END