Today is the 27, tomorrow is 28, one to the Chinese New Year, it is customary to say the lunar calendar, not to say the Gregorian calendar. These two days are very busy, and then a remake.
The Linux processing Text tool has three Musketeers, awk, grep, and SED, where awk is the most powerful and grep is often used. Say awk today and combine MySQL applications.
awk ' {[pattern] action} ' {Filenames}
There are many ways to use it, but A grammar summary can be written in two parts: pattern (condition) + action (processing action).
Pattern means that the content that awk looks for in the data is either a regular expression or a conditional judgment, and this option can be omitted.
Action refers to the command to execute after the content is matched to.
Typically, awk is treated as a unit of a file's behavior. awk processes the text by executing the corresponding command for each line that receives the file.
The process can be described with such pseudo-code
While (with the next line) { 1: reads the next line, assigns the next line to $0, each column assigns $1, $2... $N variable 2 : Process the line with the specified command}
Here's an example: there are 5 rows and 2 columns of data in the City.txt text
Cat010 021 022 0657 0371
Executing awk-related commands
[Email protected] ~]#awk '{print $}'City.txt #$0 represents all columns of a row BJ010SH021TJ022HZ0657ZZ0371[[Email protected]~]#awk '{print $}'City.txt #$1 represents the first column of a row Bjshtjhzzz[[email protected]~]#awk '{print $}'City.txt #$2 represents the second column of a row 01002102206570371
Looking at the second example, city.txt a vertical bar as a separator. To separate the data, use the-f parameter .
Cat| 010 | | 021 | | 022 | | 0657| | 0371| E
[[email protected] ~]# awk ' begin{fs= ' | '} {print "} ' city.txt #或 awk-f" | " ' {print $} ' city.txt
BJ
Sh
Tj
Hz
Zz
If there are multiple separators at the same time, the vertical bar, say hello, comma. The- f parameter can specify more than one .
[Email protected] ~]#Catcity.txt BJ|010?ASH|021?BTJ|022, CHZ|0657, DZZ|0371, E[[email protected]~]#awk-F'[|?,]' '{print $1,$2,$3}'city.txt #同时指定三个分隔参数BJ010ASH021BTJ022CHZ0657DZZ0371E
Reference article:
MySQL Analysis-awk+threads analysis status
The awk command of the Three Musketeers of Linux
Linux awk Command
AWK programming language
awk parsing MySQL Status