Extra Meal Midterm Test Write a Simple Grep Command Line

Extra Lesson - Midterm Test: Writing a Simple grep Command Line #

Hello, I’m Chen Tian.

Now that you have completed the Rust foundation course, I believe you have enough confidence to take on some simple development tasks. Today, we have a midterm test to practically examine your understanding of the Rust language and your application of the knowledge learned.

The small tool we are going to make is rgrep, which is a tool similar to grep. If you’re a *nix user, you likely have used text search tools like grep or ag.

The grep command is used to search for strings within a file that meet certain conditions. If it finds content in a file that matches the specified string, the grep command will display the line containing the string; if no file name is specified, or the given file name is -, the grep command will read data from the standard input.

Our rgrep will be a bit simpler, supporting the following three usage scenarios:

First is the simplest one, given a string and a file, print all lines inside the file that contain that string:

$ rgrep Hello a.txt
55: Hello world. This is an exmaple text

Then, we loosen the restriction, allowing the user to provide a regular expression to find all lines in the file that contain that string:

$ rgrep Hel[^\\s]+ a.txt
55: Hello world. This is an exmaple text
89: Help me! I need assistant!

If you can implement this too, then we’ll loosen the restrictions further by allowing users to provide a regular expression to search all files that satisfy file wildcards (you can use globset or glob to handle wildcards), for example:

$ rgrep Hel[^\\s]+ a*.txt
a.txt 
    55:1 Hello world. This is an exmaple text
    89:1 Help me! I need assistant!
    5:6 Use `Help` to get help.
abc.txt:
    100:1 Hello Tyr!

Where, the number before the colon is the line number and the number after the colon is the position of the character in that line.

Here are a few tips for you.

  • For the command line part, you can use clap3 or structopt, or you can just use env.args().
  • For support of regular expressions, you can use regex.
  • As for file reading, you can use std::fs or tokio::fs. You can process all files that meet the wildcards sequentially, or use rayon or tokio for parallel processing.
  • For the output result, it is better to display matching text in different colors.

Image

If you still have energy, you can look at the grep documentation and try to implement more features.

Good luck!

Keep it up, and I’ll see you in the next class for the homework review.