1

I'm quite unfamiliar with Unix, and I am trying to understand how to perform grep through a list of text files, using patterns stored in a text file called searchlist.txt. The text file is a new-line separated list of names that looks something like this:

"washington, martha"

"adams, jane"

And I need to search for these names in some very large text files. I also need to ignore case. I've come up with grep -i -f searchlist.txt *.txt, but the only output I am getting looks like this:

"adams, jane":"washington, martha"

I know for certain that these names are somewhere in the text files. I wonder if there is some specific way that searchlist.txt needs to be formatted? Is there some way I need to specify in the command that this list is separated by new lines?

3
  • Does your text file include the double quote marks? If not, please remove them.
    – roaima
    Jan 11, 2017 at 20:39
  • Did the file come from a Windows or Mac system? If so, please transfer it using text mode and try again. Or convert it from Windows/Mac format to Unix format.
    – roaima
    Jan 11, 2017 at 20:39
  • These two fixes did the trick. Thank you very much!
    – user209924
    Jan 11, 2017 at 20:47

1 Answer 1

1

If you have a file called 'needles' containing the following:

washington, martha
adams, jane

And a file called 'haystack' containing the following:

blah blah blah washington, martha blah
all work and no play
makes adams, jane a dull
girl

You should be able to use grep -i -f needles haystack.

If, however, 'needles' contains:

"washington, martha"
"adams, jane"

you will need to use grep -i -f <(sed 's/"//g' needles) haystack to strip the double-quotes off of the names. Unless, that is, you want to search for only the names when quoted.

Naturally, this will not work for cases of 'haystack' such as

And on that day she changed her name to adams,
jane.

If someone on your list is inspired by The Artist Formerly Known As The Artist Formerly Known As Prince and has decided to put a double-quote into their actual name, you can make the sed a little more explicit and only strip the leading and trailing quote from each line with grep -i -f <(sed 's/^"//;s/"$//' needles) haystack.

1
  • This is great, thank you. In addition to stripping quotes, I'd needed to convert the text file to Unix format. Thanks again.
    – user209924
    Jan 11, 2017 at 20:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .