Use of AWK
AWK

Syntax: awk-f/f/v/+' pattern '+ full path of the file.

Or: standard output | awk-f/f/v/+' mode'

Parameter meaning: -F specifies the separator of the field, such as the silent colon ":"

There are three ways to write it.

1.awk-f: the full path of the "pattern" file.

2.awk-f ":the full path of the" mode "file.

3. Full path of awk-f' [:\ t]' pattern' file # Here, single quotation marks and brackets are used to represent a set of separators, and each symbol in the set will be used as a separator to separate the current line.

-f specifies that the awk command is executed from the awk file.

Awk is a language with its own grammar. If reusing awk statement is complicated, it is recommended to write it to a file. It is recommended to use suffix at the end of the file. awk。

In the first line of the awk script file, write the location of the command interpreter, so write the strength of the awk interpreter here.

Which awk command does the terminal execute to obtain the location?

Generally /usr/bin/awk.

So, the file starts with #! /usr/bin/awk -f

Then write awk statement on the next line, save and exit.

Execute the awk -f script.awk file like this.

-v custom variable

awk-v var = " name " ' BEGIN { print var } '

It feels useless. If you want to define variables, you might as well define them directly in BEGIN.

Awk-f:' begin {var = "name"} {printvar} end {print "balabalaba"}' file.

Awk can use conditional judgment, for, do-while and other control statements.

Grammar is similar to C language, please see here if necessary.

Blogs. com/ Cheng Mo/archive/2010104/1842073.html.

Awk's print statement supports format control, similar to c,

Grammar: print "balabal%-6s balabalabala% d \ n", name and age.

%s, control string type output

%d, control shaping type output

%f, control floating-point type

%%, escaped as%.

Awk has an array data type.

Arrays, like variables, initialization statements are also declarations, with arr[110] = "AAA" and arr [Ni Hao] = 123.

It can be seen that awk's array is not really an array, but more like a dictionary.

Awk built-in variable

(1) FS: field separator) # Set the field separator character, which means replacing the previous content with a character.

(2) OFS: output field separator) # replaces the content of FS.

(3) RS: record separator) # Set a line break to change the content after one character to the next line.

(4) ORS: output record separator) # Replace line breaks with the contents of ORS.

(5) NF: Number of fields (number of fields)

Here (NF- 1) can get the value of the penultimate field.

(6) NR: record number) # line number.

(7) FNR: Calculate the "record number" by file (1). FNR indicates which line the current line is in different input streams. # When using the same AWk to process multiple files, correctly display the line numbers of the transferred lines in the files.

(8) File name: file name.

(9) ARGC: parameter account.

(10) ARGV: independent variable.