-
-
Notifications
You must be signed in to change notification settings - Fork 966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #518 for 07-find #528
Fix #518 for 07-find #528
Changes from 6 commits
217b1f4
dc07df0
8b40048
fe23fb3
22c7745
0b83da0
85f87ec
af53148
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -504,6 +504,11 @@ about them." | |
> 2. `grep -E "of" haiku.txt` | ||
> 3. `grep -w "of" haiku.txt` | ||
> 4. `grep -i "of" haiku.txt` | ||
> | ||
> > ## Solution | ||
> > The correct answer is 3, because the `-w` flag looks only for whole-word matches. | ||
> > The other options will all match "of" when part of another word. | ||
> {: .solution} | ||
{: .challenge} | ||
|
||
> ## `find` Pipeline Reading Comprehension | ||
|
@@ -514,20 +519,38 @@ about them." | |
> wc -l $(find . -name '*.dat') | sort -n | ||
> ~~~ | ||
> {: .bash} | ||
> | ||
> > ## Solution | ||
> > Find all files with a `.dat` extension in the current directory. | ||
> > Count the number of lines each of these files contains, then sort the output numerically. | ||
> {: .solution} | ||
{: .challenge} | ||
|
||
> ## Matching and Subtracting | ||
> | ||
> The `-v` flag to `grep` inverts pattern matching, so that only lines | ||
> which do *not* match the pattern are printed. Given that, which of | ||
> the following commands will find all files in `/data` whose names | ||
> end in `ose.dat` (e.g., `sucrose.dat` or `maltose.dat`), but do | ||
> *not* contain the word `temp`? | ||
> end in `s.txt` (e.g., `animals.txt` or `planets.txt`), but do | ||
> *not* contain the word `net`? | ||
> Once you have thought about your answer, you can test the commands in the `data-shell` | ||
> directory. | ||
> | ||
> 1. `find /data -name '*.dat' | grep ose | grep -v temp` | ||
> 2. `find /data -name ose.dat | grep -v temp` | ||
> 3. `grep -v "temp" $(find /data -name '*ose.dat')` | ||
> 1. `find /data -name '*s.txt' | grep -v net` | ||
> 2. `find /data -name *s.txt | grep -v net` | ||
> 3. `grep -v "temp" $(find /data -name '*s.txt')` | ||
> 4. None of the above. | ||
> | ||
> > ## Solution | ||
> > The correct answer is 1. Putting the match expression in quotes prevents the shell | ||
> > expanding it, so it gets passed to the `find` command. | ||
> > | ||
> > Option 2 is incorrect because the shell expands `*s.txt` instead of passing the wildcard | ||
> > expression to `find`. | ||
> > | ||
> > Option 3 is incorrect because it searches the contents of the files for lines which | ||
> > do not match "temp", rather than searching the file names. | ||
> {: .solution} | ||
{: .challenge} | ||
|
||
> ## Tracking a Species | ||
|
@@ -544,15 +567,14 @@ about them." | |
> ~~~ | ||
> {: .source} | ||
> | ||
> She wants to write a shell script that takes a directory and a species | ||
> as command-line parameters and return one file called `species.txt` | ||
> containing a list of dates and the number of that species seen on that date, | ||
> such as this file for rabbits: | ||
> She wants to write a shell script that takes a species as the first command-line argument | ||
> and a directory as the second argument. The script should return one file called `species.txt` | ||
> containing a list of dates and the number of that species seen on each date. | ||
> For example using the data shown above, `rabbits.txt` would contain: | ||
> | ||
> ~~~ | ||
> 2013-11-05,22 | ||
> 2013-11-06,19 | ||
> 2013-11-07,18 | ||
> ~~~ | ||
> {: .source} | ||
> | ||
|
@@ -571,6 +593,23 @@ about them." | |
> | ||
> Hint: use `man grep` to look for how to grep text recursively in a directory | ||
> and `man cut` to select more than one field in a line. | ||
> | ||
> An example of such a file is provided in `data-shell/data/animal-counts/animals.txt` | ||
> | ||
> > ## Solution | ||
> > | ||
> > ``` | ||
> > grep -w $1 -r $2 | cut -d : -f 2 | cut -d , -f 1,3 > $1.txt | ||
> > ``` | ||
> > {: .source} | ||
> > | ||
> > You would call the script above like this: | ||
> > | ||
> > ``` | ||
> > $ bash count-species.sh bear . | ||
> > ``` | ||
> > {: .bash} | ||
> {: .solution} | ||
{: .challenge} | ||
|
||
> ## Little Women | ||
|
@@ -579,13 +618,39 @@ about them." | |
> Louisa May Alcott, are in an argument. Of the four sisters in the | ||
> book, Jo, Meg, Beth, and Amy, your friend thinks that Jo was the | ||
> most mentioned. You, however, are certain it was Amy. Luckily, you | ||
> have a file `LittleWomen.txt` containing the full text of the novel. | ||
> have a file `LittleWomen.txt` containing the full text of the novel | ||
> (`data-shell/writing/data/LittleWomen.txt`). | ||
> Using a `for` loop, how would you tabulate the number of times each | ||
> of the four sisters is mentioned? | ||
> | ||
> Hint: one solution might employ | ||
> the commands `grep` and `wc` and a `|`, while another might utilize | ||
> `grep` options. | ||
> | ||
> > ## Solutions | ||
> > ``` | ||
> > for sis in Jo Meg Beth Amy | ||
> > do | ||
> > echo $sis: | ||
> > grep -ow $sis littlewomen.txt | wc -l | ||
> > done | ||
> > ``` | ||
> > {: .source} | ||
> > | ||
> > Alternative, slightly inferior solution: | ||
> > ``` | ||
> > for sis in Jo Meg Beth Amy | ||
> > do | ||
> > echo $sis: | ||
> > grep -ocw $sis LittleWomen.txt | ||
> > done | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say let's leave out the inferior solution altogether. Any advantage to keeping it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only that the question suggests two different ways to achieve the outcome. I don't mind removing the alternative solution, so long as we also remove the hint which suggests it. Or I could add a short note explaining that there is often more than one way to complete a programming task. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, @gcapes - I'd suggest keeping the hint and adding the note. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
> > ``` | ||
> > {: .source} | ||
> > | ||
> > This solution is inferior because `grep -c` only reports the number of lines matched. | ||
> > The total number of matches reported by this method will be lower if there is more | ||
> > than one match per line. | ||
> {: .solution} | ||
{: .challenge} | ||
|
||
> ## Finding Files With Different Properties | ||
|
@@ -600,10 +665,12 @@ about them." | |
> | ||
> Hint 2: The value for `-mtime` will need to be negative---why? | ||
> | ||
> Solution: Assuming that Nelle’s home is our working directory we type: | ||
> | ||
> ~~~ | ||
> $ find ./ -type f -mtime -1 -user ahmed | ||
> ~~~ | ||
> {: .bash} | ||
> > ## Solution | ||
> > Assuming that Nelle’s home is our working directory we type: | ||
> > | ||
> > ~~~ | ||
> > $ find ./ -type f -mtime -1 -user ahmed | ||
> > ~~~ | ||
> > {: .bash} | ||
> {: .solution} | ||
{: .challenge} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
2012-11-05,deer,5 | ||
2012-11-05,rabbit,22 | ||
2012-11-05,raccoon,7 | ||
2012-11-06,rabbit,19 | ||
2012-11-06,deer,2 | ||
2012-11-06,fox,4 | ||
2012-11-07,rabbit,16 | ||
2012-11-07,bear,1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we list (enumerate) them:
.dat
extension in the current directoryThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!