0

I am trying to search through a number of sites on my server for a few key words and output these to a text file. I only know how to do:

 grep -r "keyword1" * grep -r "keyword2" * 

I need to be able to combine both keywords into the search and output the locations into a text file.

2 Answers 2

6

TIP: use a regexp for better performance and to combine results

grep -r -E "keyword1|keyword2" * > results

Combining both searches into the one will result in better performance for larger searches. You get both sets of results without the overhead of running grep again.

If you want just the actual matching part (not the whole line) then add the -o option.

1
  • 1
    I'm not entirely sure from the question if you are looking for the files, or the lines that match. If you are looking for the lines that match, this is exactly the answer. If you are looking for a list of files that match, add "-l" to the command to list the files that match. Commented Jun 27, 2011 at 14:10
0
#!/bin/bash touch outputfile.txt grep -r "keyword1" * >> outputfile.txt grep -r "keyword2" * >> outputfile.txt 

This shell script wiil create a empty destination file an the following two commands will append the file. The script file must be executable.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.