awk: Report Generator, formatted text output
Awk:aho, Weinberger, Kernighan---New AWK, Nawk
GNU awk, Gawk
Gawk-pattern scanning and processing language
Basic usage: gawk [options] ' program ' FILE ...
Program:pattern{action statements}
The statements are separated by semicolons
Print, printf
Options:
-F: Indicates the field delimiter to use when entering
-V var=value: Custom variable
1. Print
Print item1, item2, ...
Points:
(1) Comma delimiter
(2) Each item of the output can be a string, or it can be a numeric value; An expression of a field, variable, or awk of the current record
(3) If item is omitted, it is equivalent to print $
2. Variables
2.1 Built-in variables
Fs:input field seperator, default to white space characters
Ofs:output field seperator, default to white space characters
Rs:input record seperator, line break at input
Ors:output record seperator, line break at output
Nf:number of field, number of fields
{print NF}, {print $NF}
Nr:number of record, number of rows
FNR: Each file is counted separately; number of rows
FileName: Current file name
ARGC: Number of command line arguments
ARGV: An array that holds the arguments given by the command line
2.2 Custom variables
(1)-V Var=value
Variable name Distinguishing character case
(2) directly defined in program
3. printf command
Formatted output: printf format, item1, ITEM2, ...
(1) format must give the
(2) does not wrap automatically, you need to explicitly give the line-break control, \ n
(3) in format, you need to specify a format symbol for each subsequent item, respectively.
Format characters:
%c: ASCII code for displaying characters
%d,%i: Displays decimal integers
%e,%e: Numerical display of scientific counting method
%f: Displayed as a floating-point number
%g,%g: Displaying values in scientific notation or floating-point form
%s: Display string
%u: unsigned integer
Percent: show% itself
Modifier:
#[.#]: The width of the first digital control display; The second # indicates the precision after the decimal point
%3.1f
-: Align Left
+: Display symbols for numeric values
4. Operator
Arithmetic operators:
X+y, X-y, x*y, x/y, X^y, x%y
-X
+x: Convert to Numeric
String operator: unsigned operator, string connection
Assignment operators:
=, +=, -=, *=, /=, %=, ^=
++, --
Comparison operators:
>=, <, <=,! =, = =
Pattern-matching characters:
~: whether match
!~: does not match
Logical operators:
&&
||
!
Function call:
Function_name (ARGU1, ARGU2, ...)
Conditional expression:
Selector?if-true-expression:if-false-expression
# awk-f: ' {$3>=1000?usertype= ' Common User ': usertype= "Sysadmin or Sysuser";p rintf "%15s:%-s\n", $1,usertype} '/etc/ passwd
5. PATTERN
(1) Empty: null mode, matching each row
(2)/regular expression/: Handles only the rows that can be matched to the pattern here
(3) Relational expression: relationship expressions; The result is "true" has "false"; the result is "true" will be processed
True: The result is a non-0 value, non-empty string
(4) Line ranges: Range
startline,endline:/pat1/,/pat2/
Note: Formats that give numbers directly are not supported
~]# awk-f: ' (nr>=2&&nr<=10) {print '} '/etc/passwd
(5) Begin/end mode
begin{}: Executes only once before beginning the processing of text in a file
end{}: Only once after text processing is complete
6. Commonly used action
(1) Expressions
(2) Control statements:if, while etc.
(3) Compound statements: Combined statement
(4) Input statements
(5) Output statements
7. Control statements
if (condition) {statments}
if (condition) {statments} else {statements}
while (Conditon) {statments}
Do {statements} while (condition)
for (EXPR1;EXPR2;EXPR3) {statements}
Break
Continue
Delete Array[index]
Delete array
Exit
{statements}
8. Array
Associative array: array[index-expression]
Index-expression:
(1) You can use any string; string to use double quotation marks
(2) If an array element does not exist beforehand, when referenced, awk automatically creates this element and initializes its value to "empty string"
To determine whether an element exists in an array, use the "index in array" format to
weekdays[mon]= "Monday"
To iterate through each element in the array, use the For loop
for (var in array) {For-body}
~]# awk ' begin{weekdays["Mon"]= "Monday" weekdays["Tue"]= "Tuesday"; for (I in weekdays) {print Weekdays[i]}} '
Note:var iterates through each index of the array
9. Functions
9.1 Built-in functions
Numerical Processing:
RAND (): Returns a random number between 0 and 1
String processing:
Length ([s]): Returns the length of the specified string
Sub (r,s,[t]): Finds matching content in the character represented by T in the pattern represented by R and replaces it with the content represented by S for the first time
Gsub (R,s,[t]): Finds matches in the character represented by T in the pattern represented by R and replaces all occurrences with the content represented by s
Split (S,a[,r]): Cuts the character s with the R delimiter and saves the resulting cut to the array represented by a
~]# Netstat-tan | awk '/^tcp\>/{split ($5,ip, ":"); Count[ip[1]]++}end{for (i in count) {print I,count[i]}} '
9.2 Custom Functions
This article is from the "Ricky Technology Blog" blog, make sure to keep this source http://r1cky.blog.51cto.com/10646564/1773865
Linux text The Three Musketeers of awk