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.
Enterprise customers install SCOM2012 behind the constantly generated alarms, performance, informat
First install CentOS6 to install rp-pppoe this software, the previous version of centos is installed
This article mainly introduces the DB2 database in the inux installation process detailed, the step
First, download the magent download address: http://pan.baidu.com/s/1eRygtEUmkdir -p /usr/local/mag
Introduction to hashlimit module in iptables
Linux configuration and deployment of the Flex development environment eclipse
Expired file causes VCS dual machine to fail to start
Those powerful commands that improve the efficiency of Linux environment development
vmware linux system with Internet settings
How to make your Linux use more like Windows
Can you use online banking even under Linux?
Linux part of the system management command usage summary
Dual system format C disk reinstall windows 7 repair xp startup items
Win7 64-bit Ultimate System boot prompts to configure Windows update failure solution
Tune Windows 7: No need to restart after the system is updated
Remove the Skydrive shortcut tutorial in the Win8.1 navigation pane
How to recover the built-in application that Win10 system is uninstalled?
Windows 8 network is limited? Get the IP address to get
Win10 major update Redstone and new features: OneDrive file placeholder return to
Win7 prohibits the installation of software, win7 restrictions on the installation of software