awk Basic usage:
awk [Options] ' program ' file1 file2
program:pattern{ACTION STATEMENT} is made up of statements, and the statement delimiter is a semicolon
Action:print, printf
option:-F: Indicates the input field delimiter
-V var=value: variable Assignment
One,awk output command print
Print item1,item2, ...
Points:
(1) each item is separated by commas, and the output is separated by an output delimiter;
(2) each item of the output can be a string or numeric value, a field ($n) of the current record, a variable, or an expression of awk, and the value is implicitly converted to a character for output;
(3) If the item after print is omitted, it is equivalent to print$0; output "blank", using print "";
Second, the variable
1. Built-in variables
FS: Input delimiter, default is white space character
RS:The input recordseparator defaults to line breaks
Ofs:the output field separator, default to white space character
Ors:the Output Record separator, default to line break
NF: Number of fields
NR: Number of rows, uniform count of all files
FNR: Number of rows, each file Count
FileName: Current file name
ARGC: Number of command line arguments
ARGV: Array of command-line arguments
2. Custom Variables
-V var=value: variable name distinguishes character case
(1) variable can be defined in program
(2) defining variables with the-v option
Third,the printf command
format:printf formatitem1,item2
Important:1. Format is a must
2, does not wrap automatically, you need to explicitly give a row delimiter
3. Format needs to specify one for each subsequent item
format characters: All begin with % , followed by a character
%c: ASCII code for displaying characters
%d,%i: displaying decimal integers
%e,%e: Scientific notation displays values
%f: Displayed as a floating-point number
%s: String
%g,%g: Displaying values in scientific notation format or floating-point number format
%u: unsigned integer
Percent: Show % itself
Modifier:
#[.#]: First # display width, second # display precision For example:%3.2s
-: Indicates left alignment%-3d to left
+: Display numeric symbols
Four, the operator:
Arithmetic operators:
+,-,*,/,%
-X: Negative value
+x: convert to numeric
String manipulation: String connection
Assignment operators:
=,+=,-=,*=,/=,%=,^=,+ +,--
Comparison operators:
>,<,>=,<=, = =, ! =
Pattern match:~
logical operators:&& | |
Conditional expression:? :
function Call:function_name (argu1,argu2,....)
Example:awk-f: ' {$3>=500?usertype= ' common user ': usertype= "Sysuser oradmin";p rintf "%20s:%-s\n", $1,usertype} ' /etc/passwd
Five, PATTERN
1./regularexpression/: Only the rows that are matched to the /regular expression/ are processed
2,relationalexpression: The relationship expression, there is true and false, generally speaking, not 0 or non-empty string is true, otherwise false
3,line ranges: range, similar to sed or vim address bound method
4.gegin/end: Executes only once before or after awk runs the program
5,empty: null mode, matching any row
Six, the commonly used acion
1.expression
2.Control Statements
3,compound statements
4.Input Statements
5.Output Statements
Seven, control statements
if (condition) statement [Else statement]
while (condition) statement
Do Statementwhile (condition)
for (EXPR1;EXPR2; expr3) statement
for (var in array) statement
Break
Continue
Delete Array[index]
Delete array
Exit [expression]
{statements}
7.1 If-else
Syntax:if (condition) statement [Else statement]
if (condition) {statements;} [Else {statements;}]
# awk-f: ' {if ($3>=500) print $, "is a common user."} '/etc/passwd
# awk-f: ' {if ($3>=500) {print $, ' is a common user. '} else {print $, "Isa system user or admin."}} ' /etc/passwd
# awk ' {if (nf>6) print NF, $} '/etc/inittab
usage: make conditional judgments on the fields in the entire row or row that awk obtains;
7.2 While Loop
syntax:while (condition) statement
while (condition) {statements}
The condition is true when the loop is made until the false exit;
Usage: Typically used for looping between fields in the current row;
# awk ' {i=1;while (I<=NF) {printf "%20s:%d\n", $i, Length ($i); i++}} '/etc/inittab
# awk ' {i=1;while (I<=NF) {if (length ($i) >5) {printf "%20s:%d\n", $i, Length ($i);} i++} '/etc/inittab
7.3 Do-while Cycle
syntax:do statement while (condition)
do {do-while-body} while (condition)
Meaning: At least one cycle body is executed;
7.4 for Loop
syntax:for (expr1; expr2; expr3) statement
for (EXPR1;EXPR2; expr3) {statements}
for (varaiable assignment; condition; iteration process) {For-body}
# awk ' {for (i=1;i<=nf;i++) {printf "%s:%d\n", $i, Length ($i)}} '/etc/inittab
The for Loop has a dedicated traversal array element in awk:
syntax:for (var in array) {For-body}
7.5 Switch
syntax:switch (expression) {case VALUE or/regexp/: statement; ...; DEFAULT:STATEMENTN}
7.6 Break and continue
Break [n]: exit current Loop
Continue: End this cycle ahead of time and go straight to the turn cycle
7.7 Next
End the processing of the bank in advance and proceed to the next line
# awk-f: ' {if ($3%2!=0) next;print $1,$3} '/etc/passwd
Eight, Array
associative array:array[index-expression]
Index-expression:
Any string can be used;
if an array element does not exist beforehand, when referenced,awk automatically creates the element and initializes its value to an empty string;
therefore, to determine whether an array exists for an element, use "indexin array";
a[mon]= "Monday"
Print A[mon]
to iterate through each element in the group, use:for (var in array) {for body}
Note:var iterates through each index of the array,print Array[var]
Example: Count the occurrences of each word in each row
# awk ' {for (i=1;i<=nf;i++) {count[$i]++}}end{for (J in count) {print j,count[j]}} ' Awk.txt
# Ss-tan | awk '!/^state/{state[$1]++}end{for (i in state) {print I,state[i]}} '
# Netstat-tan | awk '/^tcp/{state[$NF]++}end{for (i in state) {print I,state[i]}} '
For example: Statistics httpd access logs, the number of occurrences of each IP;
# awk ' {ip[$1]++}end{for (i in IP) {printi,ip[i]}} '/var/log/httpd/access_log
Nine, function
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 the match in the T string in the pattern represented by R, substituting the string represented by the same s for the first occurrence ;
Sub (ab,ab,$0)
Gsub (R, S [, T]): finds the match in the T string in the pattern represented by R , replacing all occurrences with the string represented by S;
Split (S, a [, R]): cuts the string s with the R delimiter, and saves the result of the cut to a representation array;
# netstat-tan| awk '/^tcp/{len=split ($5,client, ":"); ip[client[len-1]]++}end{for (iin IP) {print i,ip[i]}} '
SUBSTR (S, I [, N]): takes a substring from the string represented by s, starting with I, taking n characters;
Functions of the time class:
Systime (): take time stamp;
Bit arithmetic functions:
and (V1,VA2):
9.2 Custom Functions
Functionf_name (P,Q)
{
...
}
This article from the "source clean is clear, the shape of the end of the shadow straight" blog, please be sure to keep this source http://zhouxt.blog.51cto.com/8743763/1641571
Linux basic awk simple to use