当前位置:网站首页>Awk from getting started to digging in (4) user defined variables
Awk from getting started to digging in (4) user defined variables
2022-07-04 08:46:00 【Wonderful binary】
No type
BEGIN{}{ num = 100; # Save an integer first print("num is: "num); num = 3.1415926; # Save another floating point number print("num is: "num); num = "I'm string."; # Finally, look at the string print("num is:"num);}END{}
$echo “” |awk -f chapter_2_2-1.awk
num is: 100
num is: 3.14159
num is:I’m string.
awk Variables, like all other scripting languages, are untyped , You can store any type of value , And can be changed at any time , The latter is different from languages that support type inference ( Such as :C#, Although there is no need to specify the type when defining , But once assigned, the type is fixed ).
There is no need to define
BEGIN{}{ print("var is:"var); # I wipe it up. , This is directly used }END{}
$echo “”|awk -f chapter_2_2-2.awk
var is:
Variable write as you use , Even if you use uninitialized variables, there will be no error .
Overall
BEGIN{ print("i in begin: "i);}{ ++i; print("i in body: "i);}END{ print("i in end: "i);}
$cat chapter_2_2-3.awk |awk -f chapter_2_2-3.awk
i in begin:
i in body: 1
i in body: 2
i in body: 3
i in body: 4
i in body: 5
i in body: 6
i in body: 7
i in body: 8
i in body: 9
i in body: 10
i in end: 10
awk All variables in , Whether it is in BEGIN Medium 、BODY Medium 、END Medium , Or what will be mentioned later function Medium , It's all about the overall situation , That is, as long as the variable name is the same , No matter where it is modified , Its value will continue to the next executed code line . This is what we should pay special attention to when using variables , Otherwise, it may cause all kinds of inexplicable problems .
Transfer parameter variable
BEGIN{ print(x); print("i in begin: "i);}{ ++i; print("i in body: "i);}END{ print("i in end: "i);}
$cat chapter_2_2-4.awk |awk -v x=“I’m X” -v i=100 -f chapter_2_2-4.awk
I’m X
i in begin: 100
i in body: 101
i in body: 102
i in body: 103
i in body: 104
i in body: 105
i in body: 106
i in body: 107
i in body: 108
i in body: 109
i in body: 110
i in body: 111
i in end: 111
Variables can be assigned in the program or called on the command line , As the above routine ,x and i The values of are all passed in as command line parameters , This way of parameter passing expands the applicability of scripting .
边栏推荐
- DM8 uses different databases to archive and recover after multiple failures
- Awk from entry to earth (12) awk can also write scripts to replace the shell
- How can we make a monthly income of more than 10000? We media people with low income come and have a look
- Technology sharing | MySQL parallel DDL
- Codeforces Round #803 (Div. 2)(A-D)
- 力扣今日题-1200. 最小绝对差
- Manjaro install wechat
- @Role of requestparam annotation
- ctfshow web255 web 256 web257
- 没有Kubernetes怎么玩Dapr?
猜你喜欢

C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码

SSRF vulnerability exploitation - attack redis

Take you to master the formatter of visual studio code

Codeforces Round #793 (Div. 2)(A-D)

Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14

How does Xiaobai buy a suitable notebook

Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid

The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)

Awk from entry to earth (12) awk can also write scripts to replace the shell

地平线 旭日X3 PI (一)首次开机细节
随机推荐
Use preg_ Match extracts the string into the array between: & | people PHP
Webapi interview question summary 01
Parallel shift does not provide any acceleration - C #
DM8 tablespace backup and recovery
没有Kubernetes怎么玩Dapr?
微服务入门:Gateway网关
Newh3c - network address translation (NAT)
Codeforces Round #793 (Div. 2)(A-D)
awk从入门到入土(12)awk也可以写脚本,替代shell
MySQL relearn 1-centos install mysql5.7
团体程序设计天梯赛-练习集 L1-006 连续因子
SSRF vulnerability exploitation - attack redis
Getting started with microservices: gateway gateway
转:优秀的管理者,关注的不是错误,而是优势
到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
[attack and defense world | WP] cat
DM database password policy and login restriction settings
ES6 summary
What should I do if there is a problem with the graphics card screen on the computer
地平线 旭日X3 PI (一)首次开机细节