You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _episodes/05-counting-mining.md
+2-45Lines changed: 2 additions & 45 deletions
Original file line number
Diff line number
Diff line change
@@ -331,20 +331,13 @@ programming languages.
331
331
> ## Counting number of files
332
332
> Let's make a different pipeline. You want to find out how many files and
333
333
> directories there are in the current directory. Try to see if you can pipe
334
-
> the output from `ls` into `wc` to find the answer, or something close to the
335
-
> answer.
334
+
> the output from `ls` into `wc` to find the answer.
336
335
>
337
336
> > ## Solution
338
-
> > You get close with
339
-
> >
340
337
> > ~~~
341
-
> > $ ls -l | wc -l
338
+
> > $ ls | wc -l
342
339
> > ~~~~
343
340
> > {: .bash}
344
-
> >
345
-
> > but the count will be one too high, since the "total" line from `ls`
346
-
> > is included in the count. We'll get back to a way to fix that later
347
-
> > when we've learned about the `grep` command.
348
341
> {: .solution}
349
342
{: .challenge}
350
343
@@ -754,42 +747,6 @@ Pair up with your neighbor and work on these exercises:
754
747
> {: .solution}
755
748
{: .challenge}
756
749
757
-
> ## Counting number of files, part II
758
-
> In the earlier counting exercise in this episode, you tried counting the number
759
-
> of files and directories in the current directory.
760
-
>
761
-
> * Recall that the command `ls -l | wc -l` took us quite far, but the result was one
762
-
> too high because it included the "total" line in the line count.
763
-
> * With the knowledge of `grep`, can you figure out how to exclude the "total"
764
-
> line from the `ls -l` output?
765
-
> * Hint: You want to exclude any line *starting*
766
-
> with the text "total". The hat character (^) is used
767
-
> in regular expressions to indicate the start of a line.
768
-
>
769
-
> > ## Solution
770
-
> > To find any lines starting with "total", we would use:
771
-
> >
772
-
> > ~~~
773
-
> > $ ls -l | grep -E '^total'
774
-
> > ~~~
775
-
> > {: .bash}
776
-
> >
777
-
> > To *exclude those lines, we add the `-v` flag:
778
-
> >
779
-
> > ~~~
780
-
> > $ ls -l | grep -v -E '^total'
781
-
> > ~~~
782
-
> > {: .bash}
783
-
> >
784
-
> > The grand finale is to pipe this into `wc -l`:
785
-
> >
786
-
> > ~~~
787
-
> > $ ls -l | grep -v -E '^total' | wc -l
788
-
> > ~~~
789
-
> > {: .bash}
790
-
> {: .solution}
791
-
{: .challenge}
792
-
793
750
### Using a Loop to Count Words
794
751
795
752
We will now use a loop to automate the counting of certain words within a document. For this, we will be using the _[Little Women](http://www.gutenberg.org/cache/epub/514/pg514.txt)_ e-book from [Project Gutenberg](https://www.gutenberg.org/). The file is inside the `shell-lesson` folder and named `pg514.txt`. Let's rename the file to `littlewomen.txt`.
0 commit comments