LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem using grep to search for multiple strings listed in a file (https://www.linuxquestions.org/questions/programming-9/problem-using-grep-to-search-for-multiple-strings-listed-in-a-file-4175478486/)

scruffbag 09-25-2013 07:53 AM

Problem using grep to search for multiple strings listed in a file
 
Hi - I'm having a problem using grep to extract lines from a data file that contain one of several different strings. I am trying to load the strings from a file - hence, the code I'm using is:

grep -f file.containing.strings data.file

When I run this, grep doesn't return any output.

The strings are listed in my file one per line, and I have tried listing the strings in this file both with and without single quotation marks. I have also checked that (at least some of) the strings I'm searching for are definitely in the data file, using the regular:
grep 'string' data.file

Any ideas what I'm doing wrong?

Thanks!

danielbmartin 09-25-2013 08:14 AM

Quote:

Originally Posted by scruffbag (Post 5034502)
Any ideas what I'm doing wrong?

Help us to help you. Provide sample input files (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.

Daniel B. Martin

ntubski 09-25-2013 08:16 AM

If your strings are not regular expressions you should use -F to indicate that:
Code:

grep -Ff file.containing.strings data.file
If that doesn't work, post the input files: it's hard to help blindly...

scruffbag 09-25-2013 08:22 AM

Hi - thanks for the responses but I've just managed to solve the problem. I originally created the input file under Windows, so it had Windows line ends (which I was copying and pasting into new files created in Linux). I've just resaved it with Linux line ends and, magically, grep is now working on my file!

Hopefully this might at least help someone else to avoid doing this in future!

Cheers,

Nick


All times are GMT -5. The time now is 01:51 AM.