- awk的处理是以行为标准,并以输入为驱动的。比如文件test1为只有1行的文件:
[dhcc@bogon ~]$ awk '{print "hello world"}' test1
hello world
如果test1为3行的文件:
[dhcc@bogon ~]$ awk '{print "hello world"}' test1
hello world
hello world
hello world
- awk实际上是三段式分布:BEGIN-主循环体-END,BEGIN和END也是跳出输入驱动的唯一方法。
模式匹配是主循环的一部分,只有当pattern为真时,command才会执行。下面试一个小例子,用于判断字母,数字和空行。
/[0-9]/ {print "This is a number"}
/[a-zA-Z]/ {print "This is a letter"}
/^$/ {print "This is a blank line"}
执行结果
[dhcc@bogon ~]$ awk -f awkser
1
This is a number
+
This is a blank line
13
This is a number
a2
This is a number
This is a letter
- 关于字段分割的例子 -F后面紧跟分隔符,例如:
awk -F"\t" '{print $2,$3}' testsep
awk -F, '{print $2,$3}' testsep
浙公网安备 33010602011771号