Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get external variables by linux shell awk

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces linux shell awk how to obtain external variables, the article is very detailed, has a certain reference value, interested friends must read it!

AWK provides extremely powerful features:

Regular expressions can be matched

Style loading

Flow control

Mathematical operator

Process control statement

Built-in variables and functions

Think of awk as a complete programming language that processes text amazingly fast. Now many shell-based log analysis tools can be done with it. The design is simple and the speed performance is very good. Related to the above six aspects, I will introduce them in future articles. This time, I will mainly talk about how to pass external variables into the awk execution statement.

1. Fundamentals:

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

The general syntax of awk is as mentioned above.

Such as:

The code is 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, can be used in pattern (see the previous awk syntax). The purpose of providing BEGIN and END is to give the program an initial state and to perform some clean-up work after the program ends. Any actions listed after BEGIN (within {}) will be performed before awk starts scanning inputs, while actions listed after END will be performed after all inputs have been scanned. Therefore, BEGIN is usually used to display variables and preset (initialize) variables, and END is used to output the final result.

Second, the method of obtaining external variables

1. Get ordinary external variables

The code is 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 pass in the variable, and you can get the value in action. Note: the variable name and value are placed after'{action}'.

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

This variable is not available in the action of BEGIN.

Variables in 2.BEGIN block

The code is 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: variables passed in with-v are available in all types of action in 3, but in order before action.

3. Get environment variables

The code is 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

.

You can get the environment variables directly by calling the awk built-in variable ENVIRON. It is a dictionary array. The environment variable name is its key value.

The above is all the content of the article "how to get external variables from linux shell awk". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report