Unix Shell exemplar refines Chapter 6 - AWK utility has six exercises, from simple to complex, currently done the first three questions, the latter three questions feel very laborious to do, about loops, arrays, functions, do not know how to do.
AWK Exercise 1: Exercise 1 mainly focuses on the operation of regular expressions with patterns. Some topics can be answered with the contents explained later in the book.
[kevin.tao@cws76 Ex_6.001-6.054]$ 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
The database contains names, phone numbers, and campaign contributions over the last three months.
1.Print all phone numbers awk -F '[: ]' '{print $3,$4 }' lab3.data #Set field separators to colons and spaces 2.Printing Dan Telephone number awk -F '[: ]' '/^Dan/{print $3 $4 }' lab3.data #Patterns Matching Dan with Corresponding Actions 3.Printing Susan Name and telephone number awk -F '[: ]' '/^Susan/{print $1,$2, $3 $4 }' lab3.data 4.Print all surnames with D First surname awk -F '[: ]' '$2~/^D/{print $2}' lab3.data #$x ~/ XXX / This is a common way 5.Print all with C or E Name at the beginning awk '/^[CE]/{print $1}' lab3.data 6.Print all names that contain only four characters awk '/^....\>/{print $1}' lab3.data #Although the result is correct, it is not rigorous. awk -F '[: ]' '$1~/^[a-zA-Z]{4}$/{print $1}' lab3.data awk -F '[: ]' '{if(length($1)==4){print $1}}' lab3.data 7.Print the names of all people with area code 916 awk -F '[: ]' '$3=="(916)"{print $1}' lab3.data awk -F '[: ]' '$3~/916/{print $1}' lab3.data 8.Printing Mike The amount of the grant, with each value beginning with a dollar symbol, such as“ $250$100$175" awk -F '[: ]' '/^Mike/{print "$"$5"$"$6"$"$7}' lab3.data awk -F '[: ]' '$1~/Mike$/{print "$"$5"$"$6"$"$7}' lab3.data 9.Print all surnames followed by a comma and name awk -F '[: ]' '{print $2","$1}' lab3.data 10.Write a program called facts The script, and complete the following work: a.Print all surnames as Savage Person's full name and telephone number b.Printing Chet Amount of funding c.Print out all the people who funded $250 in the first month //The script content: #/usr/bin/awk #Print the full name and phone number of all people with Savage name $2~/Savage$/{print $1,$2,$3,$4} #Print out the amount of Chet's grant $1~/Chet$/{print $5,$6,$7} #Print out all the people who funded $250 in the first month $5~/250$/{print $1,$2} #Change the value of FS when executing scripts
AWK Exercise 2: The main modes of practice are comparative expressions, relational operators, etc.
[kevin.tao@cws76 Ex_6.055-6.089]$ cat lab4.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
The database contains names, phone numbers, and campaign contributions over the last three months.
1.Print the names and phone numbers of people who donated more than $100 in the second month awk -F '[: ]' '$6>100{print $1,$2,$3 $4}' lab4.data 2.Print the names and phone numbers of people who donated less than $85 in the last month awk -F '[: ]' '$7<85{print $1,$2,$3 $4}' lab4.data 3.Print the first month contribution at 75-150 People between Dollars awk -F '[: ]' '$5>75 && $5<150{print $1,$2}' lab4.data 4.Print out those who donate no more than $800 in three months awk -F '[: ]' '($5+$6+47)<800{print $1,$2}' lab4.data awk -F '[: ]' '{if(($5+$6+$7)<800){print $1,$2}}' lab4.data 5.Print the names and phone numbers of people who contribute more than $200 a month on average awk -F '[: ]' '($5+$6+$7)/3>200{print $1,$2,$3 $4}' lab4.data awk -F '[: ]' '{if(($5+$6+$7)/3>200){print $1,$2,$3 $4}}' lab4.data 6.Print the surnames of people who are not in District 916 awk -F '[: ]' '!($3=="(916)"){print $2}' lab4.data 7.Print each record and add a record number before the record. awk '{print NR,$0}' lab4.data 8.Print out everyone's name and total donations awk -F '[: ]' '{print $1,$2,$5+$6+$7}' lab4.data 9.hold Chet Contributions for the second month plus 10 awk -F '[: ]' '/^\<Chet\>/{print $6+10}' lab4.data awk -F '[: ]' '/^\<Chet\>/{$6=$6+10;print}' lab4.data 10.hold Nancy McNeil Change the name to Louise McInnes awk -F '[: ]' '$1=="Nancy" && $2=="McNeil"{$1="Louise";$2="McInnes";print $1,$2 }' lab4.data
AWK Exercise 3: Main Exercise Variables, Format Control, BEGIN END, etc.
[kevin.tao@cws76 Ex_6.090-6.126]$ cat lab5.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
The above database includes name, telephone number and the amount of campaign donations received in the last three months.
Write a nawk script that produces the following output:
[kevin.tao@cws76 Ex_6.090-6.126]$ awk -f nawk.sc lab5.data ***CAMPAIGN 1998 CONTRIBUTIONS*** -------------------------------------------------------------------------------------- NAME PHONE Jan| Feb | Mar | Total Donated -------------------------------------------------------------------------------------- Mike Harrington (510) 548-1278 250.00 100.00 175.00 525.00 Christian Dobbins (408) 538-2358 155.00 90.00 201.00 446.00 Susan Dalsass (206) 654-6279 250.00 60.00 50.00 360.00 Archie McNichol (206) 548-1348 250.00 100.00 175.00 525.00 Jody Savage (206) 548-1278 15.00 188.00 150.00 353.00 Guy Quigley (916) 343-6410 250.00 100.00 175.00 525.00 Dan Savage (406) 298-7744 450.00 300.00 275.00 1025.00 Nancy McNeil (206) 548-1278 250.00 80.00 75.00 405.00 John Goldenrod (916) 348-4278 250.00 100.00 175.00 525.00 Chet Main (510) 548-5258 50.00 95.00 135.00 280.00 Tom Savage (408) 926-3456 250.00 168.00 200.00 618.00 Elizabeth Stachelin (916) 440-1763 175.00 75.00 300.00 550.00 ---------------------------------------------------------------------------------- SUMMARY ---------------------------------------------------------------------------------- The campaign received a total of 6137.00 for this quarter. #Total contributions from all The average donation for the 12 contribuors was $511.42. #How much is the average contribution per person? The highest contribution was $450.00. #Maximum monthly contribution The lowest contribution was $15.00. #Minimum monthly contribution
The fields in the book are aligned. The format of the script output is not very neat. The script content is as follows:
[kevin.tao@cws76 Ex_6.090-6.126]$ cat nawk.sc #/usr/bin/awk BEGIN { FS=":" printf "%40s\n","***CAMPAIGN 1998 CONTRIBUTIONS***" print "--------------------------------------------------------------------------------------" printf "%-22s %-16s %7s| %7s | %7s | %8s","NAME","PHONE", "Jan", "Feb","Mar","Total Donated\n" print "--------------------------------------------------------------------------------------" total=0;sum=0;lc=0;highest=0;lowest } { total=$3+$4+$5;sum=sum+total;lc=lc+1 highest=(highest > $3)? highest : $3 highest=(highest > $4)? highest : $4 highest=(highest > $5)? highest : $5 lowest=(lowest==0 || lowest > $3)? $3 : lowest lowest=(lowest < $4)? lowest : $4 lowest=(lowest < $5)? lowest : $5 printf "%-22s %-16s %7.2f %7.2f %7.2f %8.2f\n",$1,$2,$3,$4,$5,total } END { printf "----------------------------------------------------------------------------------\n" printf "\t\t\tSUMMARY\n" printf "----------------------------------------------------------------------------------\n" printf "The campaign received a total of %7.2f for this quarter.\n",sum printf "The average donation for the 12 contribuors was $%.2f.\n",sum/lc #Or sum/NR, sum/12 printf "The highest contribution was $%.2f.\n",highest printf "The lowest contribution was $%.2f.\n",lowest } #The total variable is used to record the total contributions of each person for three months, the sum variable is used to record the total contributions of all people, the LC variable is used to record the number of contributions (sum/lc and sum/NR are equal to sum/12), and the highest and lowest variables are used to process the maximum and minimum values.
Summary: It feels awk is very difficult to learn, there are many contents, and the exercises behind the book can not be done. Array, function, loop and other functions are needed. It feels awk script is more difficult to write than bash script.