awk parsing MySQL Status

Source: Internet
Author: User
Tags processing text

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.

1. Syntax

 

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.

2.awk principle

  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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.