Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a couple of big text files and in the file UNIQS.txt I have a list of strings to grep from another file. The code I use is

grep -f UNIQS.txt EEP_VSL.uniqs.sam > UNIQ_templates.sam

which does nothing - the file generated is empty. But when I do

grep -F -f UNIQS.txt EEP_VSL.uniqs.sam > UNIQ_templates.sam

it works correctly. This confuses me because I didn't think grep would interpret the entries in UNIQS.txt as regexp patterns without quotes and slashes and so on being in the file (which there aren't). Is it the case in general that if you are getting the patterns from a file then it will automatically think that they are regexp patterns?

Edit: In the UNIQS.txt file, there are newline separated strings of the form

HWI-ST365:215:D0GH0ACXX:2:1101:10034:186783

(called template names) and the file EEP_VSL... tab separated columns, with about 14 columns and the first column is the template name, so basically I want to extract the line corresponding to each template in the file.

share|improve this question
    
Could you include a little of the UNIQS.txt file? It's difficult to answer your question without seeing the actual strings, or knowing the nature of these strings. –  slm Jul 16 '13 at 19:07
    
See the edit I added. –  sdf Jul 17 '13 at 15:04

1 Answer 1

The -f option specifies a file where grep reads patterns. That's just like passing patterns on the command line (with the -e option if there's more than one), except that when you're calling from a shell you may need to quote the pattern to protect special characters in it from being expanded by the shell.

The argument -E or -F or -P, if any, tells grep which syntax the patterns are written in. With no argument, grep expects basic regular expressions; with -E, grep expects extended regular expressions; with -P (if supported), grep expects Perl regular expressions; and with -F, grep expects literal strings. Whether the patterns come from the command line or from a file doesn't matter.

Note that the strings are substrings: if you pass a+b as a pattern then a line containing a+b+c is matched. If you want to search for lines containing exactly one of the supplied strings and no more, then pass the -x option.

share|improve this answer
    
Okay that's useful thanks. Can I ask, suppose that I wanted to match the string exactly, i.e if the string is 12345 then I want it to match on 12345 only, not on 123456 or 123455 etc., how would I go about doing this? –  sdf Jul 17 '13 at 15:05
    
@sdf Read my last paragraph: -x –  Gilles Jul 17 '13 at 18:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.