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
- sed awk 传递参数
- sed awk 传递参数
- awk 传递参数
- 向awk传递参数
- awk 向命令传递参数
- [Linux] awk与shell参数传递
- shell和awk之间传递参数
- 向AWK脚本中传递参数
- awk向脚本传递参数(一)
- awk向脚本传递参数(二)
- [awk]awk 与 shell的参数传递(双向)
- 一日一省linux:awk向脚本传递参数
- awk与shell参数传递(或说变量传递)二三点
- awk与shell参数传递(或说变量传递)二三点
- shell awk 变量传递
- 将参数传递到awk内,用shell脚本提取符合条件的行
- shell & awk 变量传递+awk综合概要
- awk处理命令行参数
- 机器学习中的过拟合和防止过拟合
- PMTU 黑洞路由器
- CentOS上编译安装OpenCV-2.3.1与ffmpeg-2.1.2
- 游戏策划的力道在哪里
- tomcat
- awk 传递参数
- LVS配置NAT模式简单示例
- 两个队列实现一个栈-有错
- 给HTML<select>标签里的option添加双击事件
- Revit二次开发技术开放声明
- OpenCV基础篇之读取显示图片
- 第一周工作周报
- OpenCV基础篇之像素操作对比度调节
- DSP/BIOS使用之初窥门径——滴答时钟及烧写Flash