awk 传递参数

来源:互联网 发布:网络赚钱平台 编辑:程序博客网 时间:2024/05/22 01:53

今下午要写一个变态的数据统计脚本,用到awk,要传参数,等等吧。最近基本上转到Python上来写了,导致shell有点略往,写一下,加深下印象吧。

上代码

0> cat run.sh #!/bin/bash#######################################config 文件存放的是要统计的key#其中有且只能有两行,如有需求可以修改,同时要记住下面的awk脚本#第一行是all_item 所有的#第二行是根据需要,分到other的一些key#key之间的分割符是 " | "##其他配置项可以自行修改###########config info#################dir_path=`dirname $0`config_file="${dir_path}/config"log_file='/data0/logs/unistore/error.log'date_min=`date "+%Y-%m-%d %H:%M"`#date_min="2014-09-21 18:34"output_file="./output"#clear the output file :>${output_file}###########awk script #################awk -F'|' 'BEGIN{OFS="\t"}{        if(FILENAME=="'$config_file'")        {            if(NR==1)            {                for(i=1;i<=NF;i++)                {                    all_item[i]=$i;                    output[$i]=0;                }            }            if(NR==2)            {                for(i=1;i<=NF;i++)                    other_item[$i]=1;            }         }        if (FILENAME=="'$log_file'")        {            for(i=1;i<=length(a);i++)            {                key_str=all_item[i];                if(match($0,all_item[i])!=0 && match($0,"""'"$date_min"'""")!=0)                {                    output[key_str]++;                }            }        }    }END{        otheritem_value=0;        for(key in output)        {            if (other_item[key]!=1)                print key,output[key] >> "'$output_file'";            else                otheritem_value+=output[key];        }        print "other",otheritem_value >> "'$output_file'";    }' "${config_file}" "${log_file}"
配置文件

0> cat config icache get failed|storage splice get failed|test|123123123123icache get failed

着重的说一下,传递参数,有以下几种方式,我在脚本中用到前两种。

一:"'$var'" 外层双引号,内层是一对单引号

"'$log_file'"
这种写法大家无需改变用'括起awk程序的习惯,是老外常用的写法.如:
var="test"
awk 'BEGIN{print "'$var'"}'
这种写法其实际是双括号变为单括号的常量,传递给了awk.
如果var中含空格,为了shell不把空格作为分格符,便应该如下使用:
var="this is a test"
awk 'BEGIN{print "'"$var"'"}'

"'"$date_min"'"

二:export 变量,使用ENVIRON["var"]形式,
[root@test ~]# declare -x var="this is a test"[root@test ~]# awk 'BEGIN{print ENVIRON["var"]}'this is a test

三:当然也可以使用-v选项

[root@test ~]# var="this is a test"[root@test ~]# echo hello|awk -vnvar="$var" '{print nvar}'this is a test[root@test ~]# 
上面的脚本写的太二了,优化下

0> cat  awk_cond_count.sh #!/bin/bash#######################################config 文件存放的是要统计的key#其中有且只能有两行,如有需求可以修改,同时要记住下面的awk脚本#第一行是all_item 所有的#第二行是根据需要,分到other的一些key#key之间的分割符是 " | "##其他配置项可以自行修改###########config info#################dir_path=`dirname $0`config_file="${dir_path}/awk_cond_count.conf"log_file='/data0/logs/unistore/access.log'temp_file='./tmp_file'date_min=`date -d "-1 minute" "+%Y-%m-%d %H:%M"`#date_min="2014-09-21 18:34"output_file="./awk_cond_count.log"#clear the output file :>${output_file}tail -40000 ${log_file}|grep "${date_min}" > ${temp_file}###########awk script #################awk -F'|' 'BEGIN{OFS="\t"}{        if(FILENAME=="'$config_file'")        {            if(NR==1)            {                for(i=1;i<=NF;i++)                {                    all_item[i]=$i;                    output[$i]=0;                }            }            if(NR==2)            {                for(i=1;i<=NF;i++)                    other_item[$i]=1;            }         }        if (FILENAME=="'$temp_file'")        {            for(i=1;i<=length(all_item);i++)            {                key_str=all_item[i];                if(match($0,all_item[i])!=0)                {                    output[key_str]++;                }            }        }    }END{        otheritem_value=0;        for(key in output)        {            if (other_item[key]!=1)                print key,output[key] >> "'$output_file'";            else                otheritem_value+=output[key];        }        print "other",otheritem_value >> "'$output_file'";    }' "${config_file}" ${temp_file}


0 0
原创粉丝点击