Simple usage of awk (built-in functions)

Source: Internet
Author: User

As a Windows system administrator, text processing is a difficult task, but it is obviously a dish of cabbage in Linux. on Linux text processing tools, awk accounts for 60% of the size, and SED and grep are also available. Here we mainly record some common usage methods of awk. For details, we can refer to the following,
Awk is not a word, but the founder of the language (three people: Alfred v. aho, Peter J. we I n B e RG e r, Brian W. kernighan (short for short), so you don't expect to find out the meaning of this word from your English dictionary. haha
Back to the truth:
Gawk is an awk under the GNU program. It contains all the functions of awk and is a programming language, so it has a fixed format. gawk cannot be used unless it is in the following format:
Gawk '/pattern/{action}' file.txt
The pattern is enclosed by //, and the action is enclosed by curly brackets. The action can also be left empty.
Well, let's take a look at the example:
Gawk '/rpcuser/'/etc/passwd
This sentence refers to the output of the entire line of the/etc/passwd file containing the rpcuser. Is it very convenient?
Of course, you may have more demanding requirements. You need to output 3rd fields and want to know if he can log on? In this way, you only need to add an action in it and then you can try it:
Gawk '/rpcuser/{print $3}'/etc/passwd
Explanations:
 
/Rpcuser/here // a pattern indicates all lines that include rpcuser; {print
$3} is an action, represented by {}. By default, gawk uses spaces or the tab key to partition blocks for each row. The first block is $1, the second block is $2 and so on.
You can also change the Block Separator to another one, such as a colon, semicolon, or comma, for example:
Gawk-F ":" '/rpcuser/{print $2}'/etc/passwd
Note: F in-F must be capitalized.

You can also calculate the file area. For example:
Example of Operators
+ Addition operation
-Subtraction
* Multiplication
/Division
^ Multiplication Operator 3 ^ 2 (= 9)
% Calculate the remainder 9% 4 (= 1)
In awk, the Third Field gawk '/**/{print $2/100}' file.txt is divided by one hundred.
Expressions are also supported. For example, actions can also be written as {print $1 + $2 * $3}

Awk also has some internal functions, such
G a w k has various internal functions, which are described as follows:
6.6.1 random numbers and mathematical functions
SQRT (x) returns the square root of X.
Sin (x) returns the sine of X.
Cos (x) returns the cosine of X.
A t a n 2 (x, y) returns the cotangent function of x/y.
Log (x) calculates the natural logarithm of X.
Exp (x) Evaluate the power of e of X
INT (x) returns the integer of X.
Rand () calculates the random number between 0 and 1.
Srand (x) set X to the number of seeds of r a n d ()

There are also internal functions of strings, such:
Index Function Format: {print index ("technical", "ch")} searches for the location where ch appears in the string technical. The result is 3.

Length Function Format: length (ABCDE) Result 5.
Match (string, example) finds the position of example on the left in string.
Sprintf is similar to printf
Sub (Regexp, replacement, target) searches for the longest and leftmost position that matches Regexp in the target string, and replaces the leftmost Regexp with the string replacement.
For example:
STR = "Water, water, everywhere"
Sub (/AT/, "ith", STR)
The returned results are wither, water, and everywhere.

Substr (string, start, length) returns the substring of the string, for example:
Substr (washingdun, 5, 3) returns a string starting from the fifth character. The length is three characters, namely

Tolower (string) converts string to lowercase letters
Toupper (string) converts string to uppercase letters

Close (filename) close the file
System (command). This function allows you to execute system commands.
Gawk '$1! = "Tim" {print} 'testfile
In the testfile text, the first line with the same field as Tim is printed.
Formatted output:
Gawk '$1! = "Tim" {print $1, $5, $6, $2} 'testfile

Further, you can add a string to the print action, for example:
Gawk '$1! = "Tim" {print "the entry for", $1, "is not Tim.", $2} 'testfile
Note: Each part in print must be separated by a comma.

It's amazing, but I want to know where gawk can be used:
• Select several lines and columns or fields of the file as required for display and output.
• Analyze the frequency and position of a word in the document.
• Prepare and format the output based on the information of a document.
• Filter output documents in a powerful way.
• Calculate based on the values in the document.

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.