1,#! /bin/awk -f
!用来定义解释性语言使用的解释器
2,检索特定字符串,并输出
awk '/li/ { print $0 }' mail-list
3, 获取行数最多的一行
awk '{ if (length($0) > max) max = length($0) }
END { print max }' data
4, 获取行field多余0的数据
awk 'NF > 0' data
5,获取一个文件夹中的文件大小总和
ls -l files | awk '{ x += $5 }
END { print "total bytes: " x }'
6,计算总行数
awk 'END { print NR }' data
7,默认不支持多行,但是可以通过反斜线连接
awk '/This regular expression is too long, so continue it
on the next line/ { print $1 }'
8,对某个field进行正则匹配
awk '$1 ~ /J/' inventory-shipped
附录数据:
1,mail-list
Amelia 555-5553 amelia.zodiacusque@gmail.com F
Anthony 555-3412 anthony.asserturo@hotmail.com A
Becky 555-7685 becky.algebrarum@gmail.com A
Bill 555-1675 bill.drowning@hotmail.com A
Broderick 555-0542 broderick.aliquotiens@yahoo.com R
Camilla 555-2912 camilla.infusarum@skynet.be R
Fabius 555-1234 fabius.undevicesimus@ucb.edu F
Julie 555-6699 julie.perscrutabor@skeeve.com F
Martin 555-6480 martin.codicibus@hotmail.com A
Samuel 555-3430 samuel.lanceolis@shu.edu A
Jean-Paul 555-2127 jeanpaul.campanorum@nyu.edu R
2,inventory-ship
Jan 13 25 15 115
Feb 15 32 24 226
Mar 15 24 34 228
Apr 31 52 63 420
May 16 34 29 208
Jun 31 42 75 492
Jul 24 34 67 436
Aug 15 34 47 316
Sep 13 55 37 277
Oct 29 54 68 525
Nov 20 87 82 577
Dec 17 35 61 401
Jan 21 36 64 620
Feb 26 58 80 652
Mar 24 75 70 495
Apr 21 70 74 514