Home Backend Development PHP Tutorial Introduction to awk and collection of study notes Page 1/3_PHP Tutorial

Introduction to awk and collection of study notes Page 1/3_PHP Tutorial

Jul 21, 2016 pm 03:51 PM
awk and Revise protocol study collect notes Introduction

Copyright © 2004 This article complies with the GPL agreement. Reprinting, modification, and distribution are welcome.

First release date: 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. Modes and operations
3.1. Modes
3.2. Operations
4. awk environment variables
5. awk operators
6. Records and fields
6.1. Records
6.2. Domains
6.3. Domain separators
7. gawk-specific regular expression metacharacters
8. POSIX character set
9. Match operator (~ )
10. Comparison expressions
11. Range templates
12. An example of verifying the validity of the passwd file
13. Several examples
14. awk programming
14.1. Variables
14.2. BEGIN module
14.3. END module
14.4. Redirection and pipeline
14.5. Conditional statement
14.6. Loop
14.7. Array
14.8. Internals of awk Creating functions
15. How-to
1. Introduction to awk
Awk is a programming language 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 from the command line, but more commonly 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
Specifies the input file 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 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 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 a 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 are not portable to traditional unix platforms.

-W lint-old or --lint-old
Print warnings about structures that are not portable to legacy unix platforms.

-W posix
Turn on compatibility mode. However, it has the following restrictions and is 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, 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 script is composed of modes 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 expression: You can use the relational operators in the operator table below to perform operations. It can be a comparison of strings or numbers. For example, $2>%1 selects the second field to be longer than the first field. OK.

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 actions that occur 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 of awk
Table 1. Environment variables of awk

Variable Description
$n The nth field of the current record, the 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 digital conversion format (default value is %.6g)
ENVIRON environment variable associative array.
ERRNO Description of the last system error.
FIELDWIDTHS field width list (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 The number of fields in the current record.
NR Current record number.
OFMT digital output format (default value is %.6g).
OFS output field delimiter (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

http://www.bkjia.com/PHPjc/319039.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319039.htmlTechArticleCopyright2004 This article complies with the GPL agreement. Reprinting, modification and distribution are welcome. First published: August 6, 2004----------------------------------------- ----------------------------------...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to change the personal name in the group on DingTalk_How to modify the personal name in the group on DingTalk How to change the personal name in the group on DingTalk_How to modify the personal name in the group on DingTalk Mar 29, 2024 pm 08:41 PM

1. First open DingTalk. 2. Open the group chat and click the three dots in the upper right corner. 3. Find my nickname in this group. 4. Click to enter to modify and save.

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of &quot;What to do if the notes published by Xiaohongshu are missing&quot; and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click &quot;Me&quot; → &quot;Publish&quot; → &quot;All Publications&quot; to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

Can Douyin Blue V change its name? What are the steps to change the name of corporate Douyin Blue V account? Can Douyin Blue V change its name? What are the steps to change the name of corporate Douyin Blue V account? Mar 22, 2024 pm 12:51 PM

Douyin Blue V certification is the official certification of a company or brand on the Douyin platform, which helps enhance brand image and credibility. With the adjustment of corporate development strategy or the update of brand image, the company may want to change the name of Douyin Blue V certification. So, can Douyin Blue V change its name? The answer is yes. This article will introduce in detail the steps to modify the name of the enterprise Douyin Blue V account. 1. Can Douyin Blue V change its name? You can change the name of Douyin Blue V account. According to Douyin’s official regulations, corporate Blue V certified accounts can apply to change their account names after meeting certain conditions. Generally speaking, enterprises need to provide relevant supporting materials, such as business licenses, organization code certificates, etc., to prove the legality and necessity of changing the name. 2. What are the steps to modify the name of corporate Douyin Blue V account?

How to modify the address location of published products on Xianyu How to modify the address location of published products on Xianyu Mar 28, 2024 pm 03:36 PM

When publishing products on the Xianyu platform, users can customize the geographical location information of the product according to the actual situation, so that potential buyers can more accurately grasp the specific location of the product. Once the product is successfully put on the shelves, there is no need to worry if the seller's location changes. The Xianyu platform provides a flexible and convenient modification function. So when we want to modify the address of a published product, how do we modify it? This tutorial guide will provide you with a detailed step-by-step guide. I hope it can help. Everyone! How to modify the release product address in Xianyu? 1. Open Xianyu, click on what I published, select the product, and click Edit. 2. Click the positioning icon and select the address you want to set.

How to quickly collect hardwood in Stardew Valley How to quickly collect hardwood in Stardew Valley Mar 26, 2024 am 11:21 AM

Hardwood is an important synthetic material in Stardew Valley. We can have many uses in the game, so we can stock it up in our daily life. What are the specific ways to obtain hardwood? Below we will bring you Stardew Valley. You can refer to the way to obtain hardwood in Monogatari if necessary. Ways to obtain hardwood in Stardew Valley 1. Go to the secret forest every day to dig tree stumps to quickly obtain hardwood. 2. There is a chance to obtain mahogany seeds by mining tree stumps and fighting monsters in the secret forest. 3. Planting seeds in the yard will produce mahogany trees. 4. Finally, after mining mahogany, you can quickly obtain a large amount of hardwood.

Python ORM Performance Benchmark: Comparing Different ORM Frameworks Python ORM Performance Benchmark: Comparing Different ORM Frameworks Mar 18, 2024 am 09:10 AM

Object-relational mapping (ORM) frameworks play a vital role in python development, they simplify data access and management by building a bridge between object and relational databases. In order to evaluate the performance of different ORM frameworks, this article will benchmark against the following popular frameworks: sqlAlchemyPeeweeDjangoORMPonyORMTortoiseORM Test Method The benchmarking uses a SQLite database containing 1 million records. The test performed the following operations on the database: Insert: Insert 10,000 new records into the table Read: Read all records in the table Update: Update a single field for all records in the table Delete: Delete all records in the table Each operation

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

See all articles