how to grep special character regular expression?

Hi :slight_smile:

I have 2 files

file1:

SNP_A-2208459   
SNP_A-4215188   
SNP_A-2012248   
SNP_A-1882998 

file2:

CHR  SNP  UNADJ       BONF         HOLM     *   *    *  etc.
  19   SNP_A-2236481  1.742e-26  5.442e-21    
  13   SNP_A-4204405  8.643e-07  1.505e-06   
   3    SNP_A-1860908  4.483e-06  7.279e-06
   7    SNP_A-1990194  7.434e-06  1.181e-05
   *
   *
   *
   etc. (thousands of rows)

Note: file2 also contains the entries listed in file1

Question: I'm using grep -w -f file1 file2 > file3 to match in file2 only the entries listed in file1, BUT this does not work :confused: ; the output file3 is giving me everything listed in file2 :(.

Can anyone help? pleaaaaaaaaaaaaase

Thank you!

What does -F do if used instead of -f?

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

-F does not work only either.

  1. -w means word. And -(hyphen) is not a word character.

So file3 will not have any content, ( by the sample input you gave ). Confirm whether you are right in saying that.

  1. Solution.
grep -f file1 file2

Let us know whether this helped.

thegeek;

Thank you for trying to help me, but grep -f file1 file2 > file3
DOES NOT WORK.

---------- Post updated 09-25-09 at 03:56 PM ---------- Previous update was 09-24-09 at 09:47 PM ----------

I also deleted the hyphens, underscores and letters from the in both files but the grep -f file1 file2 > file3 does not work
SNP_A-2208459

---------- Post updated at 03:58 PM ---------- Previous update was at 03:56 PM ----------

Ok, I give up with this forum, I guess nobody wants to help.

did you try the man pages?

i think diff may be another way around it check man diff also

Maybe this will work:

cat file1 | while read LINE
do
grep -e $LINE file2 >> file3
done

mark54g:
Could it be that grep is limited to certain line numbers. When I had close to 1/2 million columns I did not have a problem. BUT, now that I have that many rows, grep does not seem to work.

treedroppings:
Yes I have tried the man pages since I know very little about unix.

why don't you use the below code,

for line in `cat file1`
do
grep -w "$line" file2 >>file3
done

Could you feed it to cat and then use xargs to append the file?

mark54g and apsprabhu;

You were very helpful. THANK YOU!. This code works:

file="file1.txt"
while read line
do
grep $line file2 >> file3
done < $file