0

Folks,

I have a text file which contains multiple lines with one string per line :

str1
str2
str3

etc..

I would like to read every line of this file and then search for those strings inside multiple files located in a different directory.

I am not quite sure how to proceed.

Thanks very much for your help.

3
  • 4
    You want to identify which files (located in another directory) contain any of these strings? Commented Aug 19, 2013 at 18:12
  • Not sure if this most community spirited approach, but I edited things to clarify the question along those lines. My take on the OP's question may be incorrect though. Bluz, if my edits capture the intent of your original question I hope that's ok. If not I apologize. Commented Aug 19, 2013 at 18:39
  • Sorry for the lack of clarity in my question.What I was looking for is a command that reads each line of the pattern file, then lookup that string in every file from $directory and returns both the $Filename and the $StringMatch value when there is one. Commented Aug 20, 2013 at 10:25

4 Answers 4

2
awk 'NR==FNR{a[$0];next} { for (word in a) if ($0 ~ word) print FILENAME, $0 }' fileOfWords /wherever/dir/*
Sign up to request clarification or add additional context in comments.

Comments

2
for wrd in $(cut -d, -f1 < testfile.txt); do grep $wrd dir/files* ; done

1 Comment

I did a for wrd in $(cut -d, -f1 < checks.txt); do grep -o $wrd hosts/*; done instead and it worked just fine :) thanks ! (looks like I can't format code in this comment box?)
2

Use the GNU Grep's --file Option

According to grep(1):

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)

The -H and -n flags will print the filename and line number of each match. So, assuming you store your patterns in /tmp/foo and want to search all files in /tmp/bar, you could use something like:

# Find regular files with GNU find and grep them all using a pattern
# file.
find /etc -type f -exec grep -Hnf /tmp/foo {} +

3 Comments

Thanks for sharing this, I didn't know that grep option. This didn't return anything but I think it's because my question wasn't clear enough in the first place. Thanks for the grep tip though ! :)
@Bluz I've updated the answer to return the filename and line number, and to fix a small syntax error. If you have GNU grep, it should work for you now. Good luck!
Outstanding! Exactly what I needed! And the line number is the icing on the cake. Thanks very much :D
1
while read -r str
do
   echo "$str"
   grep "$str" /path/to/other/files
done < inputfile

3 Comments

Can fail in many different ways thanks to missing setting of IFS, -r argument for read, and quotes around variables.
Also you should add a caveat that it will only work on some shells in some boxes due to the non-portability of echo.
This should work fine in Linux. OP has his question with Linux as one label.

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.