Mawk of Linux commands

Source: Internet
Author: User
Tags function definition

Pattern scanning and Text processing language


Grammar:

Mawk [-F value] [-v var=value] [--] ' program text ' [File ...]

Mawk [-F value] [-v var=value] [-F program-file] [--] [file ...]


Describe:

Awk is a text-processing language, and Mawk is an interpreter for that language.

The awk program consists of a sequence of pattern{action} and a function definition. The input data is divided into individual records according to the RS (record seperator, default = ' \ n '), each record is compared to pattern, and if matched, the corresponding action is executed.


Options:

-F

Set field Seperator,fs to divide the record into field according to the value

-F

awk Program Files

-V Var=value

Set Program variables

--

Indicates the end of the option description for the Mawk command


The following describes the awk language

1, program structure

As mentioned earlier, the AWK program consists of a sequence of pattern{action} or a function definition.

Where the pattern can be

BEGIN

END

Expression

expression, expression

If the action is omitted, the implicit execution of print, if the pattern is omitted, is implied to match correctly.


The statement ends with a line break and a semicolon.

Use # to Annotate


Control flow includes

if (expr) statement

if (expr) statement Else statement

while (expr) statement

Do statement while (expr)

for (opt_expr; opt_expr; opt_expr) statement

for (var in array) statement

Continue

Break


2, data type, conversion and comparison

There are two types of numeric type and string type, where all numbers are represented by floating-point number and the calculation is done.


3, Regular expression

Expr ~/r/

The regular expression is surrounded by a slash, and if expr obeys the regular expression, it is true, otherwise it is false. Disobedience with "! ~ "indicates.

/r/{action} is equivalent to $ ~/r/{action}.


4,records and Fields

Read one line at a time, that is, a records, and according to FS divided into fields,$0 represents the entire records,$1,$2,..., $NF respectively represents the corresponding field, built-in variable NF is the total number of fields, the fields above NF is set to ""

NR and Fnr each increase by 1, the meaning after the description of the text.

Assigning values to the built-in variables will cause the related variables to change.


5, Expressions and operators

Most of the operators in the C language are identical, special have in (array membership), ~ (! ~,matching), $ (field).


6, array

AWK provides a one-dimensional array, using a[1] or a["1" to access the element

Delete array[expr] deletes the corresponding element

if ((i, J) in A) print A[i, j]


7, built-in variables

CONVFMT: Numeric to string internal conversion format, default = "%.6g"

ENVIRON: An array of environment variables, var=value is stored as Environ[var]=value

FILENAME: The name of the current input file

FNR: The current record ordinal in the filename file

Fs:field delimiter, which can be a regular expression

NF: The total number of field records currently logged

NR: Record number in all input streams

OFMT: Output numeric format, default to "%.6g"

OFS: Field delimiter used for output, default to Space

ORS: Record delimiter at output, default to line break

RS: Record delimiter at input, default to line break

Subsep:used to build multiple array subscripts, default to "\034"


8, built-in function

(1) String processing function

Gsub (R, S, T) Gsub (R, s)

Replace the character of R in the T variable with S, and if T is not specified, it is implied as $ A. Finally, the replacement quantity is returned.

Index (s, t)

Returns the position of the first occurrence of T in S, otherwise the first character of the returned 0,s is labeled 1

Length (s)

Returns the length of S

Match (S,R)

Returns the longest matching subscript for R in S, no match returns 0.

Split (S,a,r) split (S,a)

According to R, the S is divided into a, and the number of fields is returned, if R is not set, FS is used

sprintf (format, expr-list)

Constructs a string based on format

Sub (r,s,t) sub (r,s)

Only one replacement is completed

substr (s,i,n) substr (s,i)

Returns a substring of s from I to length n

ToLower (s)

ToUpper (s)

(2) Numeric processing function

ATAN2 (y,x)

COS (x)

EXP (x)

int (x)

Log (x)

RAND ()

Sin (x)

sqrt ()

Srand (expr) Srand ()


9, input and output

Output with print and printf

Print

Output $

Print expr1, Expr2, ..., exprn

printf format, expr-list


Input as Getline

Getline

Read in line to $

Getline < file

Read from File

Getline var

Read the next record to Var

Getline var < file

Reading the next record from file to Var

Command | Getline

Executes the command and reads a record from the pipeline to $

Command | Getline var

Executes the command and reads a record from the pipeline to Var

When Getline encounters End-of-file, returns 0, error returns 1, returns 1 correctly


Close (expr)

Close file or pipe that is associated with expr

Fflush (expr)


System (expr) executes expr and returns the status


10, user-defined function

Syntax for

Function name (args) {statements}

Can contain return opt_expr


11, split string, record, file


12, multi-line Records processing

13, program execution

The begin is performed first

Each record is then processed individually according to the Middle Pattern{action}

which can be achieved by

Next

Exit opt_expr

Change the order of the program execution of the pattern level, touch next to read the next record, and continue execution after the begin; Exit immediately calls end to the actions,opt_expr of the exit code.

Final execution End


Mawk of Linux commands

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.