Text Processing Tool---AWK

Source: Internet
Author: User

About AWK

Awk is a powerful report generation tool that can be formatted and displayed on the screen later, based on the information we enter. The so-called format is based on our custom format will be required to display the information more beautiful. Awk was originally completed in 1977 and later published a more powerful awk, called Nwak or gawk. Nawk is working under the Windows operating system, and Gawk is developed by the GNU organization. He used it under the Linux/unix system. In Rhel 5.8, awk is a soft-link file for gawk. In fact, AWK is also a programming language, it also supports a variety of control statements, such as: If...else, while loops, for loops, and various operators and so on. We will explain more about the use of awk in one by one below.


How awk Works

Awk reads each line in the text, divides it into n (or N) columns using a specific delimiter, and then displays it (this is an execution action, in most cases print or printf, meaning printed). Each of these paragraphs can be represented by a positional variable, which is $1,$2,$3 .... $ $ for the first paragraph, and $ for the second paragraph followed by an analogy. Where $ A represents the contents of this line of processing.



The syntax structure of awk

awk [option] ' PATTERN {ACTION} ' file1 file2 ...

Commonly-used option:

-F: Specify delimiter, default delimiter is blank

-V var_name=#: Used to assign a value to a variable on the command line


Before we talk about the pattern and action of awk, let's start with the variables and operators in awk, which are used in the following pattern and action, as well as the variables and operators in awk.


I. Variables of AWK

awk variables are divided into built-in variables and custom variables

1. awk built-in variables are divided into record variables and data variables

1.1. The record variables of awk built-in variables

Fs:field separator, the field delimiter used when reading the file;

Rs:record separator, enter the text information using the line break;

Ofs:output Filed Separator:

Ors:output Row Separator


1.2. Data variables of awk built-in variables

The number of row records processed by the nr:the numbers of the input Records,awk command, and if there are multiple files, this number will uniformly count the rows in the processed multiple files;

For example:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/1C/wKioL1SRoxXAx0pHAAArWx1s0vQ409.jpg "title=" nr.png "alt=" Wkiol1sroxxax0phaaarwx1s0vq409.jpg "/>

Nf:number of field, the number of fields (fragments or columns) recorded in the current row, that is, the total number of segments divided by each line;

For example:

[Email protected] ~]# Tail-n 1/etc/passwd | Awk-f: ' {print NF} '

7


FNR: Unlike NR, FNR is used to record the total number of rows processed in the current file;

For example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/57/1E/wKiom1SRomST89kVAAApsch0SZc262.jpg "title=" Fnr.png "alt=" Wkiom1sromst89kvaaapsch0szc262.jpg "/>

ARGV: This variable represents an array that holds all the parameters in the entire awk command line, excluding the pattern and action

For example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/57/1E/wKiom1SRoIji8mkwAABpjQeV6b4864.jpg "title=" Argv.png "alt=" Wkiom1sroiji8mkwaabpjqev6b4864.jpg "/>

where argv[0] means that the first argument is awk,argv[1] means the second argument is a.txt,argv[2] means the third parameter is B.txt

ARGC: This variable represents the number of arguments in the awk command that do not include the pattern and action, but includes the awk command;

For example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/57/1C/wKioL1SRoN6AEoYJAAArB2kAhgc911.jpg "title=" Argc.png "alt=" wkiol1sron6aeoyjaaarb2kahgc911.jpg "/>

Since both the A.txt and B.txt files contain only 2 rows of data, a total of four rows


FileName: Represents the file name of the current file processed by the awk command;

For example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/57/1C/wKioL1SRoejjqDYxAAA9KN4-1WA306.jpg "title=" Fielname.png "alt=" Wkiol1sroejjqdyxaaa9kn4-1wa306.jpg "/>

Because A.txt has 2 lines in this file, 2 rows of files are displayed


ENVIRON: This is an associative array of current shell environment variables and their values, and an array of awk is described below.

For example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/57/1E/wKiom1SRoB2wZFUIAACOHrHrUkw126.jpg "title=" Environ.png "alt=" Wkiom1srob2wzfuiaacohrhrukw126.jpg "/>






This article is from the "Linux Learning path" blog, so be sure to keep this source http://xslwahaha.blog.51cto.com/4738972/1591213

Text Processing Tool---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.