Writing shell scripts in awk

Source: Internet
Author: User

I. Overview

1. Product Overview:

Awk is a programming language used to scan and process text and data under Linux/unix. Data can come from standard input, files, and pipelines.

Awk represents the first letter of its author's surname, respectively. Because its author is three people, respectively Alfred Aho, Peter Weinberger, Brian Kernighan.

There are actually many versions of awk, such as awk, Nawk, Mawk, gawk, MKS awk, Tawk ... There are open source products and commercial products.

The current version of the awk compiler commonly used in Linux is Mawk,gawk, where Redhat is used as the representative of Gawk, and the Mawk is used by Ubuntu.

Gawk is the open source code implementation of the GNU Project's awk interpreter.

This article will use Gawk as the explanation tool.

2. Principle:

1. awk scans the file line by row, from the first line to the last line, looking for rows that match a specific pattern and doing what you want on those lines.

2. The awk infrastructure includes pattern matching (for locating the rows to be processed) and processing (that is, processing actions).

Pattern {Action}

# Hint: When awk reads each row of the file's contents, it compares whether the row matches the given pattern, executes the process if it matches, or does nothing with the row.

If no processing script is specified, the matching row is displayed to the standard output, that is, the default processing action is the print line;

If a pattern match is not specified, all data is matched by default.

3. Awk has two special patterns: Begin and end, which are placed before any data is read and executed after all the data has been read.

3. Awk Flowchart:

Tip: Awk treats each row in the file as a record and splits the records into fields that are separated by a space or tab character by default.

such as this is a test file. will be split into 5 fields that awk can handle separately for each of the 5 fields.

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.