Awk array statistics, awk Array
Process the following file content, retrieve the domain name andDomain Name countSorting: (Baidu and sohu interview questions)
1 http://www.etiantian.org/index.html2 http://www.etiantian.org/1.html3 http://post.etiantian.org/index.html4 http://mp3.etiantian.org/index.html5 http://www.etiantian.org/3.html6 http://post.etiantian.org/2.html
Results:
mp3.etiantian.org 1post.etiantian.org 2www.etiantian.org 3
Ideas:
Retrieve Domain Name
Take the second column (Domain Name) with a slash as the kitchen knife)
Processing
Create an array
Use the second column (Domain Name) as the subscript of the array
Calculate the quantity in a way similar to I ++
Output results after statistics
1. view the files to be processed
1 [root@martin ~]# cat test.txt 2 http://www.etiantian.org/index.html3 http://www.etiantian.org/1.html4 http://post.etiantian.org/index.html5 http://mp3.etiantian.org/index.html6 http://www.etiantian.org/3.html7 http://post.etiantian.org/2.html
2. Take the diagonal line as the delimiter and retrieve the second column. + indicates continuous.
1 [root@martin ~]# awk -F "/+" '{print $2}' test.txt 2 www.etiantian.org3 www.etiantian.org4 post.etiantian.org5 mp3.etiantian.org6 www.etiantian.org7 post.etiantian.org
3. Create an array and perform statistics
1 [root @ martin ~] # Awk-F "/+" '{hotel [$2]} 'test.txt # create an array 2 [root @ martin ~] # Awk-F "/+" '{hotel [$2]; print $2}' test.txt # create an array and output the element name through print 3 www. etiantian. org4 www. etiantian. org5 post. etiantian. org6 mp3.etiantian. org7 www. etiantian. org8 post.etiantian.org
1 [root @ martin ~] # Awk-F "/+" '{hotel [$2] ++} 'test.txt # Count the array with the same underlying criteria 2 [root @ martin ~] # Awk-F "/+" '{hotel [$2] ++; print $2, hotel [$2]} 'test.txt # print the output element name and statistics 3 www.etiantian.org 14 www.etiantian.org 25 post.etiantian.org 16 mp3.etiantian.org 17 www.etiantian.org 38 post.etiantian.org 2
$2 represents the second column of each row and is a variable. hotel [$2] ++ is similar to I ++, just replace variable I with array hotel [$2]
4. After the statistics are complete, print the output Array Using the for loop. Different tables and corresponding statistics
1 [root@martin ~]# awk -F "/+" '{hotel[$2]++}END{for(pole in hotel) print pole,hotel[pole]}' test.txt2 mp3.etiantian.org 13 post.etiantian.org 24 www.etiantian.org 3
1. Optimize the display and format the output. 2 [root @ martin ~] # Awk-F "/+" '{hotel [$2] ++} END {for (pole in hotel) print pole, hotel [pole]} 'test.txt | sort-k2 | column-t3 mp3.etiantian.org 14 post.etiantian.org 25 www.etiantian.org 3
5. Count the linux system's history using the first 10 commands
1 [root@martin ~]# history|awk '{order[$2]++}END{for(n in order) print n,order[n]}'|sort -rnk2|head|column -t 2 awk 54 3 history|awk 44 4 [ 22 5 ll 19 6 rpm 12 7 yum 8 8 w 6 9 uname 610 history 611 /etc/rc.d/init.d/keepalived 5
This article refer to from "Li Dao's blog", the original address http://lidao.blog.51cto.com/3388056/1912219