Chinaunix首页 | 论坛 | 博客
  • 博客访问: 166787
  • 博文数量: 21
  • 博客积分: 2371
  • 博客等级: 大尉
  • 技术积分: 217
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-15 18:36
文章分类

全部博文(21)

文章存档

2010年(6)

2009年(15)

分类: LINUX

2009-09-11 13:23:23

awk自定义函数基本格式为,参数arg1、arg2也可无。函数在何时定义并无要求,可以在BEGIN块前,也可以在END块后;除了在主语句块中,何时都可以。
BEGIN{
...}
 
function My_fun (arg1,arg2) {
print arg1
print arg2
}
 
{
主语句
...
My_fun(5,6)
}
 
END{
..}
 
调用方式为 My_fun(5,6)
--------------我是分隔符--------------
 
举一例说明自定义函数的写法,主要是为了说明在shell命令行下写的时候,以及写在awk脚本中的时候需注意的一些格式问题,本例没有用到参数。
本例来自:
要求打印ip对应的访问时长

IP地址 访问时间 访问时长
211.103.220.197 28/Aug/2009:16:55:14-28/Aug/2009:16:56:22 1分8秒

 
数据文件file

211.103.220.197 - - [28/Aug/2009:16:55:14 +0800] 'GET /web/index.action HTTP/1.1' 200 20816 0.719
211.103.220.197 - - [28/Aug/2009:16:55:26 +0800] 'GET /web/dyanmicStat.action?time=16:58:06 HTTP/1.1' 200 9 0.043
211.103.220.197 - - [28/Aug/2009:16:55:27 +0800] 'GET /web/dyanmicStat.action?time=16:58:08 HTTP/1.1' 200 9 0.044
211.103.220.197 - - [28/Aug/2009:16:55:32 +0800] 'GET /web/dyanmicStat.action?time=16:58:10 HTTP/1.1' 200 9 0.033
211.103.220.197 - - [28/Aug/2009:16:55:32 +0800] 'GET /web/dyanmicStat.action?time=16:58:13 HTTP/1.1' 200 9 0.024
211.103.220.197 - - [28/Aug/2009:16:55:37 +0800] 'GET /web/dyanmicStat.action?time=16:58:15 HTTP/1.1' 200 9 0.027
211.103.220.197 - - [28/Aug/2009:16:55:37 +0800] 'GET /web/dyanmicStat.action?time=16:58:17 HTTP/1.1' 200 9 0.023
211.103.220.197 - - [28/Aug/2009:16:55:38 +0800] 'GET /web/dyanmicStat.action?time=16:58:19 HTTP/1.1' 200 9 0.024
211.103.220.197 - - [28/Aug/2009:16:55:40 +0800] 'GET /web/dyanmicStat.action?time=16:58:21 HTTP/1.1' 200 9 0.140
211.103.220.197 - - [28/Aug/2009:16:55:42 +0800] 'GET /web/dyanmicStat.action?time=16:58:23 HTTP/1.1' 200 9 0.044
211.103.220.197 - - [28/Aug/2009:16:55:44 +0800] 'GET /web/dyanmicStat.action?time=16:58:25 HTTP/1.1' 200 9 0.038
211.103.220.197 - - [28/Aug/2009:16:55:46 +0800] 'GET /web/dyanmicStat.action?time=16:58:27 HTTP/1.1' 200 9 0.028
211.103.220.197 - - [28/Aug/2009:16:55:48 +0800] 'GET /web/dyanmicStat.action?time=16:58:29 HTTP/1.1' 200 9 0.022
211.103.220.197 - - [28/Aug/2009:16:55:50 +0800] 'GET /web/dyanmicStat.action?time=16:58:31 HTTP/1.1' 200 9 0.019
211.103.220.197 - - [28/Aug/2009:16:55:52 +0800] 'GET /web/dyanmicStat.action?time=16:58:33 HTTP/1.1' 200 9 0.019
211.103.220.197 - - [28/Aug/2009:16:55:54 +0800] 'GET /web/dyanmicStat.action?time=16:58:35 HTTP/1.1' 200 9 0.019
211.103.220.197 - - [28/Aug/2009:16:55:56 +0800] 'GET /web/dyanmicStat.action?time=16:58:37 HTTP/1.1' 200 9 0.019
211.103.220.197 - - [28/Aug/2009:16:55:57 +0800] 'GET /web/dyanmicStat.action?time=16:58:39 HTTP/1.1' 200 9 0.020
211.103.220.197 - - [28/Aug/2009:16:56:00 +0800] 'GET /web/dyanmicStat.action?time=16:58:41 HTTP/1.1' 200 9 0.167
211.103.220.197 - - [28/Aug/2009:16:56:02 +0800] 'GET /web/dyanmicStat.action?time=16:58:43 HTTP/1.1' 200 9 0.019
211.103.220.197 - - [28/Aug/2009:16:56:04 +0800] 'GET /web/dyanmicStat.action?time=16:58:45 HTTP/1.1' 200 9 0.698
211.103.220.197 - - [28/Aug/2009:16:56:06 +0800] 'GET /web/dyanmicStat.action?time=16:58:47 HTTP/1.1' 200 9 0.019
211.103.220.197 - - [28/Aug/2009:16:56:09 +0800] 'GET /web/dyanmicStat.action?time=16:58:49 HTTP/1.1' 200 9 0.019
211.103.220.197 - - [28/Aug/2009:16:56:10 +0800] 'GET /web/dyanmicStat.action?time=16:58:51 HTTP/1.1' 200 9 0.022
211.103.220.197 - - [28/Aug/2009:16:56:12 +0800] 'GET /web/dyanmicStat.action?time=16:58:53 HTTP/1.1' 200 9 0.031
211.103.220.197 - - [28/Aug/2009:16:56:14 +0800] 'GET /web/dyanmicStat.action?time=16:58:55 HTTP/1.1' 200 9 0.020
211.103.220.197 - - [28/Aug/2009:16:56:16 +0800] 'GET /web/dyanmicStat.action?time=16:58:57 HTTP/1.1' 200 9 0.019
211.103.220.197 - - [28/Aug/2009:16:56:18 +0800] 'GET /web/dyanmicStat.action?time=16:58:59 HTTP/1.1' 200 9 0.019
211.103.220.197 - - [28/Aug/2009:16:56:20 +0800] 'GET /web/dyanmicStat.action?time=16:59:01 HTTP/1.1' 200 9 0.019
211.103.220.197 - - [28/Aug/2009:16:56:22 +0800] 'GET /web/dyanmicStat.action?time=16:59:03 HTTP/1.1' 200 9 0.019
58.33.241.108 - - [29/Aug/2009:17:26:11 +0800] 'GET /web/index.action HTTP/1.1' 200 20845 0.120
58.33.241.108 - - [29/Aug/2009:17:26:14 +0800] 'GET /web/dyanmicStat.action?time=17:28:53 HTTP/1.1' 200 9 0.016
58.33.241.108 - - [29/Aug/2009:17:26:16 +0800] 'GET /web/dyanmicStat.action?time=17:28:55 HTTP/1.1' 200 9 0.017
58.33.241.108 - - [29/Aug/2009:17:26:18 +0800] 'GET /web/dyanmicStat.action?time=17:28:57 HTTP/1.1' 200 9 0.017
58.33.241.108 - - [29/Aug/2009:17:26:20 +0800] 'GET /web/dyanmicStat.action?time=17:28:59 HTTP/1.1' 200 9 0.018
58.33.241.108 - - [29/Aug/2009:17:26:22 +0800] 'GET /web/dyanmicStat.action?time=17:29:01 HTTP/1.1' 200 9 0.016
58.33.241.108 - - [29/Aug/2009:17:26:24 +0800] 'GET /web/dyanmicStat.action?time=17:29:03 HTTP/1.1' 200 9 0.017
58.33.241.108 - - [29/Aug/2009:17:26:26 +0800] 'GET /web/dyanmicStat.action?time=17:29:05 HTTP/1.1' 200 9 0.017
58.33.241.108 - - [29/Aug/2009:17:26:28 +0800] 'GET /web/dyanmicStat.action?time=17:29:07 HTTP/1.1' 200 9 0.017
58.33.241.108 - - [29/Aug/2009:17:26:30 +0800] 'GET /web/dyanmicStat.action?time=17:29:09 HTTP/1.1' 200 9 0.017
58.33.241.108 - - [29/Aug/2009:17:26:32 +0800] 'GET /web/dyanmicStat.action?time=17:29:11 HTTP/1.1' 200 9 0.017
58.33.241.108 - - [29/Aug/2009:17:26:34 +0800] 'GET /web/ngoIndex.action HTTP/1.1' 200 12862 1.114
58.33.241.108 - - [29/Aug/2009:17:27:09 +0800] 'GET /web/queryngobyname.action?name=%E8%A5%BF%E8%97%8F%E7%BD%91 HTTP/1.1' 200 6285 16.879
58.33.241.108 - - [29/Aug/2009:17:27:54 +0800] 'GET /web/querymorengo.action?type=4 HTTP/1.1' 200 5489 0.101
58.33.241.108 - - [29/Aug/2009:17:34:01 +0800] 'GET /web/minyun.jsp HTTP/1.1' 200 1492 0.001

