Vous êtes sur la page 1sur 4

Exercise 1

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
The database contains the names, phone numbers, and money contributions to the party
campaign for the past three months.
1. Print all the phone numbers.
Awk F : {print $2} homework1.txt
2. Print Dan's phone number.
Awk F : $1 ~/Dan/ {print $2} homework1.txt
3. Print Susan's name and phone number.
Awk F : $1 ~/Susan/ {print $1, $2} homework1.txt
4. Print all last names beginning with D.
Awk F [: ] $1 ~/^D/ {print $2} homework1.txt

5. Print all first names beginning with either a C or E.


Awk F [: ] $1~/^[CE]/ {print $1} homework1.txt

6. Print all first names containing only four characters.


Awk length($1) ==4 {print $1} homework1.txt
How to first separate F ?
7. Print the first names of all those in the 916 area code
Awk F [: ] $3 ~/(916)/ {print $1} homework1.txt
8. Print Mike's campaign contributions. Each value should be printed with a leading dollar
sign; e.g., $250 $100 $175.
Awk F [: ] $1 ~/Mike/ {print $$5, $$6, $$7} homework1.txt
9. Print last names followed by a comma and the first name.
Awk F [: ] {print $2 , $1} homework1.txt
10. Write an awk script called facts that
a. Prints full names and phone numbers for the Savages.
Awk F [:] $1 ~/Savage/ {print $1, $2} homework1.txt
Awk F [: ] $2 ~/Savage/ {print $1, $2, $3, $4} homework1.txt
b. Prints Chet's contributions.
Awk F [: ] $1~/Chet/ {print $3, $4, $5} homework1.txt
c. Prints all those who contributed $250 the first month.
Awk F : $3 ~/250/{print $0} homework1.txt

Exercise 2
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
The database contains the names, phone numbers, and money contributions to the party
campaign for the past three months.
1. Print the first and last names of those who contributed more than $100 in the second
month.
Awk F : $4 >100 {print $1} homework1.txt
2. Print the names and phone numbers of those who contributed less than $85 in the last
month.
Awk F : $5<85 {print $1: print $2} homework1.txt
3. Print the names of those who contributed between $75 and $150 in the first month.
Awk F : $3<150 && $3>75 {print $1}homework1.txt

No output according to the input


4. Print the names of those who contributed less than $800 over the three-month period.
Awk F : {sum=$3+$4+$5} sum>800{print $1}} homework1.txt
5. Print the names and addresses of those with an average monthly contribution greater than
$200.
Awk F : {average=($3+$4+$5)/3} average>200{print $, average} homework1.txt

6. Print the first name of those not in the 916 area code.
Awk F [: ] $3 !~/916/{print $1} homework1.txt
7. Print each record preceded by the number of the record
Awk -F":" '{print NR, $0}' awk_exercise1
NR prints the number of records
8. Print the name and total contribution of each person.
Awk F : {sum=$3+$4+$5} {print $1, sum} homework1.txt
9. Add $10 to Chet's second contribution.
Awk F : $1 ~/Chet/ {c=$4+10} {print c} homework1.txt
10. Change Nancy McNeil's name to Louise McInnes.
Awk {gsub(/Nancy McNeil/, Louise McInnes);} {print $1} homework1.txt

Vous aimerez peut-être aussi