Google Treasurehunt 2
Unzip the archive, then process the resulting files to obtain a numeric result. You'll be taking the sum of lines from files matching a certain description, and multiplying those sums together to obtain a final result. Note that files have many different extensions, like '.pdf' and '.js', but all are plain text files containing a small number of lines of text.
Sum of line 5 for all files with path or name containing def and ending in .xml
Sum of line 2 for all files with path or name containing ghi and ending in .txt
Hint: If the requested line does not exist, do not increment the sum.Multiply all the above sums together and enter the product below.
Well, as I said before, I'm gonna try to do it with my single ubuntu laptop, and the net, and my brain.
well, first this seems to be a job for awk, and regular expression :
First get the list of all files, using a regular expression to match the requirement
find -regex .*def.*xmlthen pipe it to an awk that for each line (that is a file name) prints it, with the line number
awk '{system("cat -n "$0)}'And then, make a good old awk to get the line number 5 (here) and print the second column
awk 'BEGIN{s=0}/ 5/ {s+=$2} END{print s}'All in one it makes
find -regex .*def.*xml|awk '{system("cat -n "$0)}'|awk 'BEGIN{s=0}/ 5/ {s+=$2} END{printf "http://gmplib.org/cgi-bin/gmp_calc.pl?expr="s"*">"mund"}'I've first tried to make the multiplication in awk directely, but the number is too big, and awk return a floating point value. So I've build an url to perform the multiplication using GMPLib.
find -regex .*ghi.*txt|awk '{system("cat -n "$0)}'|awk 'BEGIN{s=0}/ 2/ {s+=$2;} END{print s>>"mund"}'
wget -q -O - -i mund
the URL is built in a file called mund, and wget is used to retrieve the value.
89383*25647
89 383 * 25 647 = 2 292 405 801 |
that's it.
The previous question required some math skills, but the solution was elegant, some nice website, and that's it.
This one is much more tricky, and require much more coding skills. I've fulfilled my duty, I 've done it, in my laptop only, but, I'm glad to run ubuntu, 'cause on a window system, this would have be, MUCH more difficult.
I'm feeling like missing something. I'm not very familiar with awk, but I'm still finding the "system("cat -n "$0)" ugly. Do you ?
Edit : looking around in the web I've found this nice improvement
find -regex .*def.*xml|xargs -n 1 sed -n '5p'|awk '{s+=$0}END{print s}'
Libellés : Google, Treasurehunt
0 Comments:
Enregistrer un commentaire
Links to this post:
Créer un lien
<< Home