I just stumbled across Google's Treasure Hunt. They're always doing neat things, so I checked it out. It's a series of four problem-solving puzzles released one week at a time. I found out about it after the first question was already over, so I jumped in starting with the second question.
Question 2: File sum and multiplication problem
In this problem you are given a zip folder with hundreds of text files nested in folders an arbitrary number of levels deep. The question asks to find the sum of the nth line of all files with abc in their path, ending in .xyz. You do this twice, then calculate the product of the two sums. You can download the file I got from them at the link below. googletreasurehunt.zip
My question is below:
Sum of line 3 for all files with path or name containing jkl and ending in .rtf Sum of line 5 for all files with path or name containing stu and ending in .rtf Multiply all the above sums together and enter the product below.
I thought this would be a good chance to practice my bash scripting, so I decided to give it a shot.
$ find . -type f -wholename "*jkl*\.rtf"
-type f restricts the search to files, not directories. -wholename searches for files matching that pattern. Once we run this, we get a list of all the files we'll need to process. Now we need to create a running total of the values in the nth line in each file. This is where a bash 'for' loop and 'awk' come in handy.
$ for x in $(find . -type f -wholename "*jkl*\.rtf")
The '$' tells bash to execute that command and return the result. The 'for' loop looks at the output line by line, and the variable x will contain each filename.
$ awk 'NR==3' filename.txt
This is an awk command that prints the 3rd line of filename.txt. If we can stick this into the 'for' loop, we can get a running total.
sum1=0
for x in $(find . -type f -wholename "*jkl*\.rtf")
do
sum1=$(( `awk 'NR==3' $x` + $sum1 ))
done
echo $sum1
Now we just repeat for the other pattern, and we end up with our sum in two variables. Calculating the product is simple.
echo $(( $sum1 * $sum2 ))
Personally, I thought this puzzle was a little bit simplistic. I would have expected them to throw in some tricks, so that a poorly written search command would return some other files that would mess with your result. Now that I've said that, I hope I got it right! I find out in 10 hours if my answer is correct.
Feel free to comment if you have a different and/or better solution to this problem!