# Grep Search a file for lines matching a regular expression pattern. Return the line number and contents of each matching line. The Unix [`grep`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html) 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: 1. The pattern used to match lines in a file. 2. Zero or more flags to customize the matching behavior. 3. One or more files in which to search for matching lines. 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: ```text hello world hello again ``` If we were to call `grep "hello" input.txt`, the returned string should be: ```text hello hello again ``` ### Flags 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: ```text 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: ```text 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". # Java Tips Since this exercise has difficulty 5 it doesn't come with any starter implementation. This is so that you get to practice creating classes and methods which is an important part of programming in Java. It does mean that when you first try to run the tests, they won't compile. They will give you an error similar to: ``` path-to-exercism-dir\exercism\java\name-of-exercise\src\test\java\ExerciseClassNameTest.java:14: error: cannot find symbol ExerciseClassName exerciseClassName = new ExerciseClassName(); ^ symbol: class ExerciseClassName location: class ExerciseClassNameTest ``` This error occurs because the test refers to a class that hasn't been created yet (`ExerciseClassName`). To resolve the error you need to add a file matching the class name in the error to the `src/main/java` directory. For example, for the error above you would add a file called `ExerciseClassName.java`. When you try to run the tests again you will get slightly different errors. You might get an error similar to: ``` constructor ExerciseClassName in class ExerciseClassName cannot be applied to given types; ExerciseClassName exerciseClassName = new ExerciseClassName("some argument"); ^ required: no arguments found: String reason: actual and formal argument lists differ in length ``` This error means that you need to add a [constructor](https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html) to your new class. If you don't add a constructor, Java will add a default one for you. This default constructor takes no arguments. So if the tests expect your class to have a constructor which takes arguments, then you need to create this constructor yourself. In the example above you could add: ``` ExerciseClassName(String input) { } ``` That should make the error go away, though you might need to add some more code to your constructor to make the test pass! You might also get an error similar to: ``` error: cannot find symbol assertEquals(expectedOutput, exerciseClassName.someMethod()); ^ symbol: method someMethod() location: variable exerciseClassName of type ExerciseClassName ``` This error means that you need to add a method called `someMethod` to your new class. In the example above you would add: ``` String someMethod() { return ""; } ``` Make sure the return type matches what the test is expecting. You can find out which return type it should have by looking at the type of object it's being compared to in the tests. Or you could set your method to return some random type (e.g. `void`), and run the tests again. The new error should tell you which type it's expecting. After having resolved these errors you should be ready to start making the tests pass! # Running the tests You can run all the tests for an exercise by entering ```sh $ gradle test ``` in your terminal. ## Source Conversation with Nate Foster. [http://www.cs.cornell.edu/Courses/cs3110/2014sp/hw/0/ps0.pdf](http://www.cs.cornell.edu/Courses/cs3110/2014sp/hw/0/ps0.pdf) ## Submitting Incomplete Solutions It's possible to submit an incomplete solution so you can see how others have completed the exercise.