Shell arrays and awk arrays

Source: Internet
Author: User
Tags array length

Awk was finally able to get started, so he tidied up the article, mostly from the Internet.


First, bash supports one-dimensional arrays (which do not support multidimensional arrays) and does not limit the size of arrays. In the shell, the array is represented by parentheses, and the array elements are separated by a space symbol. similar to the C language, the subscript of an array element is numbered starting with 0. Gets the elements in the array to take advantage of subscript, the subscript can be an integer or an arithmetic expression whose value should be greater than or equal to 0


1. Definition array array name array, element a B c[[email protected]~]# array= (a b C) 2. Get all elements [email protected]~]# echo ${array[*]}a b c[[email prote cted]~]# Echo ${array[@]}a b c3. Gets the length of the array [[email protected]~]# echo ${#array [*]}34. Get each element of an array in order by subscript 0 1 2 [email protected] ~]# echo ${array[0]}a[[email protected]~]# echo ${array[1]}b[[email protected]~]# echo ${array[2]}c5. Get a partial array [email protected]~]# Echo ${array[*]:0:2}a b6. Delete the first element [[email protected]~]# unset array[0]7. Delete entire array [email protected]~]# unset Array


Small example:

#!/bin/bash# Delete the file under the specified directory 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 {} \;d one


Second, awk array

An array of awk, an associative array (associative Arrays), supports multidimensional arrays, and subscripts can be numbers and strings. Awk uses a very flexible array because it does not need to be declared in advance of the array name and elements, nor does it need to specify the number of elements.


1. Creating an array

Array[index]=value array name, subscript index, and the corresponding value


2. Reading array values

{for (item in array) print Array[item]} # The order of the output is random {for (i=1;i<=len;i++) print Array[i]} # len is the length of the array


3. Multidimensional arrays, Array[index1,index2,......] : Subsep is an array subscript separator. You can set subsep in advance, or you can enter the delimiter you want to use directly in the SUBSEP location, such as:

[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]~]# AW K ' {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 an array or array element, using the delete function

Delete array #删除整个数组delete Array[item] #删除某个数组元素 (item)


5. Sort: The Asort function in awk can be used to sort the values of an array, but the subscript after the order is changed from 1 to the length of the array. Later versions of Gawk 3.1.2 also provide a asorti function, which is not based on the value of the associative array, but rather on the subscript ordering of the associative array, that is, after the Asorti (array), the number (1 to the array length) is used as the subscript, However, the array value of the array becomes the original subscript after sorting, unless you specify another parameter such as 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, subscript $ A, assignment is $0end{len=asort (a) #利用asort函数对数组a的值排序, while obtaining       Array length lenfor (i=1;i<=len;i++) print i "\ t" a[i] #打印} ' 1 Geneva 106 1007 A8 B


6. Go to the heavy

[Email protected]~]# cat file 1213456[[email protected]~]# awk ' a[$1]++ ' file1[[email protected]~]# awk '!a[$1]++ ' file12 3456


7. Summation

[[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. Create an array from the Split function: The array's subscript is a number starting from 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];p rint "length = "Len} ' a[1] = aa[2] = ba[3] = ca[4] = Dlength = 4

January same name and sum

[[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. Averaging

[Email protected]~]# cat File/circlelistbytjid, Time: 25ms/circlelistbytjid, Time: 24ms/circlelistbytjid, Time: 21ms/ Circlelistbytjid, Time: 13ms/circlelistbytjid, Time: 25ms/circlelistbytjid, Time: 13ms/circlelistbytjid, Time: 23ms/ Circlelistbytjid, time consuming: 24ms[[email protected]~]# awk-f: ' {a+=+$2}end{print a/nr} ' File21


10. Ask for maximum value

Get Number field Maximum value

[[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}en D{print Max} ' file3


Print the third field maximum row

[[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. Merging file1 and file2, removing duplicates

[Email protected]~] #cat file1aaabbbcccddd[[email protected]~] #cat file2aaaeeedddfff[[email protected]~]# awk ' NR== Fnr{a[$0]=1;print} #读取file1, set up array A, subscript $ A, and assign a value of 1, and then Print nr>fnr{#读取file2if (! A[$0]) {print} #如果file2 the $ $ does not exist in array A, i.e. it does not exist in File1. } ' File1 file2aaabbbcccdddeeefff


Extract file 1 has, but not in file 2:

[Email protected]~]# awk ' nr==fnr{a[$0]=1} #读取file2, set up array A, subscript $ A, and assign a value of 1nr>fnr{#读取file1if (! A[$0]) {print} #如果file1 the $ $ does not exist in array A, i.e. it does not exist in File2. } ' File2 FILE1BBBCCC


Reference article: http://bbs.chinaunix.net/thread-2312439-1-2.html


This article is from "Kaka West" blog, please be sure to keep this source http://whnba.blog.51cto.com/1215711/1891360

Shell arrays and awk arrays

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.