Write Your Own head
This challenge is to build your own version of the Unix command line tool head. It’s a great challenge for beginners and those new to the Unix command line tools.
The Unix command line tools are a great metaphor for good software engineering and they follow the Unix Philosophies of:
- Writing simple parts connected by clean interfaces - each tool does just one thing and provides a simple CLI that handles text input from either files or file streams.
- Design programs to be connected to other programs - each tool can be easily connected to other tools to create incredibly powerful compositions.
Following these philosophies has made the simple Unix command line tools some of the most widely used software engineering tools - allowing us to create very complex text data processing pipelines from simple tools. There’s even a Coursera course on Data Engineering with Bash!
You can read more about the Unix Philosophy on the Coding Challenges blog.
The Challenge - Building Head
Head is a command line tool that displays the first n lines or bytes of a file, where the user can provide the value for n. If no file or value for n is provided then it displays the first 10 lines from the standard input.
As always the first thing to do is decide on the programming language you’re going to use to tackle this challenge and set up your development environment. Then please download the following text:
https://www.gutenberg.org/cache/epub/132/pg132.txt and save it as
Once that’s done proceed to Step 1.
In this step your goal is to take the filename or no filename and print the contents out. Some sample test cases for this look like:
% cchead test.txt The Project Gutenberg eBook of The Art of War This ebook is for the use of anyone anywhere in the United States and most other parts of the world at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Project Gutenberg License included with this ebook or online at www.gutenberg.org. If you are not located in the United States, you will have to check the laws of the country where you are located before using this eBook.
% cchead This is some text I typed in This is some text I typed in line 2 line 2 line 3 line 3 line 4 line 4 line 5 line 5 line 6 line 6 line 7 line 7 line 8 line 8 line 9 line 9 line 10 line 10
Note in this second example it is reading from the standard input and each line appears twice - the first is the echo of the standard input to the screen that bash (or whatever shell you are using - BTW you can learn more about Shells and build your own shell in another Coding Challenge) does; the second is the line from our
cchead. Our implementation should terminate after line 10.
In this step your goal is to display only the first n lines, where n is passed to the user as a command line argument. That should look like this:
% cchead -n1 test.txt The Project Gutenberg eBook of The Art of War
Do test with different values of n and consider what will happen if there aren’t enough lines in the file. You could test that like this:
% echo "Hello, World" >> test2.txt % cchead -n3 test2.txt Hello, World
Make sure your program terminates correctly.
In this step your goal is to only display the first c bytes. You can test that like this:
% cchead -c 31 test.txt The Project Gutenberg eBook
% cchead -c 30 test2.txt Hello, World
Again ensure you handle reaching the end of the file before printing the required number of characters.
In this step your goal is to handle multiple files correctly. When multiple files are provided the output is presented, each file is preceded by a header consisting of the string
==> XXX <== where “XXX” is the name of the file. That should look like this:
% cchead -n 10 test2.txt test.txt ==> test2.txt <== Hello, World ==> test.txt <== The Project Gutenberg eBook of The Art of War This ebook is for the use of anyone anywhere in the United States and most other parts of the world at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Project Gutenberg License included with this ebook or online at www.gutenberg.org. If you are not located in the United States, you will have to check the laws of the country where you are located before using this eBook.
Note that up to n lines per file are output.
Once that is done, congratulations you’ve completed this Coding Challenge, why not try another more complex one?
Help Others by Sharing Your Solutions!
If you think your solution is an example other developers can learn from please share it, put it on GitHub, GitLab or elsewhere. Then let me know - ping me a message on the Discord Server or in the Coding Challenges Sub Reddit, via Twitter or LinkedIn or just post about it there and tag me.
Get The Challenges By Email
If you would like to recieve the coding challenges by email, you can subscribe to the weekly newsletter on SubStack here: