% cat lab3.data
Mike Harrington:(510) 548-1278:250:100:175
Christian Dobbins:(408) 538-2358:155:90:201
Susan Dalsass:(206) 654-6279:250:60:50
Archie McNichol:(206) 548-1348:250:100:175
Jody Savage:(206) 548-1278:15:188:150
Guy Quigley:(916) 343-6410:250:100:175
Dan Savage:(406) 298-7744:450:300:275
Nancy McNeil:(206) 548-1278:250:80:75
John Goldenrod:(916) 348-4278:250:100:175
Chet Main:(510) 548-5258:50:95:135
Tom Savage:(408) 926-3456:250:168:200
Elizabeth Stachelin:(916) 440-1763:175:75:300
上面的数据库中包含名字,电话号码和过去三个月里的捐款
1.显示所有电话号码
- % awk -F: '{print $2}' lab3.data
- (510) 548-1278
- (408) 538-2358
- (206) 654-6279
- (206) 548-1348
- (206) 548-1278
- (916) 343-6410
- (406) 298-7744
- (206) 548-1278
- (916) 348-4278
- (510) 548-5258
- (408) 926-3456
- (916) 440-1763
2.显示Dan的电话号码
- [~]$ awk -F: '/Dan/{print $2}' lab3.data
- (406) 298-7744
3.显示Susan的名字和电话号码
- [~]$ awk -F: '/Susan/{print $1, $2}' lab3.data
- Susan Dalsass (206) 654-6279
4.显示所有以D开头的姓
- [~]$ awk -F"[ :]" '$2 ~/^D/{print $2}' lab3.data
- Dobbins
- Dalsass
5.显示所有以一个C或E开头的名
- [~]$ awk '$1 ~/^[CE]/{print $1}' lab3.data
- Christian
- Chet
- Elizabeth
6.显示所有只有四个字符的名
- [~]$ awk '$1 ~/[A-Z][a-z][a-z][a-z]$/{print $1}' lab3.data
- Mike
- Jody
- John
- Che
7.显示所有区号为916的人名
- [~]$ awk -F: '$2 ~/(916)/{print $1}' lab3.data
- Guy Quigley
- John Goldenrod
- Elizabeth Stachelin
8.显示Mike的捐款.显示每个值时都有以$开头.如$250$100$175
- [~]$ awk -F: '$1 ~/Mike/{print $1 ": ""$"$3"$"$4"$"$5}' lab3.data
- Mike Harrington: $250$100$175
9.显示姓,其后跟一个逗号和名,如Jody,Savage
- [~]$ awk -F"[ :]" '{print $2","$1}' lab3.data
- Harrington,Mike
- Dobbins,Christian
- Dalsass,Susan
- McNichol,Archie
- Savage,Jody
- Quigley,Guy
- Savage,Dan
- McNeil,Nancy
- Goldenrod,John
- Main,Chet
- Savage,Tom
- Stachelin,Elizabeth
10.写一个awk的脚本,它的作用:
.显示Savage的全名和电话号码
.显示Chet的捐款
.显示所有头一个月捐款$250的人名.
- [~]$ cat awk_prac
- #List Savage full name and Tel No.
- #List Chet donation amount.
- #List names of $250 donation in first month.
- /Savage/{print $1": "$2}
- /Chet/{print $1": ""$"$3", $"$4", $"$5}
- $3 ~/250/{print $1}
- [~]$
- [~]$
- [~]$ awk -f awk_prac -F: lab3.data
- Jody Savage: (206)548-1278
- Dan Savage: (406)298-7744
- Tom Savage: (408)926-3456
- Chet Main: $50, $95, $135
- Mike Harrington
- Susan Dalsass
- Archie McNichol
- Guy Quigley
- Nancy McNeil
- John Goldenrod
- Tom Savage