awk - 数据提取和报告工具 (2) 进阶

  • 原创
  • Madman
  • /
  • /
  • 0
  • 8005 次阅读

awk - 数据提取和报告工具 (2) 进阶-min.png

Synopsis: 详细描述了awk编程语言的基础语法,其中操作符和正则表达式组合后,即为awk中的匹配模式。action是一系列的执行语句,一般包括赋值语句、条件语句、循环语句、输入/输出语句等。awk数组也叫关联数组,其下标既可以是数字也可以是字符串,awk提供了逻辑上模拟二维数组的访问方式,其本质上还是一维数组。可以将数据重定向到一个文件,重定向出现在print或printf语句之后。awk中的重定向与shell命令中的重定向一样,只是它们写在awk程序中。要输出美观的报告,printf格式化怎么少得了

awk系列:


1. 操作符

1.1 算术运算符

1. 加
# awk 'BEGIN { a = 50; b = 20; print "(a + b) = ", (a + b) }'
(a + b) =  70

2. 减
# awk 'BEGIN { a = 50; b = 20; print "(a - b) = ", (a - b) }'
(a - b) =  30

3. 乘
# awk 'BEGIN { a = 50; b = 20; print "(a * b) = ", (a * b) }'
(a * b) =  1000

4. 除
# awk 'BEGIN { a = 50; b = 20; print "(a / b) = ", (a / b) }'
(a / b) =  2.5

5. 余数
# awk 'BEGIN { a = 50; b = 20; print "(a % b) = ", (a % b) }'
(a % b) =  10

6. 指数
# awk 'BEGIN { a = 10; a = a ^ 2; print "a =", a }'
a = 100
或者:
# awk 'BEGIN { a = 10; a = a**2; print "a =", a }'
a = 100

1.2 递增和递减运算符

1. 先自增,再赋值
# awk 'BEGIN { a = 10; b = ++a; printf "a = %d, b = %d\n", a, b }'
a = 11, b = 11

2. 先自减,再赋值
# awk 'BEGIN { a = 10; b = --a; printf "a = %d, b = %d\n", a, b }'
a = 9, b = 9

3. 先赋值,再自增
# awk 'BEGIN { a = 10; b = a++; printf "a = %d, b = %d\n", a, b }'
a = 11, b = 10

4. 先赋值,再自减
# awk 'BEGIN { a = 10; b = a--; printf "a = %d, b = %d\n", a, b }'
a = 9, b = 10

1.3 复合赋值运算符

1. 简单赋值
# awk 'BEGIN { name = "Jerry"; print "My name is", name }'
My name is Jerry

2. 左边变量加上右边的值,再赋值给左边的变量
# awk 'BEGIN { cnt = 10; cnt += 10; print "Counter =", cnt }'
Counter = 20

3. 左边变量减去右边的值,再赋值给左边的变量
# awk 'BEGIN { cnt = 100; cnt -= 10; print "Counter =", cnt }'
Counter = 90

4. 左边变量乘以右边的值,再赋值给左边的变量
# awk 'BEGIN { cnt = 10; cnt *= 10; print "Counter =", cnt }'
Counter = 100

5. 左边变量除以右边的值,再赋值给左边的变量
# awk 'BEGIN { cnt = 100; cnt /= 5; print "Counter =", cnt }'
Counter = 20

6. 左边变量与右边的值求余数,再赋值给左边的变量
# awk 'BEGIN { cnt = 100; cnt %= 8; print "Counter =", cnt }'
Counter = 4

7. 左边变量为底数,右边的值为指数,求出指数,再赋值给左边的变量
# awk 'BEGIN { cnt = 2; cnt ^= 4; print "Counter =", cnt }'
Counter = 16
或者:
# awk 'BEGIN { cnt = 2; cnt **= 4; print "Counter =", cnt }'
Counter = 16

1.4 关系运算符

表达式成立时返回true,执行后续的命令;否则返回false,不执行后续的命令

