The Linux Report Generator tool awk
awk: Displayed after formatting based on input information
1.1. awk Basic Usage
awk [Options] ' script ' File1,file2,....
or awk [options] PATTERN [ACTION] ' file1,file2 ....
Example: awk ' {print '} ' test.txt
Options
-F: defining delimiters
Example awk-f: delimiter with a colon
1.2. awk built-in variables
(1) Record variables
FS: Set default split Word descriptor example awk-f: Colon as delimiter
RS: Set default line break
OFS: Set Default Output delimiter
ORS: Set default output line delimiter
(2) Data variables
The number of records (rows) processed by the Nr:awk command, if there are multiple files, the total number of records
The record (row) that the Fnr:awk is processing is the total number of records (rows) currently processed in this file
NF: The number of fields in the current record (row)
ARGV: Array, save the command line itself this string
Parameters of the Argc:awk command
The name of the file processed by the Filename:awk command
ENVIRON: An associative array of current shell environment variables and their values
1.3, the use of print format
Print ITEM1,ITEM2,...
Each item is separated by commas, and the output is separated by a space
1.4. printf
Unlike print, printf needs to specify the format
Use format: printf format,item1,item2 ...
The format indicator is preceded by%, followed by a character
%c: ASCII code for displaying characters
%d,%i: Decimal integer
%f: Show floating-point numbers
%s: Display string
Percent: show% itself
Format modifiers
N: Display width
-: Align Left
+: Display numeric symbols
Example: awk-f: ' {PRINTF '%-15s '%i\n,$1,$3} '/etc/passwd
1.5. Awk's operator
(1) Arithmetic operators
-X: Negative value
+x: Convert to Numeric
(2) Assignment operator
=
+=
%=
(3) Comparison operators
= =: Equal
! =: Unequal
(4) Logical relationship between expressions
&&
||
1.6. awk mode pattern
Use format: awk ' pattern{action} ' file1 file2
Common pattern types
1 Regular expressions,/pattern/
2 expressions, where ~ and!~ indicate matching and mismatches
3 Specifies the match range, formatted as PATTERN1,PATTERN2, from match pattern1 start to match to Pattern2 end
4 Begin/end: Special mode, run only once or before the end of the awk command execution
5 empty (Null mode): matches any row
The Common Acton
An expression
Control statements for and so on
if (condition) (then ...) Else{[...]}
while (condition) {Statement 1, statement 2 ...}
do{Statement 1, Statement 2 ....} while (condition)
for (condition) {Statement 1, statement 2 ...}
switch (expression) {case-value or regular-expression: statement 1...default: statement 1 ...}
Compound statement
Input and OUTPUT statements
Break and continue are often used in loops or case statements
Next: End the processing of the bank's text in advance and proceed to the next line
Example: Display a user whose ID is odd
Awk-f: ' {if ($3%2==0) next;print $1,$3} '/etc/passwd
1.7. Using Arrays in awk
array[array subscript or index]
Netstat-ant | awk '/^tcp/{++s[$NF]} END {for (a in S) print A, s[a]} '
Each occurrence of a row that is matched to the/^tcp/pattern, the array s[$NF] adds 1,nf to the last field of the row that is currently matched to,
This is the index of the elements of the array s with its value;
1.8. Awk built-in functions
Split: Split function
How to use Split (String,a,pattern): Splits a string by pattern and stores it as a[1],a[2]
Length ([string])
Returns the number of characters in string strings;
SUBSTR (String, start [, length])
Takes a substring of string, starting with start, taking length; start counting from 1;
System (Command)
Executes the system command and returns the result to the awk command
Systime ()
Fetch System Current Time
ToLower (String)
Convert all letters in a string to lowercase
ToUpper (s)
Convert all letters in s to uppercase
XI. user-defined functions
The custom function uses the function keyword. The format is as follows:
function f_name ([variable]) defining custom functions
{
Statements
}
F_name: Using a custom function
The function can also return a value using the return statement in the form "return value".
This article is from the "surgery industry has specialized" blog, please be sure to keep this source http://fuvip.blog.51cto.com/9276123/1983135
The Linux Report Generator tool awk