Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

AWK study notes

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Learning ideas:

AWK command line writing consists of three parts of BEGEN Program END.

BEGEN: executed before AWK reads the input stream text line processing, used to initialize variables and define output header information Program: defines how to handle read data, which consists of two parts: pattern {action statements;..}, which consists of patterns and actions, and matching patterns perform actions. END executes END statements after the file is processed line by line, which is used to count the results and generate reports

Processing flow:

AWK treats the processing object as a two-dimensional table, row-by-row reading and row-by-row processing. By default, the space character is used as the delimiter (you can use-F to specify the delimiter), and the file content is separated into multiple fields (which can be understood as columns or fields). A row is called a record, and each record is composed of multiple fields. These rows and columns are mapped to AWK's built-in variables (you can customize variables through the-v option), so users can use these built-in variables to locate the contents of the processing object for processing, which is simply understood as: row-based reading, column-based processing, and output.

Syntax:

Awk [options] 'program' var=value file...

Awk [options]-f programfile var=value file...

Awk [options] 'BEGIN {action;...} pattern {action;...} END {action;...}' file...

Every sentence block has

Common option

The-F "delimiter" is used to specify the field delimiter used in reading the input stream text line. If it is not specified, the white space character is used as the delimiter by default, and multiple consecutive white space characters are automatically compressed as a blank character.

-f file reads common statement blocks from the specified file

-v var=value for variable assignment

Built-in variables:

AWK treats the processing object as a two-dimensional table reading row by row, processing row by row, a row as a record and a column as a field, and rows and columns are mapped into AWK's built-in variables, so users can deal with data like a two-dimensional table. Therefore, the commonly used built-in variables of AWK are defined in terms of row and column dimensions.

Row and column dimension built-in variable: AWK divides the text lines read into multiple columns with specified delimiters, which are mapped to the built-in variable $1, which is $1 for column 1, $2 for column 2, and so on. Note: $0 represents all columns. If there is no action action defined in the statement block, the default action is print $0, that is, all columns are printed.

NF: represents the number of fields separated, that is, the number of columns NR: one line is a record, and the line number FNR:AWK can take multiple files as the input stream. If you want each file to output independent line numbers, you can use FNRFILENAME:AWK to use multiple files as the input stream. If you want to output the file name in the output line, use FILENAME.

Delimiter:

AWK can use the-F option to specify the delimiter when dealing with the input stream, but it will certainly meet the needs of the user, for example: in the printout, it is troublesome to specify the separator manually when printing the input. AWK also thought of this need of the user and provided built-in variables related to the delimiter. The processing flow of AWK is to generate the output stream after the input stream is processed, so the delimiter has the input separator and the output delimiter.

Remember a few words: F:Field S:Separated O:output R:recordFS: input delimiter OFS: output delimiter RS: input record separator ORS: output record delimiter FNR: each file is counted separately, record number FILENAME: current file name

Other variables

ARGC: the number of command line arguments

ARGV: an array that holds the parameters given by the command line

Custom variable

Custom variables are generally used for data-assisted processing (such as control statements), report output beautification and so on. Custom variables can be defined in two ways:

-v option definition: without prior declaration, written directly in the program statement block.

Formatted output

The strong point of AWK is the report output function. The printf command can format and output the processed data to beautify the report. Note the following when formatting the output:

You must specify that FORMA does not wrap automatically. You need to explicitly give the newline control character\ nFORMAT. You need to specify a format character for each subsequent item.

Format character

% c: display character ASCII code% d,% I: display decimal integer% e,% E: display scientific counting value% f: display as floating point number% g,% G: display value in scientific counting or floating point form% s: display string% u: unsigned integer%: display% itself

Modifier

# [. #] the first number controls the width of the display; the second # indicates the precision after the decimal point,% 3.1f-left alignment (default right alignment)%-15s + the positive and negative sign% + d of the display value

Example:

Specifies that the output corresponding to output $1 is modified to be left-aligned, the output field width is 20 characters, and the $3 output field width is 10 characters, and line breaks

Awk-F:'{printf "%-20s d\ n", $1 recorder 3}'/ etc/passwd

Specify Username: followed by output $1, format left-aligned, output field width of 15 characters, UID: followed by output $3

Awk-F:'{printf "Username:%-15s Username% d\ n", $1 mine3}'/ etc/passwd

Operators in AWK

1. Arithmetic operator

Note: when the arithmetic run expression is typed, the variable does not need to be referenced by the $symbol, it is written directly.

X x%y-x: convert to negative number + x: convert a string to a numeric value

two。 Assignment operator

Note: pay attention to the difference between iSuppli + and + + I when typing and writing self-addition and self-subtraction assignment expressions

=, + =,-=, * =, / =,% =, ^ =, + +,-iBoth + assign values first, and then add themselves. for example, in a loop statement, the result of An is that the result of 0 quotation is 1, and then the value is assigned. for example, in the loop statement, the result of An is 1, and the result of An is 1

3. Comparison operator

Note: in the application, we need to pay attention to the input and writing of = and =, sometimes negligence may lead to writing errors.

=,!, >, > =

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report