Search a file for lines matching a regular expression pattern. Return the line number and contents of each matching line.
The Unix grep
command can be used to search for lines in one or more files
that match a user-provided search query (known as the pattern).
The grep
command takes three arguments:
Your task is to implement the grep
function, which should read the contents
of the specified files, find the lines that match the specified pattern
and then output those lines as a single string. Note that the lines should
be output in the order in which they were found, with the first matching line
in the first file being output first.
As an example, suppose there is a file named "input.txt" with the following contents:
hello
world
hello again
If we were to call grep "hello" input.txt
, the returned string should be:
hello
hello again
As said earlier, the grep
command should also support the following flags:
-n
Print the line numbers of each matching line.-l
Print only the names of files that contain at least one matching line.-i
Match line using a case-insensitive comparison.-v
Invert the program -- collect all lines that fail to match the pattern.-x
Only match entire lines, instead of lines that contain a match.If we run grep -n "hello" input.txt
, the -n
flag will require the matching
lines to be prefixed with its line number:
1:hello
3:hello again
And if we run grep -i "HELLO" input.txt
, we'll do a case-insensitive match,
and the output will be:
hello
hello again
The grep
command should support multiple flags at once.
For example, running grep -l -v "hello" file1.txt file2.txt
should
print the names of files that do not contain the string "hello".
To run the tests, run the command dotnet test
from within the exercise directory.
Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the Skip
property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using exercism submit Grep.cs
For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.
Conversation with Nate Foster. http://www.cs.cornell.edu/Courses/cs3110/2014sp/hw/0/ps0.pdf
// This file was auto-generated based on version 1.2.0 of the canonical data.
using System;
using System.IO;
using Xunit;
public class GrepTest : IDisposable
{
[Fact]
public void One_file_one_match_no_flags()
{
var pattern = "Agamemnon";
var flags = "";
var files = new[] { "iliad.txt" };
var expected = "Of Atreus, Agamemnon, King of men.";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_one_match_print_line_numbers_flag()
{
var pattern = "Forbidden";
var flags = "-n";
var files = new[] { "paradise-lost.txt" };
var expected = "2:Of that Forbidden Tree, whose mortal tast";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_one_match_case_insensitive_flag()
{
var pattern = "FORBIDDEN";
var flags = "-i";
var files = new[] { "paradise-lost.txt" };
var expected = "Of that Forbidden Tree, whose mortal tast";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_one_match_print_file_names_flag()
{
var pattern = "Forbidden";
var flags = "-l";
var files = new[] { "paradise-lost.txt" };
var expected = "paradise-lost.txt";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_one_match_match_entire_lines_flag()
{
var pattern = "With loss of Eden, till one greater Man";
var flags = "-x";
var files = new[] { "paradise-lost.txt" };
var expected = "With loss of Eden, till one greater Man";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_one_match_multiple_flags()
{
var pattern = "OF ATREUS, Agamemnon, KIng of MEN.";
var flags = "-n -i -x";
var files = new[] { "iliad.txt" };
var expected = "9:Of Atreus, Agamemnon, King of men.";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_several_matches_no_flags()
{
var pattern = "may";
var flags = "";
var files = new[] { "midsummer-night.txt" };
var expected =
"Nor how it may concern my modesty,\n" +
"But I beseech your grace that I may know\n" +
"The worst that may befall me in this case,";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_several_matches_print_line_numbers_flag()
{
var pattern = "may";
var flags = "-n";
var files = new[] { "midsummer-night.txt" };
var expected =
"3:Nor how it may concern my modesty,\n" +
"5:But I beseech your grace that I may know\n" +
"6:The worst that may befall me in this case,";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_several_matches_match_entire_lines_flag()
{
var pattern = "may";
var flags = "-x";
var files = new[] { "midsummer-night.txt" };
var expected = "";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_several_matches_case_insensitive_flag()
{
var pattern = "ACHILLES";
var flags = "-i";
var files = new[] { "iliad.txt" };
var expected =
"Achilles sing, O Goddess! Peleus' son;\n" +
"The noble Chief Achilles from the son";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_several_matches_inverted_flag()
{
var pattern = "Of";
var flags = "-v";
var files = new[] { "paradise-lost.txt" };
var expected =
"Brought Death into the World, and all our woe,\n" +
"With loss of Eden, till one greater Man\n" +
"Restore us, and regain the blissful Seat,\n" +
"Sing Heav'nly Muse, that on the secret top\n" +
"That Shepherd, who first taught the chosen Seed";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_no_matches_various_flags()
{
var pattern = "Gandalf";
var flags = "-n -l -x -i";
var files = new[] { "iliad.txt" };
var expected = "";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_one_match_file_flag_takes_precedence_over_line_flag()
{
var pattern = "ten";
var flags = "-n -l";
var files = new[] { "iliad.txt" };
var expected = "iliad.txt";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void One_file_several_matches_inverted_and_match_entire_lines_flags()
{
var pattern = "Illustrious into Ades premature,";
var flags = "-x -v";
var files = new[] { "iliad.txt" };
var expected =
"Achilles sing, O Goddess! Peleus' son;\n" +
"His wrath pernicious, who ten thousand woes\n" +
"Caused to Achaia's host, sent many a soul\n" +
"And Heroes gave (so stood the will of Jove)\n" +
"To dogs and to all ravening fowls a prey,\n" +
"When fierce dispute had separated once\n" +
"The noble Chief Achilles from the son\n" +
"Of Atreus, Agamemnon, King of men.";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_files_one_match_no_flags()
{
var pattern = "Agamemnon";
var flags = "";
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
var expected = "iliad.txt:Of Atreus, Agamemnon, King of men.";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_files_several_matches_no_flags()
{
var pattern = "may";
var flags = "";
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
var expected =
"midsummer-night.txt:Nor how it may concern my modesty,\n" +
"midsummer-night.txt:But I beseech your grace that I may know\n" +
"midsummer-night.txt:The worst that may befall me in this case,";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_files_several_matches_print_line_numbers_flag()
{
var pattern = "that";
var flags = "-n";
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
var expected =
"midsummer-night.txt:5:But I beseech your grace that I may know\n" +
"midsummer-night.txt:6:The worst that may befall me in this case,\n" +
"paradise-lost.txt:2:Of that Forbidden Tree, whose mortal tast\n" +
"paradise-lost.txt:6:Sing Heav'nly Muse, that on the secret top";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_files_one_match_print_file_names_flag()
{
var pattern = "who";
var flags = "-l";
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
var expected =
"iliad.txt\n" +
"paradise-lost.txt";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_files_several_matches_case_insensitive_flag()
{
var pattern = "TO";
var flags = "-i";
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
var expected =
"iliad.txt:Caused to Achaia's host, sent many a soul\n" +
"iliad.txt:Illustrious into Ades premature,\n" +
"iliad.txt:And Heroes gave (so stood the will of Jove)\n" +
"iliad.txt:To dogs and to all ravening fowls a prey,\n" +
"midsummer-night.txt:I do entreat your grace to pardon me.\n" +
"midsummer-night.txt:In such a presence here to plead my thoughts;\n" +
"midsummer-night.txt:If I refuse to wed Demetrius.\n" +
"paradise-lost.txt:Brought Death into the World, and all our woe,\n" +
"paradise-lost.txt:Restore us, and regain the blissful Seat,\n" +
"paradise-lost.txt:Sing Heav'nly Muse, that on the secret top";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_files_several_matches_inverted_flag()
{
var pattern = "a";
var flags = "-v";
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
var expected =
"iliad.txt:Achilles sing, O Goddess! Peleus' son;\n" +
"iliad.txt:The noble Chief Achilles from the son\n" +
"midsummer-night.txt:If I refuse to wed Demetrius.";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_files_one_match_match_entire_lines_flag()
{
var pattern = "But I beseech your grace that I may know";
var flags = "-x";
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
var expected = "midsummer-night.txt:But I beseech your grace that I may know";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_files_one_match_multiple_flags()
{
var pattern = "WITH LOSS OF EDEN, TILL ONE GREATER MAN";
var flags = "-n -i -x";
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
var expected = "paradise-lost.txt:4:With loss of Eden, till one greater Man";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_files_no_matches_various_flags()
{
var pattern = "Frodo";
var flags = "-n -l -x -i";
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
var expected = "";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_files_several_matches_file_flag_takes_precedence_over_line_number_flag()
{
var pattern = "who";
var flags = "-n -l";
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
var expected =
"iliad.txt\n" +
"paradise-lost.txt";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_files_several_matches_inverted_and_match_entire_lines_flags()
{
var pattern = "Illustrious into Ades premature,";
var flags = "-x -v";
var files = new[] { "iliad.txt", "midsummer-night.txt", "paradise-lost.txt" };
var expected =
"iliad.txt:Achilles sing, O Goddess! Peleus' son;\n" +
"iliad.txt:His wrath pernicious, who ten thousand woes\n" +
"iliad.txt:Caused to Achaia's host, sent many a soul\n" +
"iliad.txt:And Heroes gave (so stood the will of Jove)\n" +
"iliad.txt:To dogs and to all ravening fowls a prey,\n" +
"iliad.txt:When fierce dispute had separated once\n" +
"iliad.txt:The noble Chief Achilles from the son\n" +
"iliad.txt:Of Atreus, Agamemnon, King of men.\n" +
"midsummer-night.txt:I do entreat your grace to pardon me.\n" +
"midsummer-night.txt:I know not by what power I am made bold,\n" +
"midsummer-night.txt:Nor how it may concern my modesty,\n" +
"midsummer-night.txt:In such a presence here to plead my thoughts;\n" +
"midsummer-night.txt:But I beseech your grace that I may know\n" +
"midsummer-night.txt:The worst that may befall me in this case,\n" +
"midsummer-night.txt:If I refuse to wed Demetrius.\n" +
"paradise-lost.txt:Of Mans First Disobedience, and the Fruit\n" +
"paradise-lost.txt:Of that Forbidden Tree, whose mortal tast\n" +
"paradise-lost.txt:Brought Death into the World, and all our woe,\n" +
"paradise-lost.txt:With loss of Eden, till one greater Man\n" +
"paradise-lost.txt:Restore us, and regain the blissful Seat,\n" +
"paradise-lost.txt:Sing Heav'nly Muse, that on the secret top\n" +
"paradise-lost.txt:Of Oreb, or of Sinai, didst inspire\n" +
"paradise-lost.txt:That Shepherd, who first taught the chosen Seed";
Assert.Equal(expected, Grep.Match(pattern, flags, files));
}
private const string IliadFileName = "iliad.txt";
private const string IliadContents =
"Achilles sing, O Goddess! Peleus' son;\n" +
"His wrath pernicious, who ten thousand woes\n" +
"Caused to Achaia's host, sent many a soul\n" +
"Illustrious into Ades premature,\n" +
"And Heroes gave (so stood the will of Jove)\n" +
"To dogs and to all ravening fowls a prey,\n" +
"When fierce dispute had separated once\n" +
"The noble Chief Achilles from the son\n" +
"Of Atreus, Agamemnon, King of men.\n";
private const string MidsummerNightFileName = "midsummer-night.txt";
private const string MidsummerNightContents =
"I do entreat your grace to pardon me.\n" +
"I know not by what power I am made bold,\n" +
"Nor how it may concern my modesty,\n" +
"In such a presence here to plead my thoughts;\n" +
"But I beseech your grace that I may know\n" +
"The worst that may befall me in this case,\n" +
"If I refuse to wed Demetrius.\n";
private const string ParadiseLostFileName = "paradise-lost.txt";
private const string ParadiseLostContents =
"Of Mans First Disobedience, and the Fruit\n" +
"Of that Forbidden Tree, whose mortal tast\n" +
"Brought Death into the World, and all our woe,\n" +
"With loss of Eden, till one greater Man\n" +
"Restore us, and regain the blissful Seat,\n" +
"Sing Heav'nly Muse, that on the secret top\n" +
"Of Oreb, or of Sinai, didst inspire\n" +
"That Shepherd, who first taught the chosen Seed\n";
public GrepTest()
{
Directory.SetCurrentDirectory(Path.GetTempPath());
File.WriteAllText(IliadFileName, IliadContents);
File.WriteAllText(MidsummerNightFileName, MidsummerNightContents);
File.WriteAllText(ParadiseLostFileName, ParadiseLostContents);
}
public void Dispose()
{
Directory.SetCurrentDirectory(Path.GetTempPath());
File.Delete(IliadFileName);
File.Delete(MidsummerNightFileName);
File.Delete(ParadiseLostFileName);
}
}
using System;
using System.IO;
using System.Linq;
public static class Grep
{
delegate string Print(int number, string line, string file, int lenght);
delegate (string line, string pattern) IChoice(string line, string pattern);
delegate bool XChoice((string line, string patt) tuple);
delegate bool VChoice(bool xchoice);
public static string Match(string pattern, string flags, string[] files)
{
string[] lines;
string output = "";
Print print;
IChoice iChoice;
XChoice xChoice;
VChoice vChoice;
if (flags.Contains("-l"))
print = (int number, string line, string file, int lenght) => file + "\n";
else if (flags.Contains("-n"))
print = (int number, string line, string file, int lenght) => lenght <= 1 ? number + ":" + line + "\n" : file + ":" + number + ":" + line + "\n";
else
print = (int number, string line, string file, int lenght) => lenght <= 1 ? line + "\n" : file + ":" + line + "\n";
if (flags.Contains("-i"))
iChoice = (string line, string patt) => (line.ToLower(), patt.ToLower());
else
iChoice = (string line, string patt) => (line, patt);
if (flags.Contains("-x"))
xChoice = ((string line, string patt) tuple) => tuple.line == tuple.patt;
else
xChoice = ((string line, string patt) tuple) => tuple.line.Contains(tuple.patt);
if (flags.Contains("-v"))
vChoice = (bool xchoice) => !xchoice;
else
vChoice = (bool xchoice) => xchoice;
foreach (string file in files)
{
lines = File.ReadAllLines(file);
for (int i = 0; i < lines.Length; i++)
if (vChoice(xChoice(iChoice(lines[i], pattern))))
{
output += print(i + 1, lines[i], file, files.Length);
if (flags.Contains("-l")) { break; }
}
}
if (output != "") { output = output.Substring(0, output.Length - 1); }
return output;
}
}
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments