[Linux command] awk

Source: Internet
Author: User

I. Introduction of AWK

awk: Report Generator, which is processed in behavioral units and displayed after formatting

Awk is a powerful text analysis tool, with the search for grep and the editing of SED, which is especially powerful when it comes to analyzing data and generating reports. To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.

Second, awk variables

2.1 awk built-in variables of record variables

FS: Default is white space character, specify input delimiter rs: Enter text information using the line break OFS: Output character delimiter ors: Output line delimiter
FS or-F
~This is ~]# awk- F"'{print $}' test.txt FS can use the-f option, and when the-f option is not used, the awk Default-F option is a space or tab this isa test.

This is a test.
Rs

[email protected] ~]# CAT ABC
1|2|3
[[email protected] ~]# awk ' begin{rs= ' | '} {print $} ' abc found ' | ' Symbol, turn it into a carriage return
1
2
3
 ofs[[email protected]  ~]# Cat Test.txt  this  is   a test. [[email protected]  ~]# awk  " ofs=":  "{print $1,$2,$3,$4}  "   test.txt Change the interval of the file by the space character to the component number  this : is : A:test. 
~]# Cat ABC123'begin{ors= "----\ n"}{print}'  ABC # OUTPUT separator symbol.  1----2----3----

2.2 awk built-in variable data variables

The number of records processed by the Nr:awk command: If there are multiple files, this number will uniformly count the rows processed in multiple files Fnr: Unlike NR, FNR is used to record the row being processed is the total number of rows currently processed in this file NF: Number of fields in the current record
[[Email protected] ~]# Cat Test1123[[Email protected]~]# Cat Test212345 NR[[email protected]~]# awk'{print NR}'test1 Test2 # Two rows of files12345678 FNR[[email protected]~]# awk'{print FNR}'test1 Test2 # Two lines of files are displayed separately12312345
~This is'{print NF}'  test.txt  # NF is a calculation of the number of fields in a row 4'{print $NF}'  test.txt # while $ NF is a simple notation test for the last field in a row.

Third, printf
Use format for printf commands:
printf format,item1,item2,...

Points:
1. The biggest difference from the print command is that printf needs to specify format;
2. Format is used to specify the output format of each subsequent item
3. The printf statement does not automatically print line breaks; \ n

The specified characters in format are preceded by a%, followed by a character, as follows:
%c: The ASCII code that displays the characters;
%s: Display string

Modifier:
N: Display width;
-: Align Left
+: Display numeric symbols


Common pattern types

1, regexp: Regular expression, the format is/regular expression/
2, expression: expressions, whose value is not 0 or non-empty string satisfies the condition, such as: $ ~/foo/or = = "magedu", with operator ~ (match) and!~ (mismatch)
3, Ranges: Specify the matching range, the format is Part1,part2
4. Begin/end: Special mode, run only once or before the end of the awk command execution
5. Empty (null mode): matches any input line

Example: Get each user name and shell format displayed in/etc/passwd:

 [[email protected] ~]# awk-f:  " { printf "%-10s%-10s\n", $ $, $NF}   "/etc//bin/nolo Gindaemon /sbin/nologinadm /sbin/nolo Gin   
' begin{printf "%-10s%-10s\n", "User", "Shell"}{printf "%-10s%-10s\n", $ $NF} ' /etc/passwduser       Shell     root       /bin/Bash bin        /sbin/Nologindaemon      /sbin/nologinadm        /sbin/nologinlp         /sbin/nologin ...

Example: Get the number of each state for a server TCP connection

' /^tcp/{++s[$NF]}end{for (i in S) {print i,s[i]}} '  34

/^tcp/ Regular expression: Matches lines beginning with TCP
{++s[$NF]} gets the last byte of each row added to the array, at which time the S array $NF as subscript
end{for (i in S) {print i,s[i]}} after processing is complete, the subscript and the variable values are printed out via subscript loop

[Linux command] awk

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.