标签:
awk使用ARGC和ARGV来在命令行中传入一些变量到awk脚本中。其中:
fengxi@ubuntu:~/bash/awk$ cat arguments.awk BEGIN { print "ARGC=", ARGC for(i=0;i<ARGC;i++) print ARGV[i] } fengxi@ubuntu:~/bash/awk$ awk -f arguments.awk arg1 arg2 ARGC= 3 awk arg1 arg2在gawk中,当前正在处理的文件是存储在ARGV数组中,该数组可在循环块中被访问。ARGIND是访问ARGV数组的下标去检索当前文件。当你在awk脚本中仅仅处理一个文件时,ARGIND的值为1,并且ARGV[ARGIND]会返回当前正在处理的文件名。
{ print "ARGIND:", ARGIND print "Current file:", ARGV[ARGIND] }
fengxi@ubuntu:~/bash/awk$ awk -f argind.awk num.txt ARGIND: 1 Current file: num.txt ARGIND: 1 Current file: num.txt ARGIND: 1 Current file: num.txt ARGIND: 1 Current file: num.txt ARGIND: 1 Current file: num.txt其中num.txt的内容为:
fengxi@ubuntu:~/bash/awk$ cat num.txt 1 2 3 4 5如果传入不是一个文件,而是两个文件,则输出结果为:
fengxi@ubuntu:~/bash/awk$ awk -f argind.awk num.txt num2.txt ARGIND: 1 Current file: num.txt ARGIND: 1 Current file: num.txt ARGIND: 1 Current file: num.txt ARGIND: 1 Current file: num.txt ARGIND: 1 Current file: num.txt ARGIND: 2 Current file: num2.txt ARGIND: 2 Current file: num2.txtnum2.txt的内容为:
fengxi@ubuntu:~/bash/awk$ cat num2.txt 1 2
标签:
原文地址:http://blog.csdn.net/xibeichengf/article/details/51346974