awk 一种编程语言
Syntax format: awk "option" ' Pattern{action} ' "File" ... awk [option] ' condition {action} ' [file] ...
Note: There must be at least one space between each element in the awk command and in subsequent options and files
Option Description:
parameter Options |
Explanatory notes |
-F |
Specify character characters delimiter |
-V |
Define or modify a variable inside the awk |
$ |
Fetch, reference |
$ |
First column |
$ |
Entire row |
$NF |
Last line |
$ (NF-1) |
Penultimate line |
Rs |
Delimiter for each record read in (similar to line break \ n) |
Nr |
Line number, number of records |
ORS |
Delimiter for output time |
Relational operators
operator |
meaning |
Example |
< |
Less than |
X < y |
<= |
Less than or equal |
X <= y |
== |
Equals |
x = = y |
!= |
Not equal to |
X! = y |
>= |
Less than |
X < y |
< |
Greater than or equal |
X >= y |
> |
Greater than |
X > Y |
~ |
Match Regular expression |
x~/y/ |
!~ |
does not match the regular expression |
X!~y |
Case one:
Introduction to the AWK environment
Case TWO:
awk Execution Process:
- awk reads the first line of content
- to determine whether the conditions in the pattern are met (for example: nr>=2)
A. If the match executes the corresponding action (e.g. {print $})
B. If the condition is not matched, continue reading the next line li> continue to the next line
- Repeat procedure 1-3, know the last line read (Eof:end of file)
Simple description of the record:
- How many steps does an elephant put in a refrigerator? Open the refrigerator door, put the elephant in, close the refrigerator door.
- to debug your awk command with NR, NF, $ number.
- NR holds the number of each record (line number) when the new line is read automatically +1
- RS is the delimiter for the record, and a simple understanding is that you can specify the end flag for each record.
- use RS instead of \ n
- rs action means the end of a record
- FS marks the end of each zone
AWK basic structure
awk begin{coms}/pattern/{ COMs} end{coms}
Start the module,/Who/{to do}, End module
- begin and end modules can only have one, begin{}begin{} or end{}end{}
- who does the module? can have multiple (for example: Nr==2{print $ nr==5{print)
Case three:
parameter Rs,nr example
Description: When RS is null, print the output record number in the line hand and print out each row The contents of the
Regular expression Simple example
Note: $1~ indicates a match to the first column (the first region), o{1,2} indicates that the condition has one or two o,{print nr,$1,nf} for printing line numbers, first column, last column
Case FOUR:
Use awk to remove the corresponding port number,
The number of empty lines in the statistics file
New file, with a space delimiter, area 3 greater than 15, how many lines
Use AWK implements a value of 1 and 100
Linux common Commands--awk