Due: Fri, Dec 13 @11:59pm
In this project, you will implement a simple version of grep, in Rust, which we will call rgrep.
Print NUM lines of leading context before matching lines.
Suppress normal output; instead print a count of matching lines for the input file. With the -v option, count non-matching lines.
Print a usage statement to stdout and exit with status 0.
Perform a case-insensitive match for STR.
Prefix each line of output with the 1-based line number of the file, followed immediately by a colon (e.g., 1:foo).
Quiet; do not write anything to stdout. Exit immediately with zero status if any match was found. If a match is not found, exit with a non-zero status.
Invert the sense of matching, to print non-matching lines.
Submit your project as a zip file via gradescope. You should use cargo and zip the cargo project:
cd rgrep
ls
(out) Cargo.lock Cargo.toml src
zip -r submission.zip *
There is no need for Makefile; the automated grader will invoke cargo build.
Input Files: alice.txt dorothy.txt
Helper Source Files: You can download a zip file with the starter cargo project. The project lists the following dependencies in Cargo.toml, which you may find helpful:
anyhow = "1.0.79"
clap = { version = "4.5.0", features = ["derive"] }
regex = "1.10.3"
./rgrep -c pie alice.txt
(out) 0
Has the above output (stdout).
./rgrep -c pie alice.txt
Exits with nonzero status.
./rgrep -c cake alice.txt
(out) 3
Has the above output (stdout).
./rgrep -c cake alice.txt
Exits with status of 0.
./rgrep -q rainbox alice.txt
Prints nothing.
./rgrep -q rainbox alice.txt
Exits with nonzero status.
./rgrep -q golden alice.txt
Prints nothing.
./rgrep -q golden alice.txt
Exits with status of 0.
./rgrep -n key alice.txt
(out) 107:glass; there was nothing on it except a tiny golden key, and Alice's
(out) 109:but, alas! either the locks were too large, or the key was too small,
(out) 113:little golden key in the lock, and to her great delight it fitted!
(out) 128:back to the table, half hoping she might find another key on it, or at
(out) 149:flavour of cherry-tart, custard, pine-apple, roast turkey, toffee, and
(out) 167:door, she found she had forgotten the little golden key, and when she
(out) 188:Alice, "and if it makes me grow larger, I can reach the key; and if it
Has the above output (stdout).
./rgrep -B1 garden alice.txt
(out) much larger than a rat-hole: she knelt down and looked along the
(out) passage into the loveliest garden you ever saw. How she longed to get
(out) brightened up at the thought that she was now the right size for going
(out) through the little door into that lovely garden. First, however, she
(out) After a while, finding that nothing more happened, she decided on going
(out) into the garden at once; but, alas for poor Alice! when she got to the
(out) makes me grow smaller, I can creep under the door; so either way I'll
(out) get into the garden, and I don't care which happens!"
Has the above output (stdout).
./rgrep -B3 cupboards alice.txt
(out) was going to happen next. First, she tried to look down and make out
(out) what she was coming to, but it was too dark to see anything; then she
(out) looked at the sides of the well, and noticed that they were filled with
(out) cupboards and book-shelves; here and there she saw maps and pictures
(out) passed; it was labelled "ORANGE MARMALADE", but to her great
(out) disappointment it was empty: she did not like to drop the jar for fear
(out) of killing somebody underneath, so managed to put it into one of the
(out) cupboards as she fell past it.
Has the above output (stdout).
./rgrep -n -B2 mouse alice.txt
(out) 78:tea-time. Dinah my dear! I wish you were down here with me! There are
(out) 79:no mice in the air, I'm afraid, but you might catch a bat, and that's
(out) 80:very like a mouse, you know. But do cats eat bats, I wonder?" And here
Has the above output (stdout).
./rgrep -c -B2 mouse alice.txt
(out) 1
Has the above output (stdout).
./rgrep -i rabbit alice.txt
(out) Down the Rabbit-Hole
(out) picking the daisies, when suddenly a White Rabbit with pink eyes ran
(out) so _very_ much out of the way to hear the Rabbit say to itself, "Oh
(out) time it all seemed quite natural); but when the Rabbit actually _took a
(out) had never before seen a rabbit with either a waistcoat-pocket, or a
(out) large rabbit-hole under the hedge.
(out) The rabbit-hole went straight on like a tunnel for some way, and then
(out) long passage, and the White Rabbit was still in sight, hurrying down
(out) turned the corner, but the Rabbit was no longer to be seen: she found
Has the above output (stdout).
./rgrep -v the dorothy.txt
by wagon many miles. There were four walls, a floor and a roof,
which made one room; and this room contained a rusty looking cooking
whirlwinds arose, mighty enough to crush any building in its path. It
Has the above output (stdout).
./rgrep '\w{10}' dorothy.txt
whirlwinds arose, mighty enough to crush any building in its path. It
the sky in all directions. The sun had baked the plowed land into a
they were the same gray color to be seen everywhere. Once the house had
away, and now the house was as dull and gray as everything else.
Has the above output (stdout).
./rgrep '^[A-Z]' dorothy.txt
Dorothy lived in the midst of the great Kansas prairies, with Uncle
Henry, who was a farmer, and Aunt Em, who was the farmer's wife.
Their house was small, for the lumber to build it had to be carried
When Dorothy stood in the doorway and looked around, she could see
Has the above output (stdout).
./rgrep '\?[^ ]' alice.txt
"without pictures or conversations?"
many miles I've fallen by this time?" she said aloud. "I must be
then I wonder what Latitude or Longitude I've got to?" (Alice had no
country is, you know. Please, Ma'am, is this New Zealand or Australia?"
falling through the air! Do you think you could manage it?) "And what
very like a mouse, you know. But do cats eat bats, I wonder?" And here
dreamy sort of way, "Do cats eat bats? Do cats eat bats?" and
sometimes, "Do bats eat cats?" for, you see, as she couldn't answer
"Now, Dinah, tell me the truth: did you ever eat a bat?" when suddenly,
wonder what I should be like then?" And she tried to fancy what the
way?", holding her hand on the top of her head to feel which way it was
Has the above output (stdout).