注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

随机过程

http://superware.blog.163.com

 
 
 

日志

 
 

Using awk  

2013-04-16 14:37:16|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
1、Data in examples

For example: the file text list below

64, bytes, from, 10.128.1.241: icmp_req=1 ttl=64 time=0.611 ms
64, bytes, from, 10.128.1.241: icmp_req=1 ttl=64 time=0.611 ms
64, bytes, from, 10.128.1.241: icmp_req=1 ttl=64 time=0.611 ms
64, bytes, from, 10.128.1.241: icmp_req=1 ttl=64 time=0.611 ms
64, bytes, from, 10.128.1.241: icmp_req=1 ttl=64 time=0.611 ms
64, bytes, from, 10.128.1.241: icmp_req=1 ttl=64 time=0.611 ms
64, bytes, from, 10.128.1.241: icmp_req=1 ttl=64 time=0.611 ms
64, bytes, from, 10.128.1.241: icmp_req=1 ttl=64 time=0.611 ms



2、Running awk

Two easy ways to run gawk
From the Command line

cat file | gawk ‘(pattern){action}’
cat file | gawk -f program.awk


From a script (recommended)
 

#!/usr/bin/gawk –f
# This is a comment
 
(pattern) {action}
 
...


3、Programming

The rules have two parts, a pattern and an action
If the input record matches the pattern, then the
action is applied

Awk divides the file into records and fields
(1)、Each line is a record (by default)
(2)、Fields are delimited by a special character
(3)、Whitespace by default
(4)、Can be change with
 
          “–F” (command line) or
 
           FS (special varaible)

awk -F ":" '{print $1} text
64 bytes from 10.128.1.241 64 bytes from 10.128.1.241 64 bytes from 10.128.1.241 64 bytes from 10.128.1.241 64 bytes from 10.128.1.241 64 bytes from 10.128.1.241 64 bytes from 10.128.1.241 64 bytes from 10.128.1.241


4、Records and fields

Fields are accessed with the ‘$’

(1)、$1 is the first field, $2 is the second...
(2)、$0 is a special field which is the entire line
(3)、NF is a special variable that is equal to the number of
  fields in the current record
(4)、NR is the index of record.

awk '{print NF}' text
8

8
8
8
8
8
8
8
8


awk '{print NR}' text


1

2

3

4

5

6

7

8


implies that there are 8 fields in each record


5、Variables in awk


Variables uses are naked
(1)、No need for declaration
(2)、Implicitly set to 0 AND Empty String
(3)、Combination of a floating-point and string
(4)、The variable is converted as needed
(5)、Based on it’s use
(6)、No matter what is in x you can always
             x=x+1
             length(x)

awk '/icmp_req/ {n = substr($7,6); printf (%s\n", n/100);} text
0.00611 0.00611 0.00611 0.00611 0.00611 0.00611 0.00611



6、Arrays in awk


All arrays in awk are associative

(1)、A[1] = “foo”;
(2)、B[“awk talk”] = “pizza”;

To check if there is an element in the array

(1)、Use “in”: If ( “awk talk” in B ) ...

Arrays can be sparse, they automatically resize, auto-initialize, and are fast (unless they get  huge)
Built in array iterator “in”

(1)、For ( x in myarray )


The arrays in awk can be used to implement almost any data structure
(1)、Set:

myset[“a”]=1; myset[“b”]=1;
if (“b” in myset)

(3)、Multi-dimensional array: 

  myarray[1,3] = 2; myarray[1,”happy”] = 3;

(4)、List:

mylist[1,”data”]=2; mylist[1,”next”] = 3;

awk '/icmp_seq/ {
n = int(substr($7,6)/10);

hist[n]++;} END {for(x in hist) printf(“%s: %s”, x*10, hist[x]);}
53: 1 62: 4 63: 1 55: 1 73: 1 64: 1 65: 1 56: 2

Patterns can be


(1)、Empty will print every line: {print $0}
(2)、Regular expression: (/regular expression/)
(3)、Boolean Expression: ($2==“foo” && $7==“bar”)
(4)、Range: ($2==“on” , $3==“off”)
(5)、Special: BEGIN and END




7、BEGIN and END Special Patterns

BEGIN and END are special patterns. They are not used to match input records. Rather, they are used for supplying start-up or clean-up information to your awk script.

A BEGIN rule is executed, once, before the first input record has been read.
A END rule is executed, once, after all the input hase been read.

For example:

awk 'BEGIN {print "Analysis of 'icmp_req'"}
/icmp_req/ {++n_icmp_req}
END {print "'icmp_req' appears " n_icmp_req " times."}' text

Analysis of 'icmp_req'
'icmp_req' appears 8 times.


So that the whole statement can be seperated into 3 phases:

BEGIN phase: 
BEGIN {print "Analysis of 'icmp_req'"}
action phase:
/icmp_req/ {++n_icmp_req}
END phase:
 END {print "'icmp_req' appears " n_icmp_req " times."}
  评论这张
 
阅读(99)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018