Linux shell awk get external variables (variable value) Introduction

  

mentioned here awk, I believe friends who write shell will be exposed. AWK is a programming language tool for working with text. AWK provides extremely powerful features: 1. Can match regular expressions 2. Style loading 3. Flow control 4. Math operators 5. Process control statements 6. Built-in variables and functions

Think of awk as a complete programming language, and the speed at which it processes text is amazingly fast. Many shell-based log analysis tools can now be used to do this. The design is simple and the speed is very good. I will cover the above six aspects, which I will introduce in future articles. This time, mainly, how to pass external variables into the awk execution statement.


First, the basics:

awk [ -F re] [parameter...] ['pattern {action}' ] [-f progfile][in_file. ..]

The general syntax of awk is as described above.

如:

[chengmo@localhost ~]$ echo 'awk code' |  Awk 'BEGIN{print "start\ ============="}{print $0}END{print "=========\ end"}'start =============aww code=========end


Two special expressions in awk, BEGIN and END Both of them can be used in pattern (refer to the previous awk syntax). The functions of providing BEGIN and END are to give the program an initial state and perform some finishing work after the program ends. Any operation listed after BEGIN (within {} will be executed before awk starts scanning input, and the operations listed after END will be executed after scanning the full input. Therefore, BEGIN is usually used to display variables and preset (initialize) variables, and END is used to output the final result.


Second, get the external variable method

1, get the ordinary external variable

[chengmo@localhost ~]$ test='awk code' [ ,null,null,3],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)


Formats such as: awk ‘{action}’ variable name=variable value, so that the variable can be passed in the action. Note: The variable name and value are placed after ’{action}& rsquo;.

[chengmo@localhost ~]$ echo |  Awk 'BEGIN{print test}' test="$test"


This variable is not available for BEGIN actions.

2.BEGIN variables in the program block

[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


Formats such as: awk –v variable name=variable value[–v variable 2=value 2 …] 'BEGIN{action}& rsquo; Note: Passing variables with -v can be obtained in actions of type 3, but in order before the action.


3.Get environment variables

[chengmo@localhost ~]$ awk 'BEGIN{for (i in ENVIRON) {print i"="ENVIRON[i ];}}'AWKPATH=.:/usr/share/awkSSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpassSELINUX_LEVEL_REQUESTED=SELINUX_ROLE_REQUESTED=LANG=en_US.UTF-8.......


You only need to call: awk built-in variable ENVIRON, you can get the environment variable directly. It is an array of dictionaries. The environment variable name is its key value.

Copyright © Windows knowledge All Rights Reserved