0

I am trying to grep out all of the lines in a file that correspond with words found in another file and then make a third, new file with those lines in it. For example:

File 1
rs2132k34hh
rs234hk5kk4
rsklhh32432

File 2
Info   more info   otherstuff     rs2132k34hh somethings
Info   more info   otherstuff    rs234hk5kk4  somethings
Info   more info   otherstuff     rsklhh32432 somethings
Info   more info   otherstuff    rs234hk5kk4  somethings

I think that it woud be something along the line of

 egrep -l "(\s(rs\S+))" /filethatIamsearching.txt > newfile.txt

grep an rs from a file and send to new file

But I know that this isn't everything that should be in the command. Can someone point me in the right direction?

Stephopolis
  • 1,765
  • 9
  • 36
  • 65

2 Answers2

1

try this:

grep -F -f file1 file2 >newfile.txt
Kent
  • 189,393
  • 32
  • 233
  • 301
  • This seems to be working. Do you mind explaining the -F -f command you recommended? – Stephopolis Feb 22 '13 at 15:07
  • `-f` read Pattern from a file (file1), `-F` looks Pattern as fixed string. `man grep` will give you more info. – Kent Feb 22 '13 at 15:12
  • This won't work if `file2` has a line with `foors234hk5kk4` in it. It will be incorrectly matched. That's why you need `-w` to match words only. – dogbane Feb 22 '13 at 15:26
  • 1
    @dogbane grep is not 100% secure for this, you gave a solution, I can always find exception. e.g what about the keyString not in 4th column, but 3rd? back to your `-w`. if a line in file1 like `.*`, what would happen? you see the necessary of `-F`. or maybe -F and -w better. :) – Kent Feb 22 '13 at 15:35
0

Try:

grep -wf file1 file2 > file3

You can tell grep to read patterns from a file using the -f option. You can also use the -w option to search for whole words only.

dogbane
  • 266,786
  • 75
  • 396
  • 414