Use of awk:
Awk:aho,kernighan,weinberger
New Awk:nawk
Gawk,awk
# awk [option] ' script ' file ...
# awk [option] ' Pattern {action} ' file ...
The output of awk:
First, print
Print ITEM1,ITEM2,...
Points:
1. Separate items using ', ', separated by the specified delimiter when output, and the default input and output delimiter is a space
2. The output item can be a string or numeric value, the current record field ($), a variable, or an expression of awk, and the value will be converted to a string before output
The item in the back of the 3.print command can be omitted, which is equivalent to {print $}; If you want to output a blank, {print "}
Example:
Print the specified field
[[email protected] tmp]# awk ' {print $} ' test.txt
This
This
[[email protected] tmp]# awk ' {print $1,$2} ' test.txt
This is
This is
Print all the fields
[[email protected] tmp]# awk ' {print $} ' test.txt
This is a like.
This was love;
Add a string to the printed field, using ' # ' as the output delimiter
[[email protected] tmp]# awk ' begin{ofs= "#"}{print $, "Hello", $2,$3} ' test.txt
This#hello#is#like.
This#hello#is#love;
Processing a string
[[email protected] tmp]# awk ' begin{print ' line one\nline two\nline three '} '
Line One
Line
Line Three
Specifying an input delimiter and an output delimiter
[Email protected] tmp]# head-1/etc/passwd | Awk-f: ' begin{ofs= ': '}{print} '
Root:x:0:0:root:/root:/bin/bash
[Email protected] tmp]# head-1/etc/passwd | awk ' begin{fs= ': "; ofs=" # "}{print $1,$3} '
Root#0
Ii. variables of awk
1. Built-in variables _ record variables:
FS: Enter a delimiter for text, default to a space
OFS: Output delimiter, default space
RS: Enter line break for text: \ n
ORS: Output line break
2. Built-in variable _ data variable:
The number of records processed by the 1.nr:awk command, the total number of records
2.FNR: The number of records for the current file processed by the command, relative to the currently processed file
3.NF: Number of fields currently working with lines of text
4.ARGV: Array, save the command line itself this string, such as awk ' {print $} ' a.txt, argv[0] Save Awk,argv[1] Save A.txt
The number of arguments to the 5.argc:awk command
The name of the file processed by the 6.filename:awk command
7.ENVIRON: An associative array of current shell environment variables and their values
[[email protected] tmp]# awk ' begin{print environ["PATH"]} '
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
3. User-defined variables:
Allow users to customize variables, named variable rules can only use letters, numbers, and underscores, but cannot start with a number
[[email protected] tmp]# awk-v test= "HELLP awk" ' BEGIN {print test} '
HELLP awk
[[email protected] tmp]# awk ' begin{test= "Hello awk";p rint test} '
Hello awk
Third, printf
Format of the printf command:
printf format1format2...,item1,item2,...
Points:
1. The biggest difference from print is that the format must be developed
2.FORMAT specifies the output format for each subsequent item
3.printf statements do not automatically print line breaks; \ n
The format indicator is preceded by a%, followed by a character:
%c: ASCII code for displaying characters
%d,%i: Decimal integer
%e,%e: Scientific notation Displays values
%f: Show floating-point numbers
%g,%g: Displaying values in scientific notation or floating-point format
%s: Display string
%u: unsigned integer
Percent:% of itself
Modifier: Used to adjust above formatting, modifier is after '% ', before format character
N: Display length
-: Left-aligned, default right-justified
+: Display numeric symbols
awk ' {printf '%-5s%-3s%-2s\n ', $1,$2,$3} ' test.txt
Iv. output Redirection
{Print Items > output_file}
{Print Items >> output_file}
Special File Descriptor:
/dev/stdin: Standard input
/dev/stdout: Standard Output
/dev/stderr: Error Output
/dev/fd/n: A specific file descriptor, such as/dev/stdin, is equivalent to/dev/fd/0
Awk-f: ' {printf '%-15s%-i\n ', $1,$3 > '/dev/stdout '} '/etc/passwd
V. the operator of awk:
1. Arithmetic operators:
-X: Negative value
+x: Convert to Numeric
X^y,x**y:x y-Square
X*y: Multiplication
X/Y: Division
X+y
X-y
X%y
2. String operators:
Only one, and does not need to show that the connection used for the string
3. Assignment operators:
=
+ =: X + = y--and X = X+y
-= :
*= :
/= :
%= :
^=,**= :
+ +: X---x=x+1
-- :
4. Boolean value:
In awk, any non-0 value or non-empty string is true and the reverse is false
5. Comparison operators:
X is a string, Y is a pattern, matches a string with a pattern
X < y
X <= y
X > Y
X >= y
x = = y
X! = y
x ~ y: Returns True if X can be matched by Y, otherwise false
X!~ y
6. Logical relationship between expressions:
&&
||
7. Three mesh operator:
A>b?x:y---if A>b is Trun, returns x, otherwise returns y
8. Function calls:
Function_name (Paremeter1,parameter2,...)
Vi. Model of awk:
awk ' program ' Input_file1 input_file2 ...
The program is:
Pattern {Action}
Pattern {Action}
...
1. Common Pattern Types:
1.Regexp: Regular expression, formatted as/regexp/
Awk-f: '/^r/{print $ '/etc/passwd
2.express: expression that satisfies the condition when its value is not 0 or non-null characters, such as $7 ~ "bash$"
Awk-f: ' $7~ ' bash$ ' {print $1,$7} '/etc/passwd
3.range: Specify a matching range, format pat1,pat2
Awk-f: '/^r/,/^m/{print $1,$7} '/etc/passwd
4.begin/end: Special mode, allows only one time before the awk command is allowed to execute or before the end
Awk-f: ' begin{printf "%-20s%-50s\n", "USERNAME", "Def_shell"}{printf "%-20s%-50s\n", $1,$7}end{print "END of Report"} '/ etc/passwd
5. Empty mode, matching all input lines
2. Common action:
1. Expression
2. Control statements
3. Conforming statement
4. Input
5. Output
If-else:
Syntax: if (Express) {then-body} else {else-body}
Example:
Awk-f: ' {if ($1== "root") print $, "Admin"; else print $, "User"} '/etc/passwd
Awk-f: ' {if ($1== "root") {print $, ' Admin '} else {print $, ' User '} ' '/etc/passwd
[Email protected] tmp]# awk-f: ' BEGIN {sum=0}{if ($3>500) sum++}end{print sum} '/etc/passwd
1
[[email protected] tmp]# awk-f:-V sum=0 ' {if ($3>500) {sum++}}end{print sum} '/etc/passwd
1
While: Looping through each field in a row
Syntax: while (Express) {{while_body1};{ While_body2} ...}
Example:
Awk-f: ' {i=1;while (I<=NF) {if (length ($i) >=4) {print $i};{ i++}}} '/etc/passwd
Do-while:
Syntax: Do{while_body,...} while (Express)
Example:
Awk-f: ' {i=1;do{if ($i) >=4) {print $i};i++}while (i<=nf)} '/etc/passwd
For
Syntax: for (variable assignment;express;iteration process) {Body1,body2 ...}
Example:
Awk-f: ' {for (i=1;i<=nf;i++) {if (length ($i) >=4) {print $i}}} '/etc/passwd
Use an array in awk:
1. Arrays:
Array[index-express]
Index-express can use any string; it is important to note that if an array element does not exist in advance, awk automatically creates the element and initializes it to an empty string when it is referenced, so you need to use the index in array to determine whether an element exists in an array
To enumerate through each element of a group, you need to use the following special structure:
for (var in array) {Statement1,...}
Where Var is used to reference array subscripts, not element values
Example: Count the number of States currently connected to the host:
Netstat-tan | awk '/^tcp/{state[$NF]++}end{for (A in state) {print A,state[a]}} '
Use of awk