


axialisiconworkshop awk introduction and study notes collection page 1/3
Copyright © 2004 This article complies with the GPL agreement. Reprinting, modification, and distribution are welcome.
First release time: August 6, 2004
---------------------------------------- -----------------------------------------------
Table of Contents
1 . Introduction to awk
2. awk command format and options
2.1. There are two forms of awk syntax
2.2. Command options
3. Mode and operation
3.1. Mode
3.2. Operation
4. awk environment variables
5. awk operator
6. Records and fields
6.1. Records
6.2. Fields
6.3. Field separators
7. Gawk-specific regular expression metacharacters
8. POSIX character set
9. Match operator (~)
10 .Comparison expression
11. Range template
12. An example of verifying the validity of passwd file
13. Several examples
14. awk programming
14.1. Variables
14.2. BEGIN module
14.3. END module
14.4. Redirection and pipes
14.5. Conditional statements
14.6. Loops
14.7. Arrays
14.8. Built-in functions of awk
15. How-to
1. Introduction to awk
awk is a programming language used for processing under linux/unix Text and data are processed. 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 from the command line, but more often as a script. The way awk processes text and data is that it scans the file line by line, from the first line to the last line, looking for lines that match a specific pattern, and performs the operations you want on these lines. If no processing action is specified, matching lines are displayed to the standard output (screen). If no mode is specified, all lines specified by the operation are processed. awk respectively represents the first letter of its author's last name. Because its authors are three people, namely Alfred Aho, Brian Kernighan, and Peter Weinberger. gawk is the GNU version of awk, which provides some extensions from Bell Labs and GNU. The awk introduced below takes GUN's gawk as an example. In the Linux system, awk has been linked to gawk, so the following is all introduced using awk.
2. awk command format and options
2.1. There are two forms of awk 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
Specify the input file fold separator, fs is a string or a regular expression, such as -F:.
-v var=value or --asign var=value
Assign a user-defined variable.
-f scripfile or --file scriptfile
Read awk commands from script files.
-mf nnn and -mr nnn
Set intrinsic limits on the nnn value. The -mf option limits the maximum number of blocks allocated to nnn; the -mr option limits the maximum number of records. These two functions are extended functions of the Bell Labs version of awk and are not applicable in standard awk.
-W compact or --compat, -W traditional or --traditional
Run awk in compatibility mode. So gawk behaves exactly like standard awk, and all awk extensions are ignored.
-W copyleft or --copyleft, -W copyright or --copyright
Print brief copyright information.
-W help or --help, -W usage or --usage
Print all awk options and a brief description of each option.
-W lint or --lint
Print warnings about structures that cannot be ported to traditional unix platforms.
-W lint-old or --lint-old
Print warnings about structures that are not portable to traditional unix platforms.
-W posix
Turn on compatibility mode. However, there are the following restrictions and are not recognized: x, function keywords, func, escape sequences, and when fs is a space, the new line is used as a field separator; the operators ** and **= cannot replace ^ and ^= ;ffflush is invalid.
-W re-interval or --re-inerval
Allows the use of interval regular expressions, refer to (Posix character class in grep), such as bracket expression [[:alpha:]].
-W source program-text or --source program-text
Use program-text as the source code and can be mixed with the -f command.
-W version or --version
Print the version of the bug report information.
3. Patterns and operations
awk scripts are composed of patterns and operations:
pattern {action} such as $awk '/root/' test, or $awk '$3 < 100' test.
Both are optional. If there is no pattern, the action is applied to all records. If there is no action, the output matches all records. By default, each input line is a record, but the user can specify different delimiters through the RS variable.
3.1. Pattern
The pattern can be any of the following:
/regular expression/: an expanded set using wildcards.
Relational expressions: You can use the relational operators in the operator table below to perform operations, which can be comparisons of strings or numbers. For example, $2>%1 selects rows whose second field is longer than the first field.
Pattern matching expression: use operators ~ (match) and ~! (not match).
Mode, mode: Specify a range of lines. This syntax cannot include BEGIN and END patterns.
BEGIN: Allows the user to specify the action that occurs before the first input record is processed. Global variables can usually be set here.
END: Let the user take actions after the last input record is read.
3.2. Operation
An operation consists of one or more commands, functions, and expressions, separated by newlines or semicolons, and located within curly brackets. There are four main parts:
Variable or array assignment
Output command
Built-in function
Control flow command
4. awk environment variables
Table 1. awk environment variables
Variable Description
$n The nth field of the current record, Fields are separated by FS.
$0 Complete input record.
ARGC The number of command line parameters.
ARGIND The position of the current file in the command line (counting from 0).
ARGV An array containing command line arguments.
CONVFMT Number conversion format (default value is %.6g)
ENVIRON Environment variable associative array.
ERRNO Description of the last system error.
FIELDWIDTHS List of field widths (separated by space bar).
FILENAME Current file name.
FNR Same as NR, but relative to the current file.
FS field separator (default is any space).
IGNORECASE If true, perform a case-ignoring match.
NF Number of fields in the current record.
NR Current record number.
OFMT Number output format (default value is %.6g).
OFS Output field separator (default value is a space).
ORS Output record delimiter (default value is a newline character).
RLENGTH The length of the string matched by the match function.
RS record separator (default is a newline character).
RSTART The first position of the string matched by the match function.
SUBSEP array subscript separator (default value is
The above introduces page 1/3 of axialisiconworkshop awk introduction and collection of study notes, including the content of axialisiconworkshop. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

RESTAPI design principles include resource definition, URI design, HTTP method usage, status code usage, version control, and HATEOAS. 1. Resources should be represented by nouns and maintained at a hierarchy. 2. HTTP methods should conform to their semantics, such as GET is used to obtain resources. 3. The status code should be used correctly, such as 404 means that the resource does not exist. 4. Version control can be implemented through URI or header. 5. HATEOAS boots client operations through links in response.

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

The main function of anonymous classes in PHP is to create one-time objects. 1. Anonymous classes allow classes without names to be directly defined in the code, which is suitable for temporary requirements. 2. They can inherit classes or implement interfaces to increase flexibility. 3. Pay attention to performance and code readability when using it, and avoid repeatedly defining the same anonymous classes.
