本文主要通过awk的一些应用实例,来帮助我们理解awk的相关语法。
1、awk介绍
awk是一种强大的文本分析工具。awk本身就是linux下的一个工具,既可以单独使用,也可以嵌入到bash中。
awk语言的最基本功能是在文件或字符串中基于指定规则浏览和抽取信息。awk抽取信息后,才能进行其他文本操作。
2、awk应用实例
我们处理的是如下的示例信息
[root@iZ2ze7aznw5uolxelrna1gZ software]# cat netstat.txt Proto Recv-Q Send-Q Local-Address Foreign-Address State tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 coolshell.cn:80 124.205.5.146:18245 TIME_WAIT tcp 0 0 coolshell.cn:80 61.140.101.185:37538 FIN_WAIT2 tcp 0 0 coolshell.cn:80 110.194.134.189:1032 ESTABLISHED tcp 0 0 coolshell.cn:80 123.169.124.111:49809 ESTABLISHED tcp 0 0 coolshell.cn:80 116.234.127.77:11502 FIN_WAIT2 tcp 0 0 coolshell.cn:80 123.169.124.111:49829 ESTABLISHED tcp 0 0 coolshell.cn:80 183.60.215.36:36970 TIME_WAIT tcp 0 4166 coolshell.cn:80 61.148.242.38:30901 ESTABLISHED tcp 0 1 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 tcp 0 0 coolshell.cn:80 110.194.134.189:4796 ESTABLISHED tcp 0 0 coolshell.cn:80 183.60.212.163:51082 TIME_WAIT tcp 0 1 coolshell.cn:80 208.115.113.92:50601 LAST_ACK tcp 0 0 coolshell.cn:80 123.169.124.111:49840 ESTABLISHED tcp 0 0 coolshell.cn:80 117.136.20.85:50025 FIN_WAIT2 tcp 0 0 :::22 :::* LISTEN
2.1 常用示例
下面是最简单最常用的awk示例,其输出第1列和第4例,
- 其中单引号中的被大括号括着的就是awk的语句,注意,其只能被单引号包含。
- 其中的$1..$n表示第几例。注:$0表示整个行。
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '{print $1,$4}' netstat.txt Proto Local-Address tcp 0.0.0.0:3306 tcp 0.0.0.0:80 tcp 127.0.0.1:9000 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp coolshell.cn:80 tcp :::22
我们再来看看awk的格式化输出,和C语言的printf没什么两样(里面的格式需要用双引号引起来):
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '{printf "%-8s %-8s %-8s %-18s %-22s %-15s\n",$1,$2,$3,$4,$5,$6}' netstat.txt Proto Recv-Q Send-Q Local-Address Foreign-Address State tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 coolshell.cn:80 124.205.5.146:18245 TIME_WAIT tcp 0 0 coolshell.cn:80 61.140.101.185:37538 FIN_WAIT2 tcp 0 0 coolshell.cn:80 110.194.134.189:1032 ESTABLISHED tcp 0 0 coolshell.cn:80 123.169.124.111:49809 ESTABLISHED tcp 0 0 coolshell.cn:80 116.234.127.77:11502 FIN_WAIT2 tcp 0 0 coolshell.cn:80 123.169.124.111:49829 ESTABLISHED tcp 0 0 coolshell.cn:80 183.60.215.36:36970 TIME_WAIT tcp 0 4166 coolshell.cn:80 61.148.242.38:30901 ESTABLISHED tcp 0 1 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 tcp 0 0 coolshell.cn:80 110.194.134.189:4796 ESTABLISHED tcp 0 0 coolshell.cn:80 183.60.212.163:51082 TIME_WAIT tcp 0 1 coolshell.cn:80 208.115.113.92:50601 LAST_ACK tcp 0 0 coolshell.cn:80 123.169.124.111:49840 ESTABLISHED tcp 0 0 coolshell.cn:80 117.136.20.85:50025 FIN_WAIT2 tcp 0 0 :::22 :::* LISTEN
2.2 过滤记录
我们再来看看如何过滤记录(下面过滤条件为:第三列的值为0 && 第6列的值为LISTEN)
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '$3==0 && $6=="LISTEN"' netstat.txt tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN
其中的"=="为比较运算符。其他比较运算符:!=, >, <, >=, <=
我们来看看各种过滤记录的方式:
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '$3>0 {print $0}' netstat.txt Proto Recv-Q Send-Q Local-Address Foreign-Address State tcp 0 4166 coolshell.cn:80 61.148.242.38:30901 ESTABLISHED tcp 0 1 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 tcp 0 1 coolshell.cn:80 208.115.113.92:50601 LAST_ACK
如果我们需要表头的话,我们可以引入内建变量NR:
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '$3==0 && $6=="LISTEN" || NR==1' netstat.txt Proto Recv-Q Send-Q Local-Address Foreign-Address State tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN
再加上格式化输出:
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '$3==0 && $6=="LISTEN" || NR==1 {printf "%-20s %-20s %s\n",$4,$5,$6}' netstat.txtLocal-Address Foreign-Address State 0.0.0.0:3306 0.0.0.0:* LISTEN 0.0.0.0:80 0.0.0.0:* LISTEN 127.0.0.1:9000 0.0.0.0:* LISTEN :::22 :::* LISTEN
2.3 内建变量
说到了内建变量,我们可以来看看awk的一些内建变量:
$0 |
当前记录(这个变量中存放着整个行的内容) |
$1~$n |
当前记录的第n个字段,字段间由FS分隔 |
FS |
输入字段分隔符 默认是空格或Tab |
NF |
当前记录中的字段个数,就是有多少列 |
NR |
已经读出的记录数,就是行号,从1开始,如果有多个文件话,这个值也是不断累加中。 |
FNR |
当前记录数,与NR不同的是,这个值会是各个文件自己的行号 |
RS |
输入的记录分隔符, 默认为换行符 |
OFS |
输出字段分隔符, 默认也是空格 |
ORS |
输出的记录分隔符,默认为换行符 |
FILENAME |
当前输入文件的名字 |
怎么使用呢,比如:我们如果要输出行号:
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '$3==0 && $6=="ESTABLISHED" || NR==1 {printf "%02s %s %-20s %-20s %s\n",NR, FNR, $4,$5,$6}' netstat.txt 01 1 Local-Address Foreign-Address State 07 7 coolshell.cn:80 110.194.134.189:1032 ESTABLISHED 08 8 coolshell.cn:80 123.169.124.111:49809 ESTABLISHED 10 10 coolshell.cn:80 123.169.124.111:49829 ESTABLISHED 14 14 coolshell.cn:80 110.194.134.189:4796 ESTABLISHED 17 17 coolshell.cn:80 123.169.124.111:49840 ESTABLISHED
2.4 指定分隔符
[root@iZ2ze7aznw5uolxelrna1gZ ~]# cat /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 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 vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin nscd:x:28:28:NSCD Daemon:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin rtkit:x:498:498:RealtimeKit:/proc:/sbin/nologin pulse:x:497:495:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin mysql:x:496:493:MySQL server:/var/lib/mysql:/bin/bash winner_0715:x:500:500::/home/winner_0715:/bin/bash
[root@iZ2ze7aznw5uolxelrna1gZ ~]# awk 'BEGIN{FS=":"} {print $1,$3,$6}' /etc/passwd root 0 /root bin 1 /bin daemon 2 /sbin adm 3 /var/adm lp 4 /var/spool/lpd sync 5 /sbin shutdown 6 /sbin halt 7 /sbin mail 8 /var/spool/mail uucp 10 /var/spool/uucp operator 11 /root games 12 /usr/games gopher 13 /var/gopher ftp 14 /var/ftp nobody 99 / vcsa 69 /dev saslauth 499 /var/empty/saslauth postfix 89 /var/spool/postfix sshd 74 /var/empty/sshd ntp 38 /etc/ntp tcpdump 72 / nscd 28 / dbus 81 / rtkit 498 /proc pulse 497 /var/run/pulse avahi-autoipd 170 /var/lib/avahi-autoipd haldaemon 68 / gdm 42 /var/lib/gdm mysql 496 /var/lib/mysql winner_0715 500 /home/winner_0715
上面的命令也等价于:(-F的意思就是指定分隔符)
[root@iZ2ze7aznw5uolxelrna1gZ ~]# awk -F ':' '{print $1,$3,$6}' /etc/passwd
再来看一个以\t作为分隔符输出的例子(下面使用了/etc/passwd文件,这个文件是以:分隔的):
[root@iZ2ze7aznw5uolxelrna1gZ ~]# awk -F ':' '{print $1,$3,$6}' OFS='\t' /etc/passwd root 0 /root bin 1 /bin daemon 2 /sbin adm 3 /var/adm lp 4 /var/spool/lpd sync 5 /sbin shutdown 6 /sbin halt 7 /sbin mail 8 /var/spool/mail uucp 10 /var/spool/uucp operator 11 /root games 12 /usr/games gopher 13 /var/gopher ftp 14 /var/ftp nobody 99 / vcsa 69 /dev saslauth 499 /var/empty/saslauth postfix 89 /var/spool/postfix sshd 74 /var/empty/sshd ntp 38 /etc/ntp tcpdump 72 / nscd 28 / dbus 81 / rtkit 498 /proc pulse 497 /var/run/pulse avahi-autoipd 170 /var/lib/avahi-autoipd haldaemon 68 / gdm 42 /var/lib/gdm mysql 496 /var/lib/mysql winner_0715 500 /home/winner_0715
OFS='\t'不在单引号表达式中!
2.5 字符串匹配
我们再来看几个字符串匹配的示例:
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '$6 ~ /FIN/ || NR==1 {print NR,$4,$5,$6}' OFS='\t' netstat.txt 1 Local-Address Foreign-Address State 6 coolshell.cn:80 61.140.101.185:37538 FIN_WAIT2 9 coolshell.cn:80 116.234.127.77:11502 FIN_WAIT2 13 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 18 coolshell.cn:80 117.136.20.85:50025 FIN_WAIT2
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '$6 ~ /WAIT/ || NR==1 {print NR,$4,$5,$6}' OFS='\t' netstat.txt 1 Local-Address Foreign-Address State 5 coolshell.cn:80 124.205.5.146:18245 TIME_WAIT 6 coolshell.cn:80 61.140.101.185:37538 FIN_WAIT2 9 coolshell.cn:80 116.234.127.77:11502 FIN_WAIT2 11 coolshell.cn:80 183.60.215.36:36970 TIME_WAIT 13 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 15 coolshell.cn:80 183.60.212.163:51082 TIME_WAIT 18 coolshell.cn:80 117.136.20.85:50025 FIN_WAIT2
上面的第一个示例匹配FIN状态, 第二个示例匹配WAIT字样的状态。其实 ~ 表示模式开始。/ /中是模式。这就是一个正则表达式的匹配。
其实awk可以像grep一样的去匹配第一行,就像这样:
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '/LISTEN/' netstat.txt tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN
我们可以使用 "/FIN|TIME/" 来匹配 FIN 或者 TIME :
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '$6 ~ /FIN|TIME/ || NR==1 {print NR,$4,$5,$6}' OFS="\t" netstat.txt 1 Local-Address Foreign-Address State 5 coolshell.cn:80 124.205.5.146:18245 TIME_WAIT 6 coolshell.cn:80 61.140.101.185:37538 FIN_WAIT2 9 coolshell.cn:80 116.234.127.77:11502 FIN_WAIT2 11 coolshell.cn:80 183.60.215.36:36970 TIME_WAIT 13 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 15 coolshell.cn:80 183.60.212.163:51082 TIME_WAIT 18 coolshell.cn:80 117.136.20.85:50025 FIN_WAIT2
再来看看模式取反的例子:
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '$6 !~ /WAIT/ || NR==1 {print NR,$4,$5,$6}' OFS='\t' netstat.txt 1 Local-Address Foreign-Address State 2 0.0.0.0:3306 0.0.0.0:* LISTEN 3 0.0.0.0:80 0.0.0.0:* LISTEN 4 127.0.0.1:9000 0.0.0.0:* LISTEN 7 coolshell.cn:80 110.194.134.189:1032 ESTABLISHED 8 coolshell.cn:80 123.169.124.111:49809 ESTABLISHED 10 coolshell.cn:80 123.169.124.111:49829 ESTABLISHED 12 coolshell.cn:80 61.148.242.38:30901 ESTABLISHED 14 coolshell.cn:80 110.194.134.189:4796 ESTABLISHED 16 coolshell.cn:80 208.115.113.92:50601 LAST_ACK 17 coolshell.cn:80 123.169.124.111:49840 ESTABLISHED 19 :::22 :::* LISTEN
[root@iZ2ze7aznw5uolxelrna1gZ software]# awk '!/WAIT/' netstat.txt Proto Recv-Q Send-Q Local-Address Foreign-Address State tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 coolshell.cn:80 110.194.134.189:1032 ESTABLISHED tcp 0 0 coolshell.cn:80 123.169.124.111:49809 ESTABLISHED tcp 0 0 coolshell.cn:80 123.169.124.111:49829 ESTABLISHED tcp 0 4166 coolshell.cn:80 61.148.242.38:30901 ESTABLISHED tcp 0 0 coolshell.cn:80 110.194.134.189:4796 ESTABLISHED tcp 0 1 coolshell.cn:80 208.115.113.92:50601 LAST_ACK tcp 0 0 coolshell.cn:80 123.169.124.111:49840 ESTABLISHED tcp 0 0 :::22 :::* LISTEN
2.6折分文件
awk拆分文件很简单,使用重定向就好了。下面这个例子,是按第6列分隔文件,相当的简单(其中的NR!=1表示不处理表头)。
其实就是按照第6列 group by
$ awk 'NR!=1{print > $6}' netstat.txt $ ls ESTABLISHED FIN_WAIT1 FIN_WAIT2 LAST_ACK LISTEN netstat.txt TIME_WAIT $ cat ESTABLISHED tcp 0 0 coolshell.cn:80 110.194.134.189:1032 ESTABLISHED tcp 0 0 coolshell.cn:80 123.169.124.111:49809 ESTABLISHED tcp 0 0 coolshell.cn:80 123.169.124.111:49829 ESTABLISHED tcp 0 4166 coolshell.cn:80 61.148.242.38:30901 ESTABLISHED tcp 0 0 coolshell.cn:80 110.194.134.189:4796 ESTABLISHED tcp 0 0 coolshell.cn:80 123.169.124.111:49840 ESTABLISHED $ cat FIN_WAIT1 tcp 0 1 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 $ cat FIN_WAIT2 tcp 0 0 coolshell.cn:80 61.140.101.185:37538 FIN_WAIT2 tcp 0 0 coolshell.cn:80 116.234.127.77:11502 FIN_WAIT2 tcp 0 0 coolshell.cn:80 117.136.20.85:50025 FIN_WAIT2 $ cat LAST_ACK tcp 0 1 coolshell.cn:80 208.115.113.92:50601 LAST_ACK $ cat LISTEN tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN $ cat TIME_WAIT tcp 0 0 coolshell.cn:80 124.205.5.146:18245 TIME_WAIT tcp 0 0 coolshell.cn:80 183.60.215.36:36970 TIME_WAIT tcp 0 0 coolshell.cn:80 183.60.212.163:51082 TIME_WAIT
你也可以把指定的列输出到文件:
awk 'NR!=1{print $4,$5 > $6}' netstat.txt
再复杂一点:(注意其中的if-else-if语句,可见awk其实是个脚本解释器)
$ awk 'NR!=1{if($6 ~ /TIME|ESTABLISHED/) print > "1.txt"; else if($6 ~ /LISTEN/) print > "2.txt"; else print > "3.txt" }' netstat.txt $ ls ?.txt 1.txt 2.txt 3.txt $ cat 1.txt tcp 0 0 coolshell.cn:80 124.205.5.146:18245 TIME_WAIT tcp 0 0 coolshell.cn:80 110.194.134.189:1032 ESTABLISHED tcp 0 0 coolshell.cn:80 123.169.124.111:49809 ESTABLISHED tcp 0 0 coolshell.cn:80 123.169.124.111:49829 ESTABLISHED tcp 0 0 coolshell.cn:80 183.60.215.36:36970 TIME_WAIT tcp 0 4166 coolshell.cn:80 61.148.242.38:30901 ESTABLISHED tcp 0 0 coolshell.cn:80 110.194.134.189:4796 ESTABLISHED tcp 0 0 coolshell.cn:80 183.60.212.163:51082 TIME_WAIT tcp 0 0 coolshell.cn:80 123.169.124.111:49840 ESTABLISHED $ cat 2.txt tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN $ cat 3.txt tcp 0 0 coolshell.cn:80 61.140.101.185:37538 FIN_WAIT2 tcp 0 0 coolshell.cn:80 116.234.127.77:11502 FIN_WAIT2 tcp 0 1 coolshell.cn:80 124.152.181.209:26825 FIN_WAIT1 tcp 0 1 coolshell.cn:80 208.115.113.92:50601 LAST_ACK tcp 0 0 coolshell.cn:80 117.136.20.85:50025 FIN_WAIT2
2.7统计
下面的命令计算所有的C文件,CPP文件和H文件的文件大小总和。
$ ls -l *.cpp *.c *.h | awk '{sum+=$5} END {print sum}' 2511401
我们再来看一个统计各个connection状态的用法:(我们可以看到一些编程的影子了,大家都是程序员我就不解释了。注意其中的数组的用法)
$ awk 'NR!=1{a[$6]++;} END {for (i in a) print i ", " a[i];}' netstat.txt TIME_WAIT, 3 FIN_WAIT1, 1 ESTABLISHED, 6 FIN_WAIT2, 3 LAST_ACK, 1 LISTEN, 4
再来看看统计每个用户的进程的占了多少内存(注:sum的RSS那一列)
$ ps aux | awk 'NR!=1{a[$1]+=$6;} END { for(i in a) print i ", " a[i]"KB";}' dbus, 540KB mysql, 99928KB www, 3264924KB root, 63644KB hchen, 6020KB
2.8 awk脚本
在上面我们可以看到一个END关键字。END的意思是“处理完所有的行的标识”,即然说到了END就有必要介绍一下BEGIN,这两个关键字意味着执行前和执行后的意思,语法如下:
- BEGIN{ 这里面放的是执行前的语句 }
- END {这里面放的是处理完所有的行后要执行的语句 }
- {这里面放的是处理每一行时要执行的语句}
为了说清楚这个事,我们来看看下面的示例:
假设有这么一个文件(学生成绩表):
$ cat score.txt Marry 2143 78 84 77 Jack 2321 66 78 45 Tom 2122 48 77 71 Mike 2537 87 97 95 Bob 2415 40 57 62
我们的awk脚本如下(我没有写有命令行上是因为命令行上不易读,另外也在介绍另一种用法):
$ cat cal.awk #!/bin/awk -f #运行前 BEGIN { math = 0 english = 0 computer = 0 printf "NAME NO. MATH ENGLISH COMPUTER TOTAL\n" printf "---------------------------------------------\n" } #运行中 { math+=$3 english+=$4 computer+=$5 printf "%-6s %-6s %4d %8d %8d %8d\n", $1, $2, $3,$4,$5, $3+$4+$5 } #运行后 END { printf "---------------------------------------------\n" printf " TOTAL:%10d %8d %8d \n", math, english, computer printf "AVERAGE:%10.2f %8.2f %8.2f\n", math/NR, english/NR, computer/NR }
我们来看一下执行结果:(也可以这样运行 ./cal.awk score.txt)
$ awk -f cal.awk score.txt NAME NO. MATH ENGLISH COMPUTER TOTAL --------------------------------------------- Marry 2143 78 84 77 239 Jack 2321 66 78 45 189 Tom 2122 48 77 71 196 Mike 2537 87 97 95 279 Bob 2415 40 57 62 159 --------------------------------------------- TOTAL: 319 393 350 AVERAGE: 63.80 78.60 70.00
参考:
1.http://www.cnblogs.com/skywang12345/archive/2013/05/31/3107871.html
2.https://coolshell.cn/articles/9070.html
来源:https://www.cnblogs.com/winner-0715/p/4994313.html