Step 6 - Challenge: Supporting Pipes

In this step your goal is to support pipes. If you’ve read any of the Coding Challenges that build Unix command like tools, you’ll have seen how much I rely on pipes to chain together simple tools to build complex data munging solutions.

If you’re not familiar with the terminology, most shells allow us to ‘pipe’ the output from one command into the next command using the | character.

For example in the build your own cut challenge we piped the output of a cut command into uniq and then used wc:

cut -f2 -d, fourchords.csv | uniq | wc -l

To do this you’ll need to find out how to redirect the standard input and output streams with your chosen programming language.

To test that we’ve implemented pipes correctly we can do something similar:

% ccsh
ccsh> curl https://www.gutenberg.org/cache/epub/132/pg132.txt -o test.txt
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100  333k  100  333k    0     0   366k      0 --:--:-- --:--:-- --:--:--  368k
ccsh> cat test.txt | wc -l
7137
ccsh> exit
%

Here we downloaded a book from Project Gutenberg to use as a test, saving it as test.txt. Then we piped the text of test.txt into the command line tool wc and counted the number of lines.