Linux Shell awk get an introduction to external variables (variable pass value) _linux Shell

Source: Internet
Author: User
AWK offers the most powerful features:
    1. Can be matched for regular expressions
    2. Style mount
    3. Flow control
    4. Mathematical operators
    5. Process Control Statements
    6. 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.

Related Article

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.