-2

I'm not sure how to approach this - I'm trying to create a script that will read through every file in a given directory take any lines containing a certain string, let's say 'bla', and copy them to a new file, giving me a file just with lines containing the string I'm searching for.

I'm not even sure where to look at the documentation.. If someone could point me in the right direction or give me some sample code I'd greatly appreciate it.

Mark
  • 6,112
  • 4
  • 21
  • 46

1 Answers1

3

There is no need to use ruby for this; you can simply use grep:

grep -hr "search-string" /path/to/directory/ > output_file

Where -h is the parameter to hide the filename, and -r searches the directory recursively. As from man grep:

-h, --no-filename

Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.

-R, -r, --recursive

Recursively search subdirectories listed.

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • Definitely a better solution - ruby's all I know so I guess I try and force solutions into that even if there are better options out there! Will use Grep now, many thanks. – Mark May 05 '17 at 10:01