Skip to content

Exercise: Scavenger hunt

Kenny Yu edited this page Sep 25, 2013 · 15 revisions

Getting the code

In a terminal, execute these commands:

git clone git://github.com/hcs/bootcamp-setup.git
cd bootcamp-setup

Scavenger Hunt: Part 1

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.

  1. How many lines are in the file? (Hint: use cat to dump the contents of the file, and look at the man page for wc to see how to count the number of lines in a file.) SOLUTION cat file.txt | wc -l

  2. How many times does Kenny appear in the file? (Hint: use grep "NAME" to print out the lines that contain NAME). SOLUTION cat file.txt | grep "Kenny" | wc -l

  3. How many times does Kenny OR Karen appear in the file (Hint: use grep "pattern1\|pattern2" to print out the lines that contain pattern1 OR pattern2. SOLUTION cat file.txt | grep "Kenny\|Karen" | wc -l

  4. 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 expression awk '{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 of data.txt. SOLUTION awk '{print $2}' < data.txt SOLUTION `cat data.txt | awk '{print $3}'

  5. Print out the third column of data.txt, capitalize all the letters, and save it to a file named pokemon.txt. SOLUTION cat data.txt | awk '{print $3}' | tr '[:lower:]' '[:upper:]' > pokemon.txt

  6. Print out the contents of pokemon.txt in sorted order. (Hint: look at the man page for the sort command). SOLUTION cat pokemon.txt | sort

  7. How many distinct pokemon are there in pokemon.txt? (Hint: there is a very useful option you can pass to the sort command. SOLUTION cat pokemon.txt | sort -u | wc -l

  8. 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 of data.txt. SOLUTION cat data.txt | awk '{print $2}' | python -c "import sys; print sum(int(line) for line in sys.stdin)"

  9. Sum all the numbers in the second column of data.txt for any line containing Kenny OR Karen. SOLUTION cat data.txt | grep "Kenny\|Karen" | awk '{print $2}' | python -c "import sys; print sum(int(line) for line in sys.stdin)"

TODO

grep: output all links of this format find all lines containing the word blah

find: find the location of this file count the number of files in a directory

count the total number of words in all the files where does the word foo appear

sum all these counts together etc.