awk 练习1

#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.显示所有电话号码
2.显示Dan的电话号码
3.显示Susan的名字和电话号码
4.显示所有以D开头的姓
5.显示所有以一个C或E开头的名
6.显示所有只有四个字符的名
7.显示所有区号为916的人名
8.显示Mike的捐款.显示每个值时都有以$开头.如$250$100$175
9.显示姓,其后跟一个逗号和名,如Jody,Savage
10.写一个awk的脚本,它的作用:
.显示Savage的全名和电话号码
.显示Chet的捐款
.显示所有头一个月捐款$250的人名.


1.em@em-laptop:~/Desktop/unix/chap06/Ex_6.001-6.054$ 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.em@em-laptop:~/Desktop/unix/chap06/Ex_6.001-6.054$ awk -F: '/Dan/{print $2}' lab3.data
(406) 298-7744


3.em@em-laptop:~/Desktop/unix/chap06/Ex_6.001-6.054$ awk -F: '/Susan/{print $1}' lab3.data
Susan Dalsass


4.em@em-laptop:~/Desktop/unix/chap06/Ex_6.001-6.054$ awk -F '[ :]' '$2 ~ /^D/{print $2}' lab3.data
Dobbins
Dalsass


5.em@em-laptop:~/Desktop/unix/chap06/Ex_6.001-6.054$ awk -F: '/^[CE]/{print $1}' lab3.data
Christian Dobbins
Chet Main
Elizabeth Stachelin


6.em@em-laptop:~/Desktop/unix/chap06/Ex_6.001-6.054$ awk -F: '/[A-Z][a-z][a-z][a-z] /{print $1}' lab3.data
Mike Harrington
Jody Savage
John Goldenrod
Chet Main


7.em@em-laptop:~/Desktop/unix/chap06/Ex_6.001-6.054$ awk -F: '$2 ~ /(916)/{print $1}' lab3.data
Guy Quigley
John Goldenrod
Elizabeth Stachelin


8.em@em-laptop:~/Desktop/unix/chap06/Ex_6.001-6.054$ awk -F: '$1 ~ /Mike/{print "$"$3,"$"$4,"$"$5}' lab3.data
$250 $100 $175


9.em@em-laptop:~/Desktop/unix/chap06/Ex_6.001-6.054$ awk -F '[ :]' '{print $1","$2}' lab3.data
Mike,Harrington
Christian,Dobbins
Susan,Dalsass
Archie,McNichol
Jody,Savage
Guy,Quigley
Dan,Savage
Nancy,McNeil
John,Goldenrod
Chet,Main
Tom,Savage
Elizabeth,Stachelin


10. em@em-laptop:~/Desktop/unix/chap06/Ex_6.001-6.054$ cat facts

/Savage/{sav[$0]=$1" "$2}
/Chet/{che[$0]=$1" "$3" "$4" "$5}
$3==250{s250[$0]=$1}
END{
for (a in sav)print sav[a]
for (a in che) print che[a]
for (a in s250) print s250[a]
}


em@em-laptop:~/Desktop/unix/chap06/Ex_6.001-6.054$ awk -f facts -F: lab3.data



Tom Savage (408) 926-3456
Jody Savage (206) 548-1278
Dan Savage (406) 298-7744
Chet Main 50 95 135
Archie McNichol
Nancy McNeil
Mike Harrington
Tom Savage
Guy Quigley
John Goldenrod
Susan Dalsass




你可能感兴趣的:(unix,脚本,F#)