shell數組和awk數組

來源:互聯網
上載者:User

標籤:shell和awk數組

awk終於能入門了,所以整理了該文章,內容大多來自網上。


一、bash支援一維數組(不支援多維陣列),沒有限定數組的大小。在shell中,用括弧來表示數組,數組元素用空格符號分割開。類似於C語言,數組元素的下標由0開始編號。擷取數組中的元素要利用下標,下標可以是整數或算術運算式,其值應大於或等於0


1. 定義數組數組名array,元素a b c[[email protected]~]# array=(a b c)2.擷取所有元素[[email protected]~]# echo ${array[*]}a b c[[email protected]~]# echo ${array[@]}a b c3.擷取數組的長度[[email protected]~]# echo ${#array[*]}34.通過下標0 1 2依次擷取數組的每一個元素[[email protected]~]# echo ${array[0]}a[[email protected]~]# echo ${array[1]}b[[email protected]~]# echo ${array[2]}c5.擷取部分數組[[email protected]~]# echo ${array[*]:0:2}a b6.刪除第一個元素[[email protected]~]# unset array[0]7.刪除整個數組[[email protected]~]# unset array


小例子:

#!/bin/bash#刪除指定目錄下的檔案a=(/usr/local/tomcat/logs /home/user/tomcat/logs /usr/local/app/tomcat/logs)for i in "${a[@]}"do     find "$i" -maxdepth 1 -type f -name "*.txt"  ! -name "*.*" ! -mtime +30 -exec rm {} \;done


二、awk數組

awk的數組,一種關聯陣列(Associative Arrays),支援多維陣列,下標可以是數字和字串。因無需對數組名和元素提前聲明,也無需指定元素個數 ,所以awk的數組使用非常靈活。


1.建立數組

array[index]=value 數組名array,下標index以及相應的值value


2.讀取數組值

{for (item in array)  print array[item]} # 輸出的順序是隨機的{for(i=1;i<=len;i++)  print array[i]}    # len 是數組的長度


3.多維陣列,array[index1,index2,……]:SUBSEP是數組下標分割符。可以事先設定SUBSEP,也可以直接在SUBSEP的位置輸入你要用的分隔字元,如:

[[email protected]~]# awk ‘BEGIN{array["a","b"]=1;for(i in array) print i}‘a b[[email protected]~]# awk ‘BEGIN{SUBSEP=":";array["a","b"]=1;for(i in array) print i}‘a:b[[email protected]~]# awk ‘BEGIN{array["a"":""b"]=1;for(i in array) print i}‘a:b
[[email protected]~]# cat file A 192.168.1.1 HTTP B 192.168.1.2 HTTP B 192.168.1.2 MYSQL C 192.168.1.1 MYSQL C 192.168.1.1 MQ D 192.168.1.4 NGINX[[email protected]~]# awk ‘{a[$1"-"$2]++}END{for(i in a)print a[i],i}‘ file[[email protected]~]# awk ‘{SUBSEP="-"}{a[$1,$2]++}END{for(i in a) print a[i],i}‘ file2 B-192.168.1.21 D-192.168.1.42 C-192.168.1.11 A-192.168.1.1


4.刪除數組或數組元素,使用delete函數

delete array                 #刪除整個數組delete array[item]           #刪除某個數組元素(item)


5.排序:awk中的asort函數可以實現對數組的值進行排序,不過排序之後的數組下標改為從1到數組的長度。在gawk 3.1.2以後的版本還提供了一個asorti函數,這個函數不是依據關聯陣列的值,而是依據關聯陣列的下標排序,即asorti(array)以後,仍會用數字(1到數組長度)來作為下標,但是array的數組值變為排序後的原來的下標,除非你指定另一個參數如:asorti(a,b)。

[[email protected]~]# echo ‘aabbaabbcc‘ |awk ‘{a[$0]++}END{l=asorti(a);for(i=1;i<=l;i++)print a[i]}‘aabbcc [[email protected]~]# echo ‘aabbaabbcc‘ |awk ‘{a[$0]++}END{l=asorti(a,b);for(i=1;i<=l;i++)print b[i],a[b[i]]}‘aa 2bb 2cc 1


