unix test 4
TRANSCRIPT
-
7/29/2019 UNIX Test 4
1/3
2/15/13 UNIX Test 4
uva.ulb.ac.be/cit_courseware/unix/test4.htm
Learning UNIX Copyright Brian Brown, 1988-2000. All rights reserved.
This material may not be reproduced in printed or electronic format without the express permission of
the author.
Test 4
Only use this if you have a Javascript compatible browser
The following questions use this data file. All questions relate to awk
First Last Age Sex phone Smoking City
Sue Williams 23 F 5277089 N PoriruaJoe Bloggs 45 M 3450999 Y Upper-Hutt
Bill Hunt 23 M 2341897 Y Georgetown
Karen White 19 F 4532367 Y Palmerston
Bobby Waters 17 M 3289765 N Lower-Hutt
Sam Brown 26 M 3276890 N Lower-Hutt
Fred Kent 56 M 2770876 N Upper-Hutt
Susan George 34 F 3421345 Y Belmont
Linda Gray 21 F 4563213 Y Masterton
Beth Gables 32 F 6785436 Y Auckland
Liz Samuels 33 F 6547890 N Wellington
Debby Wills 43 F 8964556 Y Wellington
1. List all smokers
$6 == "F" { print $0 }
$6 == "Y" { print $0 }
$4 == "F" { print $0 }
$4 == "N" { print $0 }
2. List all non-smoking males
$6 == "M" && $4 == "N" { print $0 }$4 == "M" || $6 == "N" { print $0 }
$6 == "M" AND $6 == "N" { print $0 }
$4 == "M" && $6 == "N" { print $0 }
3. List all smoking females under 25
$6 == "N" && $4 == "F" && $3
-
7/29/2019 UNIX Test 4
2/3
2/15/13 UNIX Test 4
uva.ulb.ac.be/cit_courseware/unix/test4.htm
$6 == "Y" && $4 == "F" || $3 < 25 { print $0 }
4. List all non-smoking males who live in Auckland
$6 == "N" || $4 == "M" && $7 ~ "Auckland" { print $0 }
$6 == "N" && $4 == "M" && $7 ~ "Auckland" { print $0 }
$6 == "N" && $4 == "M" && $7 = "Auckland" { print $0 }
$6 != "N" && $4 == "M" && $7 !~ "Auckland" { print $0 }
5. List all smoking females under 25 who live in Wellington or Palmerston
$6 == "N" || $4 == "F" && $3 < 25 && $7 ~ /(Wellington|Palmerston)/ { print $0 }
$6 == "Y" && $4 == "F" && $3 < 25 && $7 ~ /(Wellington|Palmerston)/ { print $0 }
$6 == "Y" && $4 == "M" ||$3 < 25 && $7 !~ /(Wellington|Palmerston)/ { print $0 }
$6 == "N" && $4 == "M" && $3 < 25 && $7 ~ /(Wellington|Palmerston)/ { print $0 }
6. Which of the following lists the total number of non-smokers
$6 == "N" {nosmokers++}
END {print "Number of nonsmokers is ", nonsmokers }
$6 == "N" {nosmokers--}
END {print "Number of nonsmokers is ", nonsmokers }
7. Which of the following lists the number of smokers as a percentage of the total number of people in the file
$6 == "Y" {smokers++}
END {print "Number of smokers as a % is ", (smokers + NR) * 100 }
$6 == "Y" {smokers++}END {print "Number of smokers as a % is ", (smokers/NR) * 100 }
8. All records preceeded by their line number
{print NR, ":\t", $0}
{print NF, ":\t", $0}
{print 1, ":\t", $0}
9. Which of the following prints the number of non-smokers in Wellington
$6 == "N" || $7 ~ /Wellington/ { ns++ }
END { print "Number of Wellington non-smokers is ", ns }
$6 == "N" && $7 ~ /Wellington/ { ns++ }
END { print "Number of Wellington non-smokers is ", ns }
10. Which of the following prints the record of the person with the longest surname
{if (length($2)>sum)
{
sum = length($2)
-
7/29/2019 UNIX Test 4
3/3