Uw.shell by example, 3rd Edition Chapter 5 and chapter 6 awk answers

Source: Internet
Author: User

# My system is fedora8. The actual link of the awk is gawk, that is, the actual call of the awk is gawk.
The content of the file name datafile is as follows:
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 mcnickl :( 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 shocould 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/n", $3, $4, $5}' datafile
#9.
# Method 1: awk-F' [:] ''{printf" % s, % s: % S % s: % s/n ", $1, $2, $3, $4, $5, $6, $7} 'datafile
# Method 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.
# Method 1: awk-F: '/Elizabeth/{printf "% s: % s/n", $1, $2, $3, $4 + 10, $5} 'datafile
# Method 2: awk-F: '/Elizabeth/{gsub ($4, $4 + 10, $0); print $0}' datafile
#10.
# Method 1: awk-F: '/Nancy mcnei/{printf "% s: % s/n", "Louise mclnnes ", $2, $3, $4, $5} 'datafile
# Method 2: awk-F: 'In in {OFS = ":"}/Nancy mcnei/{sub ("Nancy mcnei", "Louise mclnnes", $0 ); print} 'datafile

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.