-
Notifications
You must be signed in to change notification settings - Fork 1
Exercise: Scavenger hunt
In a terminal, execute these commands:
git clone git://github.com/hcs/bootcamp-setup.git
cd bootcamp-setup
Inside the bootcamp-setup
directory, you should see an exercise-simple
directory. Enter
cd exercise-simple
and you should see a file called data.txt
. Now answer the following questions by writing a sequence of commands to do the work for you. Save your answers in a file called answers.txt
in this directory.
-
How many lines are in the file? (Hint: use
cat
to dump the contents of the file, and look at theman
page forwc
to see how to count the number of lines in a file.) SOLUTIONcat file.txt | wc -l
-
How many times does Kenny appear in the file? (Hint: use
grep "NAME"
to print out the lines that containNAME
). SOLUTIONcat file.txt | grep "Kenny" | wc -l
-
How many times does Kenny OR Karen appear in the file (Hint: use
grep "pattern1\|pattern2"
to print out the lines that containpattern1
ORpattern2
. SOLUTIONcat file.txt | grep "Kenny\|Karen" | wc -l
-
For the next few questions, you may find this information useful. If you feed in input (either using
<
or by piping input using|
) to the expressionawk '{print $2}'
, this will print out the second column of the input. To print out the a different column, change$2
to be the column number you want (e.g. to print out the first column replace$2
with$1
). Print out the third column ofdata.txt
. SOLUTION `cat data.txt | awk '{print $3}' -
Print out the third column of
data.txt
, capitalize all the letters, and save it to a file namedpokemon.txt
. SOLUTIONcat data.txt | awk '{print $3}' | tr '[:lower:]' '[:upper:]' > pokemon.txt
-
Print out the contents of
pokemon.txt
in sorted order. (Hint: look at theman
page for thesort
command). SOLUTIONcat pokemon.txt | sort
-
You give Goldduck a water stone and it evolves into Psyduck! Replace all instances of
GOLDDUCK
withPSYDYCK
from pokemon.txt, and save the output inpokemon_evolved.txt
. (Hint:sed s/old/new/g
will replace all instances ofold
withnew
). SOLUTIONcat pokemon.txt | sed s/GOLDDUCK/PSYDUCK/g > pokemon_evolved.txt
-
How many distinct pokemon are there in
pokemon.txt
? (Hint: there is a very useful option you can pass to thesort
command. SOLUTIONcat pokemon.txt | sort -u | wc -l
-
For the next few questions, you may find this useful: If you feed in a file with one number on each line to this command:
python -c "import sys; print sum(int(s) for s in sys.stdin.readlines()[0].split())"
, this will sum all the numbers. Sum all the numbers in the second column ofdata.txt
. SOLUTIONcat data.txt | awk '{print $2}' | python -c "import sys; print sum(int(line) for line in sys.stdin)"
-
Sum all the numbers in the second column of
data.txt
for any line containingKenny
ORKaren
. SOLUTIONcat data.txt | grep "Kenny\|Karen" | awk '{print $2}' | python -c "import sys; print sum(int(line) for line in sys.stdin)"
-
curl LINK
is a command that can download the html of a webpage. How many times does the word "google" appear in the html ofwww.google.com
? (Hint: use the--only-matching
option ongrep PATTERN
to only print the parts of the text that matchPATTERN
). SOLUTIONcurl --silent www.google.com | grep google --only-matching | wc -l
-
How long is the word in
word.txt
? SOLUTIONcat word.txt | wc -c
-
There is file called
mystery.sh
in the directory. Give it executable permissions, and run it. What is the output of the program? (Hint: to run a programprog.sh
in the current directory, you need to prefix it with./
, e.g../prog.sh
). SOLUTIONchmod +x mystery.sh; ./mystery.sh
In the exercise-simple
directory, you should find a directory called os161
. This directory is the source code for the operating system you will become very familiar with if you decide to take CS161: Operating Systems (which you all should)! Go into this directory:
cd os161
-
How many files are there in the
os161
directory, including all subdirectories? (Hint:find DIRECTORY
will list ALL the files in the directory, including subdirectories). Note that.
refers to the current directory, and..
refers to the parent directory. SOLUTIONfind . | wc -l
-
Where is the file
errno.h
in theos161/kern
directory? (Hint:find DIRECTORY -name FILE
will attempt to findFILE
somewhere in the directory hierarchy rooted atDIRECTORY
). SOLUTIONfind kern -name "errno.h"
-
How many different values of errno are there in this file? SOLUTION
cat ./kern/include/kern/errno.h | grep "#define E" | wc -l
-
Find all occurrences (all files and line numbers) of the string
STACK_SIZE
in any file (including subdirectories) in theos161
directory. (Hint: Usegrep
. Look at theman
page for grep, and look for the words recursive and number). SOLUTIONgrep -r -n "STACK_SIZE" .
-
How big is
STACK_SIZE
? SOLUTIONgrep -r -n "#define STACK_SIZE" .
-
What is the numeric value of
EFAULT
? What does it mean to get anEFAULT
? (look at the associated comment) SOLUTIONgrep -r -n "#define EFAULT" .
Go back to the main page.