awk of linux command

awk is an awesome data manipulation tool. Compared with sed, which often acts on an entire line, awk tends to divide a line into several "fields" for processing. Therefore, awk is quite suitable for small data processing. Awk usually runs like this:

[root@www ~]# awk 'Condition Type 1{Action 1} Condition Type 2{Action 2}. . . ' filename

Awk is followed by two single quotes and curly brackets to set the desired processing action on the data. awk can process subsequent files, and can also read standardoutput from a signed command. But as mentioned earlier, awk mainly processes the data in the fields of each line, and the default field separator is the space bar or the [tab] key. For example, we can use last to get the data of the registrant, but I only want to get the IP of the account and the registrant, and the account and IP are separated by [tab]

[root@www ~]# last -n 5 | awk '{print $1 "\t" $3}' <==You can use last -n 5 to view the login data first, and then operate

root    192.168.1.100

root    192.168.1.100

root    192.168.1.100

dmtsai 192.168.1.100

root    Fri

The above are the most commonly used actions in awk. List the field data through the print function! Fields are separated by spaces or [tab]. The 5th row data is a bit odd; it's because of a data format issue. So when using awk, please confirm your data first. If it is continuous data, please do not include spaces or [tab], otherwise it will be misjudged as above.

In addition, each field in each line has a variable name, that is, variable names such as $1, $2, etc. In the above example, root is $1 because it is the first column, and 192.168.1.100 is the third column, so it is $3, and so on. There is also a variable $0, $0 represents the meaning of a whole row of data.

Therefore, in the above 5 lines, the entire awk processing flow is:

    1. Read in the first line, and fill in the first line of data into variables such as $0, $1, $2;

    2. According to the restriction of the condition type, judge whether the following action is required;

    3. Complete all actions and condition types;

    4. If there are subsequent "rows" of data, repeat the steps 1~3 above until all the data are read.

After such steps, you will know that awk is a processing unit for a row, and a field is the smallest processing unit. So how does awk know how many rows and columns the entire data has? This requires the help of awk's built-in variables

NF: total number of fields each row ($0) has

NR: Currently awk is dealing with "number of rows" of data

FS: the current separator, the default is space

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324844991&siteId=291194637