1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
1.脚本:
[root@oldboy ~]
# cat test
wang 4
cui 3
zhao 4
liu 3
liu 3
chang 5
li 2
[root@oldboy ~]
#
问题
1 通过第一个域找出字符长度为4的 知识点:(length函数)
[root@oldboy ~]
# awk '{if(length($1)=="4"){print $0}}' test
wang 4
zhao 4
[root@oldboy ~]
#
[root@oldboy ~]
# awk '{len=length($1);if(len=="4"){print $0}}' test
wang 4
zhao 4
[root@oldboy ~]
#
2. 将文档中 liu 字符串替换为 hong 知识点 gsub(
/r/
,
"s"
,域)
[root@oldboy ~]
# awk '{gsub(/liu/,"hong",$1);print $0}' test
wang 4
cui 3
zhao 4
hong 3
hong 3
chang 5
li 2
[root@oldboy ~]
#
3.第二列求和
[root@oldboy ~]
# awk 'BEGIN{sum=0}{sum=sum+$2}END{print sum}' test
24
[root@oldboy ~]
# echo "4+3+4+6+7"|bc
24
[root@oldboy ~]
#
4.第二列求平均值 双变量 或者运用内置变量NR
[root@oldboy ~]
# awk 'BEGIN{sum=0;i=0}{sum=sum+$2;i++}END{print sum/i}' test
3.42857
[root@oldboy ~]
# awk 'BEGIN{sum=0}{sum=sum+$2}END{print sum/NR}' test
3.42857
[root@oldboy ~]
#
5.第二列求最大值 和最初的一个值进行比较
[root@oldboy ~]
# awk 'BEGIN{a=0}{if($2>a){a=$2}}END{print a}' test
5
[root@oldboy ~]
#
6. 去重统计
[root@oldboy ~]
# awk '{arry[$1]++}END{for(i in arry){print arry[i],i}}' test|sort -nr
2 liu
1 zhao
1 wang
1 li
1 cui
1 chang
[root@oldboy ~]
#
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
脚本:
[root@oldboy ~]
# cat test02
zhangsan 80
lisi 81.5
wangwu 93
zhangsan 85
lisi 88
wangwu 97
zhangsan 90
lisi 92
wangwu 88
[root@oldboy ~]
#
7.(求每人的平均值)
反思a数组主要用于 第一列的唯一表示 累加
b 数组用于统计每个第一列的个数
key 就是数组内的值
a[key] 就是key 多对应的第几列的值
[root@oldboy ~]
# awk '{a[$1]+=$2;b[$1]++}END{for(key in a){print key,a[key]/b[key]}}' test02
zhangsan 85
lisi 87.1667
wangwu 92.6667
[root@oldboy ~]
# awk '{a[$1]+=$2;b[$1]++}END{for(key in a){print key,a[key]}}' test02
zhangsan 255
lisi 261.5
wangwu 278
[root@oldboy ~]
# awk '{a[$1]+=$2;b[$1]++}END{for(key in a){print b[key]}}' test02
3
3
3
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
8.
awk
增加表标题和行(主要用来统计)
[root@oldboy ~]
# awk 'BEGIN{FS=":";print "name\tshell"}{print $1"\t"$7}END{print "blue,/bin/bash"}' /etc/passwd
name shell
root
/bin/bash
bin
/sbin/nologin
daemon
/sbin/nologin
adm
/sbin/nologin
lp
/sbin/nologin
sync
/bin/sync
shutdown
/sbin/shutdown
halt
/sbin/halt
9.搜索哪一个关键字
[root@oldboy ~]
# awk 'BEGIN{FS=":"}/root/' /etc/passwd
root:x:0:0:root:
/root
:
/bin/bash
operator:x:11:0:operator:
/root
:
/sbin/nologin
[root@oldboy ~]
#
10.搜索以什么开头的行
[root@oldboy ~]
# awk '/^root/' /etc/passwd
root:x:0:0:root:
/root
:
/bin/bash
[root@oldboy ~]
#
11.搜索以多个开头的行
[root@oldboy ~]
# awk '/^(sshd|root)/' /etc/passwd
root:x:0:0:root:
/root
:
/bin/bash
sshd:x:74:74:Privilege-separated SSH:
/var/empty/sshd
:
/sbin/nologin
[root@oldboy ~]
#
12.搜索以字母开头的行
[root@oldboy ~]
# awk '/^[rd]/' /etc/passwd
root:x:0:0:root:
/root
:
/bin/bash
daemon:x:2:2:daemon:
/sbin
:
/sbin/nologin
dbus:x:81:81:System message bus:/:
/sbin/nologin
[root@oldboy ~]
#
13.条件判断
[root@oldboy ~]
# awk 'BEGIN{FS=":"}{if($2=="x"&&$3<=10){print $0}}' /etc/passwd
root:x:0:0:root:
/root
:
/bin/bash
bin:x:1:1:bin:
/bin
:
/sbin/nologin
daemon:x:2:2:daemon:
/sbin
:
/sbin/nologin
adm:x:3:4:adm:
/var/adm
:
/sbin/nologin
lp:x:4:7:lp:
/var/spool/lpd
:
/sbin/nologin
sync
:x:5:0:
sync
:
/sbin
:
/bin/sync
shutdown
:x:6:0:
shutdown
:
/sbin
:
/sbin/shutdown
halt:x:7:0:halt:
/sbin
:
/sbin/halt
mail:x:8:12:mail:
/var/spool/mail
:
/sbin/nologin
uucp:x:10:14:uucp:
/var/spool/uucp
:
/sbin/nologin
[root@oldboy ~]
#
14.统计一个文件的行数
[root@oldboy ~]
# awk 'BEGIN{sum=0}{sum++}END{print sum}' /etc/passwd
24
[root@oldboy ~]
#
15.统计一个文件夹下的字节数
[root@oldboy ~]
# ls -l |awk 'BEGIN{size=0}{size+=$5}END{print size}'
29479
[root@oldboy ~]
#
16.length
[root@oldboy ~]
# awk 'BEGIN{FS=":"}{len=length($5);if(len>4){print $0}}' /etc/passwd
daemon:x:2:2:daemon:
/sbin
:
/sbin/nologin
shutdown
:x:6:0:
shutdown
:
/sbin
:
/sbin/shutdown
operator:x:11:0:operator:
/root
:
/sbin/nologin
games:x:12:100:games:
/usr/games
:
/sbin/nologin
gopher:x:13:30:gopher:
/var/gopher
:
/sbin/nologin
ftp
:x:14:50:FTP User:
/var/ftp
:
/sbin/nologin
nobody:x:99:99:Nobody:/:
/sbin/nologin
dbus:x:81:81:System message bus:/:
/sbin/nologin
vcsa:x:69:69:virtual console memory owner:
/dev
:
/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:
/sbin/nologin
saslauth:x:499:76:Saslauthd user:
/var/empty/saslauth
:
/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:
/var/empty/sshd
:
/sbin/nologin
[root@oldboy ~]
#
17.查看系统的端口号:
[root@oldboy ~]
# awk -F "[ /]+" '$1~/^(ftp|ssh|https|mysql|ssh)$/{print $1,$2}' /etc/services |uniq
ftp
21
ssh
22
https 443
mysql 3306
ftp
21
ssh
22
[root@oldboy ~]
#
18.求1-100之间的和
[root@oldboy ~]
# seq 100|awk '{sum+=$1}END{print sum}'
5050
19.求PATH路径中命令为三个字母的个数
[root@oldboy ~]
# find `echo $PATH|tr ":" " "` -type f -name "???"|awk -F "/" '{i++;print $4}END{print "总的个数为:" i}'>>command.txt
20.统计日志的IP的个数,并统计每个ip的个数
[root@oldboy ~]
# awk -F "[ ]+" '{arry[$1]++}END{for(key in arry){print arry[key],key}}' access_awk.log |sort -nr|head -n 10
179 112.64.171.98
114 49.70.160.35
108 180.157.41.101
98 124.79.108.144
97 58.220.223.62
90 61.170.130.208
73 116.25.32.13
72 218.79.64.76
62 115.228.107.174
61 180.154.238.125
[root@oldboy ~]
#
21.统计单词和字母的个数
用shell处理以下内容
1、按单词出现频率降序排序!
2、按字母出现频率降序排序!
源数据:
the squid project provides a number of resources to assist
users
design,implement and support squid installations. Please browse the documentation and support sections
for
more
infomation
[root@oldboy ~]
# egrep -o "[a-Z]+" work |awk '{a[$1]++}END{for(key in a){print key,a[key]}}' |sort -nrk2
the 2
support 2
squid 2
and 2
users
1
to 1
sections 1
resources 1
[root@oldboy ~]
# egrep -o "[a-Z]" work |awk '{a[$1]++}END{for(key in a){print key,a[key]}}' |sort -nrk2
s 19
e 17
o 16
t 14
n 12
i 12
r 11
a 9
|
本文转自 小小三郎1 51CTO博客,原文链接:http://blog.51cto.com/wsxxsl/1832197,如需转载请自行联系原作者