当前位置:网站首页>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 .
边栏推荐
- [BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
- Conversion of yolov5 XML dataset to VOC dataset
- System disk expansion in virtual machine
- 2022 gas examination registration and free gas examination questions
- Ehrlich sieve + Euler sieve + interval sieve
- Codeforces Round #803 (Div. 2)(A-D)
- Educational Codeforces Round 119 (Rated for Div. 2)
- C#实现一个万物皆可排序的队列
- Educational Codeforces Round 115 (Rated for Div. 2)
- Awk from entry to soil (5) simple condition matching
猜你喜欢

Mouse over to change the transparency of web page image

es6总结

Take you to master the formatter of visual studio code

Call Baidu map to display the current position

ctfshow web255 web 256 web257

How to solve the problem that computers often flash

4 small ways to make your Tiktok video clearer

广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网

Codeforces Round #750 (Div. 2)(A,B,C,D,F1)

How to choose solid state hard disk and mechanical hard disk in computer
随机推荐
Internal learning
Group programming ladder race - exercise set l1-006 continuity factor
[CV] Wu Enda machine learning course notes | Chapter 9
1211 or chicken and rabbit in the same cage
[attack and defense world | WP] cat
Display Chinese characters according to numbers
SSRF vulnerability exploitation - attack redis
Add log file to slim frame - PHP
Turn: excellent managers focus not on mistakes, but on advantages
Live in a dream, only do things you don't say
如何通过antd的upload控件,将图片以文件流的形式发送给服务器
snipaste 方便的截图软件,可以复制在屏幕上
High order phase difference such as smear caused by myopic surgery
[untitled] 2022 polymerization process analysis and polymerization process simulation examination
Codeforces Round #803 (Div. 2)(A-D)
Flutter integrated amap_ flutter_ location
转:优秀的管理者,关注的不是错误,而是优势
Question 49: how to quickly determine the impact of IO latency on MySQL performance
What should I do if there is a problem with the graphics card screen on the computer
DM8 uses different databases to archive and recover after multiple failures