# awk 'BEGIN { a = 10; b = 10; if (a == b) print "a == b" }'
a == b
# awk 'BEGIN { a = 10; b = 20; if (a != b) print "a != b" }'
a != b
# awk 'BEGIN { a = 10; b = 20; if (a < b) print "a  < b" }'
a  < b
# awk 'BEGIN { a = 10; b = 10; if (a <= b) print "a <= b" }'
a <= b
# awk 'BEGIN { a = 10; b = 20; if (b > a ) print "b > a" }'
b > a
# awk 'BEGIN { a = 10; b = 10; if (b >= a ) print "b >= a" }'
b >= a

1.5 逻辑运算符

1. expr1 && expr2 逻辑与,同时为真时结果才为真。也叫短路与,当且仅当expr1为真时,才验证expr2
# awk 'BEGIN { num = 5; if (num >= 0 && num <= 7) printf "%d is in octal format\n", num }'
5 is in octal format

2. expr1 || expr2 逻辑或,只要一个为真结果就为真。也叫短路或,当且仅当expr1为假时,才验证expr2
# awk 'BEGIN { ch = "\n"; if (ch == " " || ch == "\t" || ch == "\n") print "Current character is whitespace." }'
Current character is whitespace.

3. ! expr1 逻辑非,取反,当expr1为真时,结果为0;当expr1为假时,结果为1。说明: awk中0代表false,非0代表true
# awk 'BEGIN { name = ""; if (! length(name)) print "name is empty string." }'
name is empty string.

1.6 三元运算符

语法为condition expression ? statement1 : statement2,如果表达式返回真,则执行statement1,否则执行statement2

# awk 'BEGIN { a = 10; b = 20; (a > b) ? max = a : max = b; print "Max =", max}'
Max = 20

1.7 正负号

负数的负数是正数

# awk 'BEGIN { a = -10; a = +a; print "a =", a }'
a = -10
# awk 'BEGIN { a = -10; a = -a; print "a =", a }'
a = 10

1.8 字符串连接运算符

空格是合并两个字符串的字符串连接运算符。

# awk 'BEGIN { str1 = "Hello, "; str2 = "World"; str3 = str1 str2; print str3 }'
Hello, World

1.9 数组成员操作符


                                
                            
分类: Linux
标签: awk gawk GNU
  • I give you a gift berukoperty.blogspot.com Ry
  • Win dollars click here geroivsedom.blogspot.com OJ
  • Your prize pick up here tutdomirabot.blogspot.com 24
  • Take the money geroivsedom.blogspot.com bu
  • Your prize pick up here tutdomirabot.blogspot.com Wy
  • Take the money berukoperty.blogspot.com DM
  • Take the money tutdomirabot.blogspot.com sb
  • Take your earnings geroivsedom.blogspot.com 26
  • Win dollars click here berukoperty.blogspot.com QU
  • Take your earnings tutdomirabot.blogspot.com sr
  • Win dollars click here melkvkladvbudesh.blogspot.com bu
  • Take your earnings geroivsedom.blogspot.com xd
  • Win dollars click here tutdomirabot.blogspot.com EW
  • OCkdKsBUPyPByK
  • I give you a gift berukoperty.blogspot.com P4
  • Take the money geroivsedom.blogspot.com WT
  • Take the money tutdomirabot.blogspot.com UF
  • Take the money tutdomirabot.blogspot.com FL
  • Your prize pick up here melkvkladvbudesh.blogspot.com Xw
  • Take the money tutdomirabot.blogspot.com Q7
  • Win dollars click here tutdomirabot.blogspot.com wN
  • Take the money geroivsedom.blogspot.com 3S
  • Take your earnings tutdomirabot.blogspot.com uv
  • Take the money geroivsedom.blogspot.com UE
未经允许不得转载: LIFE & SHARE - 王颜公子 » awk - 数据提取和报告工具 (2) 进阶

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列