awk Introduction and study Notes collection 1th/3 page _php Tutorial

Source: Internet
Author: User
Tags posix
copyright©2004 This article complies with the GPL agreement, welcome to reprint, revise, distribute.

First release date: August 6, 2004


--------------------------------------------------------------------------------

Table of Contents

1. Introduction to Awk
2. awk command format and options
2.1. AWK has two forms of syntax
2.2. Command options
3. Modes and operations
3.1. Mode
3.2. Operation
4. AWK's Environment variables
5. awk operator
6. Records and Domains
6.1. Recording
6.2. Domain
6.3. Domain Separators
7. Gawk dedicated regular expression meta-characters
8. POSIX character Set
9. Match operator (~)
10. Compare expressions
11. Scope templates
12. An example of verifying the validity of a passwd file
13. Several examples
AWK programming
14.1. Variables
14.2. Begin Module
14.3. End Module
14.4. Redirection and piping
14.5. Conditional statements
14.6. Cycle
14.7. Arrays
14.8. awk built-in functions
How-to.
1. Introduction to Awk
Awk is a programming language that is used to process text and data under Linux/unix. Data can come from standard input, one or more files, or the output of other commands. It supports advanced functions such as user-defined functions and dynamic regular expressions, and is a powerful programming tool under Linux/unix. It is used in the command line, but more is used as a script. awk handles text and data in such a way that it scans the file line-by-row, from the first line to the last line, looks for rows that match a particular pattern, and makes the actions you want on those lines. If no processing action is specified, the matching rows are displayed to the standard output (screen), and if no pattern is specified, all rows specified by the operation are processed. Awk represents the first letter of its author's last name, respectively. Because its author is three people, respectively is Alfred Aho, Brian Kernighan, Peter Weinberger. Gawk is the GNU version of AWK, which provides some extensions to the Bell Lab and GNU. Awk is described below as an example of the gawk of the gun, which has been linked to gawk in the Linux system, so all of this is described in awk below.

2. awk command format and options
2.1. AWK has two forms of syntax
awk [Options] ' script ' Var=value file (s)

awk [Options]-F scriptfile var=value file (s)

2.2. Command options
-F FS or--field-separator FS
Specifies the input file delimiter, FS is a string, or is a regular expression, such as-f:.

-V Var=value or--asign var=value
Assigns a user-defined variable.

-F scripfile or--file ScriptFile
Reads the awk command from the script file.

-MF nnn AND-MR nnn
Set intrinsic limits on the NNN value, the-MF option limits the maximum number of blocks assigned to NNN, and the-MR option limits the maximum number of records. These two features are the extended functionality of the Bell Lab version of AWK and are not available in standard awk.

-W compact or--compat,-w traditional or--traditional
Run awk in compatibility mode. So Gawk's behavior is exactly the same as the standard awk, and all awk extensions are ignored.

-W copyleft or--copyleft,-w copyright or--copyright
Print a brief copyright message.

-W Help or--help,-w usage or--usage
Print all awk options and a short description of each option.

-W Lint or--lint
Print warnings for structures that cannot be ported to traditional UNIX platforms.

-W lint-old or--lint-old
Print a warning about a structure that cannot be ported to a traditional UNIX platform.

-W POSIX
Turn on compatibility mode. However, the following limitations are not recognized: \x, function keyword, func, swap sequence, and when FS is a space, the new row is used as a domain delimiter, and the operators * * and **= are not valid in lieu of ^ and ^=;fflush.

-W re-interval or--re-inerval
Allows the use of interval regular expressions, reference (POSIX character class in grep), such as parenthesis expression [[: Alpha:]].

-W source Program-text or--source Program-text
Use Program-text as the source code, which can be mixed with the-f command.

-W version or--version
Print the version of the bug report information.

3. Modes and operations
awk scripts are made up of patterns and operations:
The pattern {action} is like $ awk '/root/' test, or the awk ' $ < ' test.

Both are optional, and if there is no pattern, the action is applied to all records, and if there is no action, the output matches all records. By default, each input line is a record, but the user can specify a different delimiter to delimit by using the RS variable.

3.1. Mode
The pattern can be any one of the following:

/Regular expression/: An extension set that uses wildcard characters.

Relational expressions: You can use the relational operator in the following operator table, which can be a string or numeric comparison, such as $2>%1 to select a row with a second field that is longer than the first word.

Pattern matching expression: with operator ~ (match) and ~ ~ (not matched).

Mode, Mode: Specifies the range of a row. The syntax cannot include the begin and end patterns.

BEGIN: Lets the user specify the action that occurs before the first input record is processed, which is where the global variable is usually set.

End: The action that occurs after the last input record has been read by the user.

3.2. Operation
An action consists of one or more commands, functions, and expressions, separated by a newline or semicolon, and enclosed in curly braces. There are four main parts:

Variable or array assignment

Output command

Built-in functions

Control Flow Command

4. AWK's Environment variables
Table 1. AWK's environment variables

Variable description
$n the nth field of the current record, and the fields are separated by FS.
A complete input record.
ARGC the number of command-line arguments.
Argind the location of the current file in the command line (starting at 0).
The ARGV contains an array of command-line arguments.
CONVFMT number conversion format (default is%.6g)
ENVIRON an associative array of environment variables.
ERRNO a description of the last system error.
fieldwidths field width list (separated by Space key).
FileName the current file name.
FNR with NR, but relative to the current file.
The FS field delimiter (the default is any space).
IGNORECASE If true, the matching of the case is ignored.
NF the number of fields in the current record.
NR current record count.
OFMT the output format of the number (the default value is%.6g).
OFS The Output field delimiter (the default value is a space).
ORS the output record delimiter (the default value is a newline character).
Rlength the length of the string that is matched by the match function.
The RS record delimiter (default is a line break).
Rstart the first position of a string that is matched by the match function.
Subsep array subscript delimiter (the default value is \034).

http://www.bkjia.com/PHPjc/319039.html www.bkjia.com true http://www.bkjia.com/PHPjc/319039.html techarticle Copyright2004 This article complies with the GPL agreement, welcome to reprint, revise, distribute. First release date: August 6, 2004------------------------------------------------------------------------...

  • 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.