awk 练习笔记
#awk 练习笔记
#参考:http://man.linuxde.net/awk
#awk格式
1
|
awk 'BEGIN{ print "start" } pattern{ commands } END{ print "end" }' file
|
一个awk脚本通常由:BEGIN语句块、能够使用模式匹配的通用语句块、END语句块3部分组成,这三个部分是可选的。
任意一个部分都可以不出现在脚本中,脚本通常是被单引号或双引号中
●第一步:执行BEGIN{ commands }语句块中的语句;
●第二步:从文件或标准输入(stdin)读取一行,然后执行pattern{ commands }语句块,它逐行扫描文件,从第一行到最后一行重复这个过程,直到文件全部被读取完毕。
●第三步:当读至输入流末尾时,执行END{ commands }语句块。
BEGIN语句块在awk开始从输入流中读取行之前被执行,这是一个可选的语句块,比如变量初始化、打印输出表格的表头等语句通常可以写在BEGIN语句块中。
END语句块在awk从输入流中读取完所有的行之后即被执行,比如打印所有行的分析结果这类信息汇总都是在END语句块中完成,它也是一个可选语句块。
pattern语句块中的通用命令是最重要的部分,它也是可选的。如果没有提供pattern语句块,则默认执行{ print },即打印每一个读取到的行,awk读取的每一行都会执行该语句块。
1
2
3
4
5
|
[[email protected] ~] # echo -e "A line 1\nA line 2" | awk 'BEGIN{print "Start"} {print } END{print "End"}'
Start A line 1 A line 2 End |
1
2
3
4
|
[[email protected] ~] # echo -e "line1 f2 f3\nline2 f4 f5 f6\nline3 f7" | awk '{print "Line No."NR", No of fields:"NF,"$0="$0}'
Line No.1, No of fields:3 $0=line1 f2 f3 Line No.2, No of fields:4 $0=line2 f4 f5 f6 Line No.3, No of fields:2 $0=line3 f7 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[[email protected] ~] # echo -e "f1 f2\nf3 f4 f5\nf6"
f1 f2 f3 f4 f5 f6 [[email protected] ~] # echo -e "f1 f2\nf3 f4 f5\nf6" |awk '{print $(NF)}'
f2 f5 f6 [[email protected] ~] # echo -e "f1 f2\nf3 f4 f5\nf6" |awk '{print $(NF-1)}'
f1 f4 f6 统计行数 [[email protected] ~] # wc -l /etc/passwd
28 /etc/passwd
[[email protected] ~] # awk 'END{print NR}' /etc/passwd
28 |
运算符
图片内容来自:http://man.linuxde.net/awk
注:求余为 %
1
2
3
4
5
6
7
8
9
10
|
[[email protected] ~] # awk 'BEGIN{a=3;b=5;print a+b;}'
8 [[email protected] ~] # awk 'BEGIN{a=3;b=5;print a*b;}'
15 [[email protected] ~] # awk 'BEGIN{a=3;b=5;print a^b;}'
243 [[email protected] ~] # awk 'BEGIN{a=3;b=5;a++;b--;print a,b;}'
4 4 [[email protected] ~] # awk 'BEGIN{a=3;b=5;print b%a}'
2 |
1
2
3
4
|
[[email protected] ~] # cat -n /etc/passwd | awk 'NR>=3 && NR<=5{print}'
3 daemon:x:2:2:daemon: /sbin : /sbin/nologin
4 adm:x:3:4:adm: /var/adm : /sbin/nologin
5 lp:x:4:7:lp: /var/spool/lpd : /sbin/nologin
|
1
2
3
|
[[email protected] ~] # cat /etc/passwd |awk '{if($0 ~ /root/){print}}'
root:x:0:0:root: /root : /bin/bash
operator:x:11:0:operator: /root : /sbin/nologin
|
1
2
3
4
|
[[email protected] ~] # awk 'BEGIN{a=1; b=2; c=b<a?a:b; print c}'
2 [[email protected] ~] # echo "A B C D" |awk '{print $1"~"$2"~"$3"~"$4}'
A~B~C~D |
#next用法 :跳过当前行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[[email protected] ~] # cat test.txt
web01[192.168.2.100] httpd ok tomcat ok sendmail ok web02[192.168.2.101] httpd ok postfix ok web03[192.168.2.102] mysqld ok httpd ok [[email protected] ~] # cat test.txt | awk '{if($0 ~ /[\]]/){T=$0;next;}{print T":"$0}}'
web01[192.168.2.100] :httpd ok tomcat ok web01[192.168.2.100] :sendmail ok web02[192.168.2.101] :httpd ok web02[192.168.2.101] :postfix ok web03[192.168.2.102] :mysqld ok web03[192.168.2.102] :httpd ok [[email protected] ~] #
|
#getline简单用法 :调用系统命令
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[[email protected] ~] # mkdir test
[[email protected] ~] # cd test/
1.txt 2.txt 3.txt 4.txt 5.txt total 0 -rw-r--r-- 1 root root 0 May 30 23:07 1.txt -rw-r--r-- 1 root root 0 May 30 23:07 2.txt -rw-r--r-- 1 root root 0 May 30 23:07 3.txt -rw-r--r-- 1 root root 0 May 30 23:07 4.txt -rw-r--r-- 1 root root 0 May 30 23:07 5.txt |
#指定界定符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
root x 0 bin x 1 daemon x 2 adm x 3 root x 0 bin x 1 daemon x 2 adm x 3 |
#流程控制语句
#条件判断语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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
[[email protected] test ] # head -10 /etc/passwd | awk '{if(NR<5){print }else if(NR>6){print}else{print "-------------"}}'
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
------------- ------------- 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
operator:x:11:0:operator: /root : /sbin/nologin
|
#循环语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
5050 5050 Tue May 30 23:24:32 CST 2017 4 23:25:50 5 CST 6 2017 1 Tue 2 May 3 30 [[email protected] test ] # awk 'BEGIN{"date" | getline t;split(t,T);for(i=1;i<=length(T);i++){print i,T[i];}}' #有序
1 Tue 2 May 3 30 4 23:26:28 5 CST 6 2017 |
#数组
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
|
[[email protected] ~] # awk 'BEGIN{S="this is a test.";split(S,A," ");for(i=1;i<=length(A);i++){print i,A[i];}}'
1 this 2 is 3 a 4 test .
[[email protected] ~] # awk 'BEGIN{S="this is a test.";split(S,A," ");for(i in A){print i,A[i];}}'
4 test .
1 this 2 is 3 a [[email protected] ~] # awk 'BEGIN{A["a"]=1;A["b"]=2;A["C"]=3;for(i in A)print i,A[i]}'
C 3 a 1 b 2 [[email protected] ~] # awk 'BEGIN{A["a"]=1;A["b"]=2;A["C"]=3;if("2" in A){print "OK"}else{print "No"}}'
No [[email protected] ~] # awk 'BEGIN{A["a"]=1;A["b"]="2";A["C"]=3;if("2" in A){print "OK"}else{print "No"}}'
No #判断是否在键值是否存在 [[email protected] ~] # awk 'BEGIN{A["a"]=1;A["b"]="2";A["C"]=3;if("b" in A){print "OK"}else{print "No"}}'
OK [[email protected] ~] # awk 'BEGIN{A["a"]=1;A["b"]=2;A["C"]=3;if("b" in A){print "OK"}else{print "No"}}'
OK [[email protected] ~] # awk 'BEGIN{A[a]=1;A["b"]=2;A["C"]=3;if("b" in A){print "OK"}else{print "No"}}'
OK #判断是否在键值是否存在 [[email protected] ~] # awk 'BEGIN{A[a]=1;A["b"]=2;A["C"]=3;if("a" in A){print "OK"}else{print "No"}}'
No [[email protected] ~] # awk 'BEGIN{A[a]=1;A["b"]=2;A["C"]=3;if("a" in A){print "OK"}else{print A[a]}}'
1 [[email protected] ~] # awk 'BEGIN{A[a]=1;A["b"]=2;A["C"]=3;if(a in A){print "OK"}else{print A[a]}}'
OK #删除键值 [[email protected] ~] # awk 'BEGIN{A[a]=1;A["b"]=2;A["C"]=3;delete A[a];if(a in A){print "OK"}else{print "No"}}'
No [[email protected] ~] # awk 'BEGIN{A[a]=1;A["b"]=2;A["C"]=3;delete A["a"];if(a in A){print "OK"}else{print "No"}}'
OK |
#内置函数
#函数练习
题目链接:http://oldboy.blog.51cto.com/2561410/1636008
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
|
原始数据: [[email protected] ~] # cat a.txt
17 /Apr/2015 :09:29:24 +0800
17 /Apr/2015 :09:30:26 +0800
17 /Apr/2015 :09:31:56 +0800
18 /Apr/2015 :09:34:12 +0800
18 /Apr/2015 :09:35:23 +0800
19 /Apr/2015 :09:23:34 +0800
19 /Apr/2015 :09:22:21 +0800
20 /Apr/2015 :09:45:22 +0800
期望结果: 2015-04-17 09:29:24+0800 2015-04-17 09:30:26+0800 2015-04-17 09:31:56+0800 2015-04-18 09:34:12+0800 2015-04-18 09:35:23+0800 2015-04-19 09:23:34+0800 2015-04-19 09:22:21+0800 2015-04-20 09:45:22+0800 [[email protected] ~] # awk -F'[ /:]' '{gsub("Apr","4");print $3"-"$2"-"$1,$4":"$5":"$6$7;}' a.txt
2015-4-17 09:29:24+0800 2015-4-17 09:30:26+0800 2015-4-17 09:31:56+0800 2015-4-18 09:34:12+0800 2015-4-18 09:35:23+0800 2015-4-19 09:23:34+0800 2015-4-19 09:22:21+0800 2015-4-20 09:45:22+0800 [[email protected] ~] #
|
#题目链接:http://oldboy.blog.51cto.com/2561410/1725148
要求去掉一个最高分,去掉一个最低分,最后求平均分,在排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[[email protected] ~] # cat a.txt
冯泉 100 100 96 95 100 96 100 97 万永振 100 95 90 88 95 98 95 98 徐亮偉 100 100 90 98 90 98 100 96 曹雅楠 100 100 95 90 97 90 95 98 陈派宇 90 95 95 96 100 98 100 96 李峰 90 90 90 85 92 95 95 98 余连辉 90 93 95 92 95 95 96 95 侯亚光 90 96 97 90 90 85 95 90 王续 85 92 98 98 90 95 蔚雷 92 92 96 90 90 80 苏浩智 88 96 85 95 90 90 徐登辉 85 95 88 90 90 90 林章益 91 97 98 90 100 90 黄品清 85 97 88 90 90 90 李健 85 95 90 88 80 80 宫全乐 88 95 90 88 90 100 万良 89 95 95 88 100 100 马自强 88 97 90 88 90 90 任冠亚 88 97 98 90 100 100 [[email protected] ~] #
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[[email protected] ~] # awk -F'[ \t]+' '{max=$2;min=$2;avg=0;sum=0;for(i=2;i<=NF;i++){if($i>max){max=$i;}if($i<min){min=$i;} sum+=$i;}avg=(sum-max-min)/(NF-3);array["$1"]=avg; print $1"\tMAX="max"\tMIN="min"\tAVG="avg}' a.txt |sort -rk4 |nl -w 2
1 冯泉 MAX=100 MIN=95 AVG=98.1667
2 徐亮偉 MAX=100 MIN=90 AVG=97
3 陈派宇 MAX=100 MIN=90 AVG=96.6667
4 任冠亚 MAX=100 MIN=88 AVG=96.25
5 曹雅楠 MAX=100 MIN=90 AVG=95.8333
6 万永振 MAX=100 MIN=88 AVG=95.1667
7 万良 MAX=100 MIN=88 AVG=94.75
8 余连辉 MAX=96 MIN=90 AVG=94.1667
9 林章益 MAX=100 MIN=90 AVG=94
10 王续 MAX=98 MIN=85 AVG=93.75 11 李峰 MAX=98 MIN=85 AVG=92 12 侯亚光 MAX=97 MIN=85 AVG=91.8333 13 蔚雷 MAX=96 MIN=80 AVG=91 14 苏浩智 MAX=96 MIN=85 AVG=90.75 15 宫全乐 MAX=100 MIN=88 AVG=90.75 16 黄品清 MAX=97 MIN=85 AVG=89.5 17 马自强 MAX=97 MIN=88 AVG=89.5 18 徐登辉 MAX=95 MIN=85 AVG=89.5 19 李健 MAX=95 MIN=80 AVG=85.75 |