awk

Section 1: 經常使用運維awk命令html

 

 

統計tomcat每秒的帶寬(字節),最大的排在最後面數組

#cat localhost_access_log.txt | awk '{ bytes[$5] += $NF; }; END{for(time in bytes) print   bytes[time] " " time}'  | sort -ntomcat

 

統計某一秒的帶寬數據結構

#grep "18:07:34" localhost_access_log.txt |awk '{ bytes += $NF; } END{ print  bytes }'運維

 

 

統計指定ip.txt中ip在local_access.txt中出現的次數函數

#cat ip.txt //內容以下this

12.3.4.5htm

12.3.4.6排序

12.3.4.7ip

12.3.4.8

 

#cat local_access.txt

19:23:35  /a.html   12.3.4.5

19:23:35  /b.html   12.3.4.5

19:23:35  /c.html   12.3.4.6

19:23:35  /d.html   12.3.4.7

19:23:35  /a.html   12.3.4.9

19:23:35  /b.html   12.3.4.9

19:23:35  /c.html   12.3.4.9

 

#awk -F " " '{if (NR==FNR) {arr1[$1]=1} else{arr2[$3]++;}} END{for(ip in arr1){print ip,arr2[ip]}}' ip.txt local_access.txt

12.3.4.5 2

12.3.4.6 1

12.3.4.7 1

12.3.4.8

 

 

 

Section 2: $0,$1,$2,$NF,$(NF-1)的使用

 

 

$0整個當前行, $1當前行的第一個域   $NF爲最後一個域   $(NF-1)爲倒數第二個

#echo "a b c d e" |awk '{print $1; print $2; print $(NF-1);print $NF;print $0}'

a                 //對應第1個域

b                 //對應第2個域

d                 //對應$(NF-1),對應倒數第二個域

e                 //對應$NF,最後一個域

a b c d e         //對應$0

 

 

Section 3: print, printf用法

 

#awk 'BEGIN{a=1;b="213";print "output "a","b;}'

output 1,213

 

 

#awk 'BEGIN{a=1;b="213";print "output",a,","b;}'

output 1 ,213

 

printf的使用

#awk 'BEGIN{a=1;b="213";printf("output %d,%s\n",a,b)}'

output 1,213

 

 

Section 4: 選擇分隔符

 

awk默認是按照空格來分割, NF表示域的個數

#echo "a:b c,d" |awk '{print $1; print $2; print NF}'

a:b

c,d

2

 

 

根據":",空格,","來進行分割

#echo "a:b c,d" |awk -F " |,|:" '{print $1; print $2; print NF}'

a

b

4

 

 

Section 5: BEGIN,END用法

 

abc.txt內容以下:

first lady

second boy

third child

 

 

#cat abc.txt |awk 'BEGIN {print "begin process"} {print "process 1 "$1} {print "process 2 "$2} END { print " the end"}'

換行後以下:

 

#cat abc.txt |awk -F " " '

         'BEGIN {print "begin process"}     //在開頭的時候執行一次

               {print "process 1 "$1}       //每一行執行一次

               {print "process 2 "$2}       //每一行執行一次

          END  { print " the end"}'         //最後的時候執行一次

 

輸出以下

begin process     

process 1 first         // {print "process 1 "$1}  執行了一次

process 2 lady          // {print "process 2 "$2}  執行了一次

process 1 second

process 2 boy

process 1 third

process 2 child

 the end

 

 

沒有BEGIN,只有END的狀況

#cat abc.txt |awk '{print "begin process"} {print "process 1 "$1} {print "process 2 "$2} END { print " the end"}'

  格式化語句以下

#cat abc.txt |awk -F ":"

  '{print "begin process"}          //由於沒有BEGIN  因此這個每一行都會執行

   {print "process 1 "$1}           //每一行都會執行

   {print "process 2 "$2}           //每一行都會執行

    END { print " the end"}'        //最後執行一次

   

輸出以下:   

begin process

process 1 first

process 2 lady

begin process

process 1 second

process 2 boy

begin process

process 1 third

process 2 child

 the end

 

 

Section 6: 數組使用

 

awk中數據結構使用, 數組也能夠理解爲map

 

#awk 'BEGIN{array1["a"]=1;array1[2]="213";print array1["a"],array1[2]}'

1 213

 

year.txt中內容以下 

 

2016:09 1    //表示2016年9月,有一個訪問

2016:06 1

2016:06 1

2016:01 1

2015:01 1

2014:01 1

2015:01 1

2016:02 1

 

 

下面語句是把每一個月的訪問量相加,排序後輸出

#awk  '{bytes[$1]+=$2}  END { for(time in bytes) print bytes[time],time}' year.txt |sort -n

 

展開以下

#awk

    '{bytes[$1]+=$2}             //bytes爲數組,下標是時間,value是訪問量

      END {

          for(time in bytes) print bytes[time], time

      }'

      year.txt |sort -n

 

 

輸出的內容以下;   bytes是一個數組,下標是字符串""上面用數組,下標能夠是數字,也能夠是字符串

1 2014:01

1 2016:01

1 2016:02

1 2016:09

2 2015:01

2 2016:06

 

 

#awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";if(tB["c"]!="1"){print "no found";};for(k in tB){print k,tB[k];}}'

 

展開是以下

#awk 'BEGIN{

          tB["a"]="a1";

          tB["b"]="b1";

          if(tB["c"]!="1"){           //這個地方會判斷在裏面,可是會往tB佔用一個值

              print "no found";

          };

          for(k in tB){

              print k,tB[k];

          }

       }'

 

輸出以下:

no found

a a1

b b1

c                   很奇怪,  「c」沒有賦值,循環的時候,就發如今裏面了,這個裏面有反作用

 

 

要修改這個點,須要使用以下

#awk 'BEGIN {

         tB["a"]="a1";

         tB["b"]="b1";

         if ("c" in tB) {                      //用這個來進行判斷,就沒有負做用

             print "c is in tB";

         }

         for(k in tB){

             print k,tB[k];

         }

      }'

 

 

 

awk的多維數組

 

#awk 'BEGIN{ for(i=1;i<=3;i++) {for(j=1;j<=3;j++) {tarr[i,j]=i*j;print i,"*",j,"=",tarr[i,j]}}}'

 

展開後以下:

#awk 'BEGIN{

          for(i=1;i<=3;i++) {

              for(j=1;j<=3;j++) {

                  tarr[i,j]=i*j;

                  print i,"*",j,"=",tarr[i,j]

              }

          }

      }'

 

輸出以下:

1 * 1 = 1

1 * 2 = 2

1 * 3 = 3

2 * 1 = 2

2 * 2 = 4

2 * 3 = 6

3 * 1 = 3

3 * 2 = 6

3 * 3 = 9

 

 

 

awk多維數組的in判斷

 

awk 'BEGIN{ tarr[1,3]=5;if ((1,3) in tarr) print "1,3 in"; if ((4,4) in tarr) print "4,4 in"}'

 

#awk 'BEGIN{

          tarr[1,3]=5;

          if ((1,3) in tarr)          //直接使用(1,3)來判斷in語句

              print "1,3 in";

          if ((4,4) in tarr)

              print "4,4 in"}'

 

 

Section 7: for語句使用

 

#awk 'BEGIN{array1["a"]=1;array1["c"]=3;array1["b"]=2;for(index1 in array1) print index1,array1[index1]}'

 

展開以下:

#awk 'BEGIN{

          array1["a"]=1;

          array1["c"]=3;

          array1["b"]=2;

          for(index1 in array1)

              print index1,array1[index1]

      }'

 

 

輸出以下:

a 1

b 2

c 3

 

 

for也能夠使用k=1;k<=3;k++的形式

 

#awk 'BEGIN{array1[1]="a";array1[3]="c";array1[2]="b";len=length(array1);for(k=1;k<=len;k++) print k,array1[k]}'

 

展開以下:

#awk 'BEGIN{

          array1[1]="a";

          array1[3]="c";

          array1[2]="b";

          len=length(array1);        //獲得數組的長度

          for(k=1;k<=len;k++)

              print k,array1[k]

      }'

 

輸出以下

1 a

2 b

3 c

 

 

Section 8: 內置函數使用

 

int函數,把字符串轉爲整數

#awk 'BEGIN {print int("12.9")}'        返回一個整數

12

 

 

index函數 

#awk 'BEGIN {print index("12.9343",".")}'     //index方法返回"."在"12.9343的位置,沒有找到則返回0

3

 

 

length函數    獲得數組的長度,字符串長度

#awk 'BEGIN{array1["a"]=1;array1["b"]=2;print length(array1)}'

輸出以下:

2

 

#awk 'BEGIN{a="123";print length(a)}'   獲得字符串長度

3

 

 

match函數,   檢測info中是否含有"te" 若是有,則返回"te"第一次出現的位置,若是沒有則返回0

 

#awk 'BEGIN {info="is is test"; print match(info,"te");}'

 

 

rand函數   生成隨機數   可是事實上是不隨機的

 

#awk 'BEGIN {print rand " " rand}'    rand會生成一個0-1的數字

 

0.840188 0.394383         //每次運行第一個,第二個都是這個數字

 

 

split函數  按照某個分隔符,對字符串進行分割

 

split按照" "對"it is a test"進行切割,切割後的內容放在thearray中 返回的是split後,thearray的元素個數,

 

#awk 'BEGIN {print split("it is a test",thearray," "); print thearray[1]}'

4                    //split後返回數組的長度

it                   //打印第一個元素

 

 

sub函數   替換

#awk 'BEGIN {info="this a test"; sub("a","b",info); print info }'   把info中"a"用"b"替代

 

this b test

 

 

substr函數   獲得子字符串

substr(s, m, n)     s是要截取的字符串,m是開始點,從1開始,   n是要截取的長度

 

#awk 'BEGIN {print substr("12.9343",2,4)}'      //substr

2.93

 

 

toupper函數   字符串轉爲大寫

#awk 'BEGIN {info="this a test"; print toupper(info);}'

THIS A TEST

 

 

tolower函數   字符串轉爲消協

#awk 'BEGIN {info="thIS A TEST"; print tolower(info);}'

this a test

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息