Bird Brother book awk tool learning

Source: Internet
Author: User

awk Tools

Awk is also a good data-processing tool. Awk tends to divide a line into several fields to deal with the processing of an entire line compared to SED. As a result, awk is quite suitable for small data processing. The usual mode of operation for awk is this:

[[email protected] opt] #awk ' condition type 1{action 1} condition type 2{action 2} ' filename

Awk follows two single quotation marks and braces {} to set the processing action you want to make to the data. QWK can process subsequent files or read the StandardOutput from the previous command. But as I said earlier, awk is primarily handling data within each row, and the default field delimiter is the SPACEBAR or the [tab] key. For example, we can use the last to read the data of the lander, and the result is as follows:

[[email protected] opt]# NL passwd | Sed-n ' $p '

PNS#This is a test

[Email protected] opt]# Last-n 5

Root pts/0 192.168.211.1 Tue Apr 09:14 still logged in

Reboot system boot 2.6.32-431.el6.x Tue APR 12 09:03-16:17 (1+07:13)

Root pts/0 192.168.211.1 Mon Apr 09:57-crash (23:05)

Reboot system boot 2.6.32-431.el6.x Mon Apr 11 09:56-16:17 (2+06:20)

Root pts/0 192.168.211.1 Mon Mar 21:47-crash (13+12:09)


Wtmp begins Sun Jan 24 00:22:20 2016

[Email protected] opt]# last-n 5| awk ' {print $ ' \ t ' $} '

Root192.168.211.1

RebootBoot

Root192.168.211.1

RebootBoot

Root192.168.211.1

WtmpSun

I only want to take out the account and the login information, and the account and IP are separated by [tab], it will become like:

[Email protected] opt]# last-n 5| grep root |awk ' {print $ \ t ' $ $} '

Root192.168.211.1

Root192.168.211.1

Root192.168.211.1

The most commonly used action by awk. List the field data by using the PRINT function! Fields are separated by a space or [tab] key. Because no matter what line to deal with, you have to deal with, so you do not need the restriction of the condition type! What we want is the first column and the third column, but why is the red font different from the others? This is because of a problem with the data format. So when using awk, check the data first, if it is continuous data, do not have a space or [tab], otherwise, the situation will occur

Also, as the above example knows, each field in each row has a variable name, which is the variable name of $ $. In the example above, root is $ $, because he is the first column! 192.168.211.1 is the third column, so it's $ $, and so on. And the variables, that is, he represents the meaning of a whole row of data. In the above example, the first line of "$" represents the "root" that

line. It follows that the whole process of awk is in the above five elements:

1, read into the first line, and the first row of data into the $0,$1,$2 variables;

2, according to the restrictions of the type of conditions, to determine whether the following actions are required;

3, finish all the action and the type of condition;

4, if there is a subsequent "line" of data, then repeat the above 1-3 steps until all the data is read out.

After this step, you will know that awk is a unit that behaves once, with a field being the smallest processing unit. So how does awk know that I have a few rows of this data? This would require the help of Awk's built-in variables,

NF total number of fields per row ($).

NR at present awk is dealing with the "number of rows" of data.

The current delimited character of FS, the default is the SPACEBAR.

List the accounts for each column (that is, $ $)

List the number of rows currently processed (that is, the NR variable in awk)

and explains how many fields are diverted (that is, the NF variable within awk)


Note that all subsequent actions of awk are enclosed in single quotes, since both single and double quotation marks must be paired, so if you want to remember the text part of the non-variable in the format of awk, which contains a section of the format that printf mentions, you need to use double quotes to define it. Because the single quotation mark is already a fixed usage of awk's commands.

[Email protected] opt]# last-n 5| grep root |awk ' {print $ \ t lines: "NR" \ t colums: "NF} '

Root lines:1 colums:10

Root lines:2 colums:10

Root lines:3 colums:10

The logical operators of awk

[email protected] opt]# Cat passwd | awk ' {fs= ': '} $3<10 {print ' \ t ' $ $} '

Root:x:0:0:root:/root:/bin/bash

Bin1

Daemon2

ADM3

LP4

Sync5

Shutdown6

Halt7

Mail8

How does the first line not show up correctly? This is because after we read the first line, those variables $1,$2 by default or by the SPACEBAR, so although we define the fs= ":", but only after the second line to take effect. What about that? We can pre-set the awk variable! Using the BEGIN keyword, do this

[email protected] opt]# Cat passwd | awk ' BEGIN {fs= ': '} $3<10 {print ' \ t ' $ $} '

Root0

Bin1

Daemon2

ADM3

LP4

Sync5

Shutdown6

Halt7

Mail8

In addition to the begin, there is the end! In addition, what if you want to use awk for "computational functions"?

[email protected] opt]# cat Pay.txt|awk ' nr==1{printf "%10s%10s%10s%10s%10s\n", $1,$2,$3,$4, "Total"}\

> nr>=2{total = $2+$3+$4\

> printf "%10s%10d%10d%10d%10.2f\n", $1,$2,$3,$4,total} '

All awk actions, which are actions within {}, can be separated by semicolons when multiple command aids are required, delimited, or directly separated by "enter" keys, such as above

In a logical operation, be sure to use the two equal sign "= =" in case of "equals"

When formatting the output, be sure to add \ n in the format of printf to branch

Unlike Bash,shell variables, in awk, variables can be used directly, without the need for a $ symbol.


Bird Brother book awk tool learning

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.