Linux (11)-awk usage

Source: Internet
Author: User

Definition of awk

Original Meaning: Aho, Weinberger, kernighan

Definition: three creators, Aho, Weinberger, and kernighan

Awk Introduction

Awk is an excellent text processing tool. It is not only one of the most powerful Data Processing engines in Linux, but also in any environment. The maximum functionality of this programming and data operation language (its name is derived from the first letter of its founder, Alfred Aho, Peter Weinberger, and Brian kernighan) depends on a person's knowledge. Awk provides extremely powerful functions: style loading, flow control, mathematical operators, process control statements, and even built-in variables and functions. It has almost all the exquisite features of a complete language. In fact, awk does have its own language: awk programming language. The three creators have formally defined it as "style scanning and processing language ". It allows you to create short programs that read input files, Sort data, process data, perform calculations on input, and generate reports. There are countless other functions.

In short, awk is a programming language tool used to process text. Awk is similar to the shell programming language in many aspects, although awk has its own syntax. Its design concept comes from the effective languages, language tools YACC and Lex of snobol4, sed, Marc rochkind design. Of course, it also obtains some excellent ideas from C language. When awk was initially created, it was intended for text processing, and the basis of this language is to execute a series of commands as long as there is a pattern match in the input data. This utility scans each row of the file to find the pattern that matches the content given in the command line. If the Matching content is found, perform the next programming step. If no matching content is found, process the next row.

Although operations may be complex, the command syntax is always:

Awk '{Pattern + action }'

Specifically, pattern indicates the content that awk looks for in the data, and action is a series of commands executed when matching content is found. Curly braces ({}) do not always appear in the program, but they are used to group A series of commands according to a specific mode.

Gawk is the GNU version of awk.

The awk is included in the general UNIX operating system, and the awk versions attached to different UNIX operating systems are also different. If the system used by the reader is not included with the awk, you can use anonymous FTP to obtain the following information:
Phi.sinica.edu.tw:/pub/gnu
Ftp.edu.tw:/Unix/gnu
Prep.ai.mit.edu:/pub/gnu

Note: a programming language is especially useful for its pattern matching syntax and is usually used for data retrieval and data conversion. A gnu version is called gawk.

Awk call Method

Awk provides different solutions to meet a variety of needs. They are:

I. awk command line: You can use awk like a Common Unix Command. You can also use the awk programming language in the command line. Although awk supports multi-line input, however, inputting a long command line and ensuring that it is correct is a headache. Therefore, this method is generally only used to solve simple problems. Of course, you can also reference awk command lines or even awk program scripts in shell script programs.

2. Use the-F option to call the awk program. Awk allows an awk program to be written into a text file, and then the program is called and executed using the-F option in the awk command line. The specific method is described in the awk syntax.

3. Use the command interpreter to call the awk program: using the command interpreter function supported by UNIX, we can write an awk program into a text file, and then add the following to its first line:

#! /Bin/awk-F

And grant this text file the execution permission. After doing so, you can call and execute this awk program in the command line in a way similar to the following.

Awk script text name: file to be processed

Awk syntax

Like other UNIX commands, awk has its own Syntax:

Awk [-f re] [parameter...] ['prog'] [-F progfile]

Parameter description:

-F re: Allows awk to change its field separator.

Parameter: this parameter helps assign values to different variables.

'Prog': Specifies the Program Statement segment of the awk. The statement segment must be enclosed by the single extension number 'and' to prevent shell interpretation. The standard format of this Program Statement segment is:

'Pattern'

The pattern parameter can be any of the regular expressions of egrep. It can be made up of the syntax/RE/and some style matching techniques. Similar to SED, you can also use "," to separate the two formulas to select a certain range. For details about the matching, refer to the appendix. If you still don't understand it, find a Unix book to learn grep and sed (I learned the matching technology when learning ed ). The action parameter is always enclosed by braces. It consists of a system awk Statement, which is separated. Awk interprets them and performs their operations on the records that match the pattern given. Similar to shell, you can also use "#" as the annotator to make the content from "#" to the end of the line into comments, which will be ignored during interpretation. You can omit either pattern or action, but not both. If pattern is omitted, no style match exists, indicating that all rows (Records) are operated, if action is omitted, the default operation is executed. The default operation is displayed on the standard output.

-F progfile: Allows awk to call and execute progfile to specify a program file. Progfile is a text file that must comply with awk syntax.

In_file: the input file of the awk. awk allows processing of multiple input files. It is worth noting that awk does not modify the input file. If no input file is specified, awk accepts the standard input and displays the result on the standard output. Awk supports input/output redirection.

References

1. Baidu awk: http://baike.baidu.com/view/209681.htm

2. awk Manual: http://linuxfire.com.cn /~ Lily/awk.html

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.