Sha256: 7b8f5309dac1bec62a8220bb88d560ee185efe90ee97d0dc02ac290a61322b4e

Contents?: true

Size: 1.87 KB

Versions: 174

Compression:

Stored size: 1.87 KB

Contents

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:

<pre>
hello
world
hello again
</pre>

If we were to call `grep "hello" input.txt`, the returned string should be:

<pre>
hello
hello again
</pre>

### 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:

<pre>
1:hello
3:hello again
</pre>

And if we run `grep -i "HELLO" input.txt`, we'll do a case-insensitive match, 
and the output will be:

<pre>
hello
hello again
</pre>

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".

Version data entries

174 entries across 174 versions & 1 rubygems

Version Path
trackler-2.1.0.18 common/exercises/grep/description.md
trackler-2.1.0.17 common/exercises/grep/description.md
trackler-2.1.0.16 common/exercises/grep/description.md
trackler-2.1.0.15 common/exercises/grep/description.md
trackler-2.1.0.14 common/exercises/grep/description.md
trackler-2.1.0.13 common/exercises/grep/description.md
trackler-2.1.0.12 common/exercises/grep/description.md
trackler-2.1.0.11 common/exercises/grep/description.md
trackler-2.1.0.10 common/exercises/grep/description.md
trackler-2.1.0.9 common/exercises/grep/description.md
trackler-2.1.0.8 common/exercises/grep/description.md
trackler-2.1.0.7 common/exercises/grep/description.md
trackler-2.1.0.6 common/exercises/grep/description.md
trackler-2.1.0.5 common/exercises/grep/description.md
trackler-2.1.0.4 common/exercises/grep/description.md
trackler-2.1.0.3 common/exercises/grep/description.md
trackler-2.1.0.2 common/exercises/grep/description.md
trackler-2.1.0.1 common/exercises/grep/description.md
trackler-2.1.0.0 common/exercises/grep/description.md
trackler-2.0.8.55 common/exercises/grep/description.md