Uinx Shell by Example,3rd Edition第5,6章 awk 章节的答案

#说明下我系统是fedora8。 awk实际是链接到gawk,也就是说调用awk实际调用的是gawk
文件名datafile内容如下:
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 above contains the names, phone numbers, and money #contributions to the party campaign for the past three months.
##############
Chapter 5:
##############
#Practice:
 1: Print all the phone numbers.
 2: Print Dan's phone number.
 3: Print Susan's name and phone number.
 4: Print all last names beginning with D.
 5: Print all first names beginning with either a C or E.
 6: Print all first names containing only four characters.
 7: Print the first names of all those in the 916 area code.
 8: Print Mike's campaign contributions. Each value should be printed with a leading
    dollar sign; e.g., $250 $100 $175.
 9: Print last names followed by a comma and the first name.
10: Write an awk script called facts that:
      a. Prints full names and phone numbers for the Savages.
      b. Prints Chet's contributions.
      c. Prints all those who contributed $250 the first month.


Answers:
#1. awk -F: '{print $2}' datafile
#2. awk -F: '/Dan/{print $2}' datafile
#3. awk -F: '/Susan/{print $1,$2}' datafile
#4. awk '$2 ~ /^D[a-z]*/ {print}' datafile
#5. awk '$1 ~ /^[C-E][a-z]*/ {print}' datafile
#6. awk 'length($1)==4 {print}' datafile
#7. awk -F'[: ]' '$3 ~/(916)/{print}' datafile
#8. awk -F: '/Mike/{printf "$%s $%s $%s/n",$3,$4,$5}' datafile
#9. 
#方法1: awk -F'[: ]' '{printf "%s,%s:%s %s:%s:%s:%s/n",$1,$2,$3,$4,$5,$6,$7}' datafile
#方法2: awk -F'[: ]' '{sub(substr($0,1,index($0," ")),$1",",$0) ;print $0 }' datafile

#10.[chlaws@mylinux awk]$ cat facts
#This is awk script
#Name:facts
BEGIN{FS=":"}
/Savage/{print $1,$2}
/Chet/{s=$3+$4+$5;print "Chet contribution:", s}
$3~/250/{print}

[chlaws@mylinux awk]$  awk -f facts datafile





##############
#Chapter6:
##############
Practices:
    1:     Print the first and last names of those who contributed over $100 in the first month.
    2:     Print the names and phone numbers of those who contributed less than $60 in the first month.
    3:     Print those who contributed between $90 and $150 in the third month.
    4:     Print those who contributed more than $800 over the three-month period.
    5:     Print the names and phone numbers of those with an average monthly contribution greater than $150.
    6:     Print the first name of those not in the 916 area code.
    7:    Print each record preceded by the number of the record.
    8:    Print the name and total contribution of each person.
    9:    Add $10 to Elizabeth's second contribution.
   10:     Change Nancy McNeil's name to Louise McInnes.

Answers:
#1.awk -F: '$3 > 100 {print $1}' datafile
#2.awk -F: '$3 < 60  {print $1,$2}' datafile
#3.awk -F: '($5>=90)&&($5<=150){print $1,$2}' datafile
#4.awk -F: '($3+$4+$5)>800 {print}' datafile
#5.awk -F: '($3+$4+$5)/3>150 {print}' datafile
#6.awk -F'[: ]' '$3!~/(916)/{print}' datafile
#7.awk '{print NR,$0}' datafile
#8.awk -F: '{print $1,$3+$4+$5}' datafile
#9.
#方法1:awk -F: '/Elizabeth/{printf "%s:%s:%s:%s:%s/n",$1,$2,$3,$4+10,$5}' datafile
#方法2:awk -F: '/Elizabeth/{gsub($4,$4+10,$0);print $0}' datafile
#10.
#方法1:awk -F: '/Nancy McNei/{printf "%s:%s:%s:%s:%s/n","Louise Mclnnes",$2,$3,$4,$5}' datafile
#方法2:awk -F: 'BEGIN{OFS=":"}/Nancy McNei/{sub("Nancy McNei","Louise Mclnnes",$0);print}' datafile

你可能感兴趣的:(c,shell,database,less,each,Numbers)