7

Okay so I have a textfile containing multiple strings, example of this -

Hello123
Halo123
Gracias
Thank you
...

I want grep to use these strings to find lines with matching strings/keywords from other files within a directory

example of text files being grepped -

123-example-Halo123
321-example-Gracias-com-no
321-example-match

so in this instance the output should be

123-example-Halo123
321-example-Gracias-com-no
3
  • 2
    The goal is that you add some code of your own to show at least the research effort you made to solve this yourself. Commented Jan 5, 2018 at 20:39
  • Well I know that if I use " Grep -rhI "Halo123" . " then it would output any lines containing Halo123 within the current directory.. but I need to know how I would do it for multiple keywords :). Commented Jan 5, 2018 at 20:42
  • @Cyrus forgot to tag Commented Jan 5, 2018 at 20:42

3 Answers 3

11

With GNU grep:

grep -f file1 file2

-f FILE: Obtain patterns from FILE, one per line.

Output:

123-example-Halo123
321-example-Gracias-com-no
Sign up to request clarification or add additional context in comments.

4 Comments

How do I set exact string? so like string has to match "Halo123", as if line contains example, 123-example-Halo it will class that as being a match and output, yet it didn't contain "Halo123"
I suggest to add option --line-regexp to grep.
I understand that the OP wants to do a literal match. You better add -F.
--line-regexp is perhaps not suitable here. The OP seems to look for substrings in the file to be searched.
2

You should probably look at the manpage for grep to get a better understanding of what options are supported by the grep utility. However, there a number of ways to achieve what you're trying to accomplish. Here's one approach:

grep -e "Hello123" -e "Halo123" -e "Gracias" -e "Thank you" list_of_files_to_search

However, since your search strings are already in a separate file, you would probably want to use this approach:

grep -f patternFile list_of_files_to_search

2 Comments

got error " list.txt invalid range end " & " list.txt unmatched [, [^, [:, [. or [= "
The patternFile must contain a list of regular expressions/patterns. Based on the error message you posted, it sounds like one or more of the 'patterns' contains some regular expression metacharacters but doesn't form a valid regular expression. Please post the offending pattern(s) , without that, it's impossible to tell what's going wrong.
0

I can think of two possible solutions for your question:

  1. Use multiple regular expressions - a regular expression for each word you want to find, for example:

    grep -e Hello123 -e Halo123 file_to_search.txt
    
  2. Use a single regular expression with an "or" operator. Using Perl regular expressions, it will look like the following:

    grep -P "Hello123|Halo123" file_to_search.txt
    

EDIT: As you mentioned in your comment, you want to use a list of words to find from a file and search in a full directory.

You can manipulate the words-to-find file to look like -e flags concatenation:

cat words_to_find.txt | sed 's/^/-e "/;s/$/"/' | tr '\n' ' '

This will return something like -e "Hello123" -e "Halo123" -e "Gracias" -e" Thank you", which you can then pass to grep using xargs:

cat words_to_find.txt | sed 's/^/-e "/;s/$/"/' | tr '\n' ' ' | dir_to_search/*

As you can see, the last command also searches in all of the files in the directory.

SECOND EDIT: as PesaThe mentioned, the following command would do this in a much more simple and elegant way:

grep -f words_to_find.txt dir_to_search/*

3 Comments

i need to run keywords/strings from a .txt file, also need to search for matches from files within a folder, not just a single .txt
@user3255841 Edited my answer to match your clarified question.
sed accepts files -- sed 's/....' words. But what is it for anyway? grep -E 'aaa|bbb' /some/path/*? Or grep -f words.txt /some/path/*.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.