直接在命令行中的处理

awk 'BEGIN{FS=" +|[[]";print "IP地址\t\t\t访问时间\t\t\t\t访问时长"}function cal(){split(s_time,M,":");split(e_time,N,":");time=(N[2]-M[2])*60*60+(N[3]-M[3])*60+N[4]-M[4];if (ip) print ip"\t"s_time"-"e_time"\t"int(time/60)"分"time%60"秒"}$1!=ip{cal();ip=$1;s_time=$5}{e_time=$5}END{cal()}' file

 

写到awk脚本中,调用方法为awk -f my.awk file

awk脚本my.awk

BEGIN {
   FS=" +|[[]"
   print "IP地址\t\t\t访问时间\t\t\t\t访问时长"
      }
{ if ($1!=ip) {cal();ip=$1;s_time=$5}
  e_time=$5
}
END {
     cal()
    }
function cal () {
   split(s_time,M,":")
   split(e_time,N,":")
   time=(N[2]-M[2])*60*60+(N[3]-M[3])*60+N[4]-M[4]
   if (ip) print ip"\t"s_time"-"e_time"\t"int(time/60)"分"time%60"秒"
               }

 

值得注意的是,在awk脚本中,BEGIN、END这两者后的{必须与之在同行,不能换行写,否则就是语法错误。而自定义函数function后的{则没有这个要求,如下面这样也是可以的。

function cal ()

{

cmd..

}

阅读(4165) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~