当前位置:网站首页>awk从入门到入土(4)用户自定义变量
awk从入门到入土(4)用户自定义变量
2022-07-04 08:38:00 【奇妙之二进制】
无类型
BEGIN{}{ num = 100; # 先存个整数 print("num is: "num); num = 3.1415926; # 再来存个浮点数 print("num is: "num); num = "I'm 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变量就像所有其他脚本语言那样是无类型的,可以存储任意类型的值,并且可以随时进行改变,后面这点区别于支持类型推断的语言(如:C#,定义时虽然不用指定类型,但是一旦赋值后类型就固定了)。
无需定义
BEGIN{}{ print("var is:"var); # 我擦嘞,这直接就用上了}END{}
$echo “”|awk -f chapter_2_2-2.awk
var is:
变量随用随写,即使使用没初始化过的变量也不会报错。
全局性
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中所有的变量,无论是出现在BEGIN中的、BODY中的、END中的,抑或是后面要提到的function中的,通通都是全局的,也就是说只要变量名字相同,无论在任何地方做出了修改,其值都会延续到下一段执行的代码行上。这是变量使用中特别要注意的地方,否则就可能引发各种莫名其妙的问题。
传参变量
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
变量除可以在程序中进行赋值外也可以在命令行调用的时候进行,如上面例程,x和i的值都是以命令行参数传入,通过这种参数传入的方式就扩大了通过脚本程序的适用性。
边栏推荐
- Snipaste convenient screenshot software, which can be copied on the screen
- Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid
- Webapi interview question summary 01
- Show server status on Web page (on or off) - PHP
- Unity-写入Word
- 如何通过antd的upload控件,将图片以文件流的形式发送给服务器
- Li Kou today's question -1200 Minimum absolute difference
- NewH3C——ACL
- Redis sentinel mechanism
- How to set multiple selecteditems on a list box- c#
猜你喜欢
L1 regularization and L2 regularization
埃氏筛+欧拉筛+区间筛
Leetcode topic [array] -136- numbers that appear only once
Educational Codeforces Round 119 (Rated for Div. 2)
Developers really review CSDN question and answer function, and there are many improvements~
DM database password policy and login restriction settings
广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
NewH3C——ACL
Wechat has new functions, and the test is started again
What does range mean in PHP
随机推荐
Scanf read in data type symbol table
埃氏筛+欧拉筛+区间筛
Educational Codeforces Round 119 (Rated for Div. 2)
Call Baidu map to display the current position
Convert datetime string to datetime - C in the original time zone
Three paradigms of database design
[performance test] read JMeter
[go basics] 2 - go basic sentences
Basic operations of databases and tables ----- view data tables
How to set multiple selecteditems on a list box- c#
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Sort by item from the list within the list - C #
How to solve the problem of computer jam and slow down
Unity-Text上标平方表示形式+text判断文本是否为空
Cancel ctrl+alt+delete when starting up
What sparks can applet container technology collide with IOT
Snipaste convenient screenshot software, which can be copied on the screen
一文了解数据异常值检测方法
Do you know about autorl in intensive learning? A summary of articles written by more than ten scholars including Oxford University and Google
广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网