[[email protected]~]# echo "a10b2108100" |awk ‘{a[$0]=$0} #建立數組a,下標為$0,賦值也為$0END{len=asort(a)      #利用asort函數對數組a的值排序,同時獲得數組長度lenfor(i=1;i<=len;i++) print i "\t"a[i]  #列印}‘1       02       13       24       85       106       1007       a8       b


6.去重

[[email protected]~]# cat file 1213456[[email protected]~]# awk ‘a[$1]++‘ file1[[email protected]~]# awk ‘!a[$1]++‘ file123456


7.求和

[[email protected]~]# echo "aaa 1aaa 1ccc 1aaa 1bbb 1ccc 1" |awk ‘{a[$1]+=$2}END{for(i in a) print i,a[i]}‘aaa 3bbb 1ccc 2


8.通過split函數建立數組:數組的下標為從1開始的數字

split(s, a [, r]) # s:string, a:array name,[,r]:regular expression。
[[email protected]~]# echo ‘abcd‘ |awk ‘{len=split($0,a,"");for(i=1;i<=len;i++) print "a["i"] = " a[i];print "length = " len}‘a[1] = aa[2] = ba[3] = ca[4] = dlength = 4

 

求1月份相同名字和總和

[[email protected]~]# cat file Tom     2012-12-11      car     5       3000John    2013-01-13      bike    4       1000vivi    2013-01-18      car     4       2800Tom     2013-01-20      car     3       2500John    2013-01-28      bike    6       3500[[email protected]~]# awk ‘{split($2,a,"-");if(a[2]==01){b[$1]+=$5}}END{for(i in b)print i,b[i]}‘ file vivi 2800Tom 2500John 4500


9.求平均數

[[email protected]~]# cat file /circlelistbytjid,耗時:25ms/circlelistbytjid,耗時:24ms/circlelistbytjid,耗時:21ms/circlelistbytjid,耗時:13ms/circlelistbytjid,耗時:25ms/circlelistbytjid,耗時:13ms/circlelistbytjid,耗時:23ms/circlelistbytjid,耗時:24ms[[email protected]~]# awk -F: ‘{a+=+$2}END{print a/NR}‘ file21


10.求最大值

擷取數字欄位最大值

[[email protected]~]# cat file a b 1 c d 2 e f 3 g h 3 i j 2[[email protected]~]# awk ‘BEGIN{max=0}{if($3>max)max=$3}END{print max}‘ file3


列印第三欄位最大行

[[email protected]~]# awk ‘BEGIN{max=0}{a[$0]=$3;if($3>max)max=$3}END{for(v in a)if(a[v]==max)print v}‘ filee f 3 g h 3


11.合并file1和file2,除去重複項

[[email protected]~]#cat file1aaabbbcccddd[[email protected]~]#cat file2aaaeeedddfff[[email protected]~]# awk ‘NR==FNR{a[$0]=1;print}   #讀取file1,建立數組a,下標為$0,並賦值為1,然後列印NR>FNR{                   #讀取file2if(!(a[$0])) {print }      #如果file2 的$0不存在於數組a中,即不存在於file1,則列印。}‘ file1 file2aaabbbcccdddeeefff


提取檔案1中有,但檔案2中沒有:

[[email protected]~]# awk ‘NR==FNR{a[$0]=1}           #讀取file2,建立數組a,下標為$0,並賦值為1NR>FNR{                   #讀取file1if(!(a[$0])) {print }      #如果file1 的$0不存在於數組a中,即不存在於file2,則列印。}‘ file2 file1bbbccc


參考文章:http://bbs.chinaunix.net/thread-2312439-1-2.html


本文出自 “卡卡西” 部落格,請務必保留此出處http://whnba.blog.51cto.com/1215711/1891360

shell數組和awk數組

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.