AWK offers the most powerful features:
- Can be matched for regular expressions
- Style mount
- Flow control
- Mathematical operators
- Process Control Statements
- Built-in variables and functions
You can think of awk as a complete programming language, and it's incredibly fast to handle text. Now a lot of shell log analysis tools can be used to accomplish this. The design is simple and the speed is very good. Related to the above six aspects of content, I will be introduced in the future article. This time, basically, how to pass the external variables to the AWK execution statement.
First, the basis:
awk [-F re] [parameter ...] [' Pattern {action} '] [F Progfile] [In_file ...]
AWK general syntax as described above.
Such as:
Copy Code code as follows:
[Chengmo@localhost ~]$ Echo ' awk code ' | awk ' begin{print ' start\n============= '}{print $0}end{print ' =========\nend '} '
Start
=============
awk Code
=========
End
Two special expressions in awk, begin and end, both of which can be used in pattern (refer to the preceding awk syntax), providing the beginning and ending function to give the program an initial state and perform some cleanup work after the program is finished. Any actions listed after begin (within {}) will be executed before awk starts scanning input, and the operations listed after end will be executed after the input of the full section is scanned. Therefore, you typically use begin to display variables and preset (initialization) variables, using end to output the final result.
Second, the method of obtaining external variables
1. Get Ordinary external variables
Copy Code code as follows:
[Chengmo@localhost ~]$ test= ' awk code '
[Chengmo@localhost ~]$ Echo | awk ' {print test} ' test= ' $test '
awk Code
[Chengmo@localhost ~]$ Echo | awk test= "$test" ' {Print test} '
Awk:cmd. line:1: Fatal:cannot Open file ' {print test} ' for reading (No such file or directory)
Format such as: awk ' {action} ' variable name = variable value, so incoming variable, you can get the value in action. Note: The variable name and value are placed behind ' {action} '.
[Chengmo@localhost ~]$ Echo | awk ' begin{print test} ' test= "$test"
This variable is in: The action of begin cannot be obtained.
Variable in 2.BEGIN program block
Copy Code code as follows:
[Chengmo@localhost ~]$ test= ' awk code '
[Chengmo@localhost ~]$ Echo | Awk-v test= "$test" ' Begin{print test} '
awk Code
[Chengmo@localhost ~]$ Echo | Awk-v test= "$test" ' {Print test} '
awk Code
Format such as: AWK–V variable name = variable Value [–v variable 2= value 2 ...] ' Begin{action} ' NOTE: Using-v afferent variables can be obtained in the action of type 3, but in the order in front of the action.
3. Access to environment variables
Copy Code code as follows:
[Chengmo@localhost ~]$ awk ' begin{for (i in ENVIRON) {print I "=" environ[i];}} '
awkpath=.:/ Usr/share/awk
Ssh_askpass=/usr/libexec/openssh/gnome-ssh-askpass
Selinux_level_requested=
Selinux_role_requested=
Lang=en_us. UTF-8
.......
Just call: awk built-in variable ENVIRON, you can get the environment variables directly. It is a dictionary array. The environment variable name is its key value.