I am looking for exact string matches for multiple strings using grep.

Lets say I want to find the following three strings in a large data file that has about 10 Million rows and 10 columns. I want to query the file so I find all the rows that have the following exact strings in them. Thankfully only 1 column has 'rs numbers' so I don't need to specify the column: rs123 rs246 rs689653

I tried the following expression and am returned all options that have these strings in them. In some cases, I'll see rows that have something like 'rs123456', where as I just want rows with the exact matches:

grep -E 'rs123|rs246|rs689653' queriedfile.txt

Then I tried the following expression with ^ and $ but received back NO matches to my query (and I know there are some).

grep -E '^rs123$|^rs246$|^rs689653$' queriedfile.txt

2 Answers 2


You can add the -w command line option to restrict the match to whole words

$ printf 'rs123456\nrs246\n' | grep -wE 'rs123|rs246|rs689653'

or add word-anchors explicitly

$ printf 'rs123456\nrs246\n' | grep -E '\b(rs123|rs246|rs689653)\b'

(you were close with ^ and $ - but those are line-anchors).

  • Will i still be returned the full line that the string is in? (that's what I want!)
    – Sheila
    Nov 1, 2018 at 0:57
  • Actually yes! I will be returned the full line! I just tried it! Thank you! I did grep -wE 'rs123|rs246|rs689653' ' queriedfile.txt
    – Sheila
    Nov 1, 2018 at 0:59

If you want exact string matching:

grep -Fwf <(printf "%s\n" rs123 rs246 rs689653) file

Where -F gives you exact string matching (no regex), -w is whole word, and -f reads the "patterns" from the file supplied by the process substitution.

If you have more words to match so that the printf part gets quite long, you can use a shell array to hold them:

words=( rs123 rs246 rs689653 )
grep -Fwf <(printf "%s\n" "${words[@]}") file

Alternatively, awk might work for your needs:

awk -F '\t' '
    $6 == "rs123"
    $6 == "rs246"
    $6 == "rs689653"
' file

Replace your actual field separator and field number as appropriate.

You must log in to answer this question.

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