1

From the manual of grep

‘-f file’
‘--file=file’

Obtain patterns from file , one per line. The empty file contains zero patterns, and therefore matches nothing. (‘-f’ is specified by POSIX.)

When there are multiple lines in the file, how are the patterns specified by each line working together?

Isn't there only one pattern used in each grep command?

0

1 Answer 1

4

When you pass multiple patterns, grep searches for lines that match at least one of the patterns. In other words, passing multiple patterns is equivalent to passing them as a single pattern combined with the “or” operator (| in ERE syntax (grep -E)). This is mostly useful with BRE (plain grep) and string (grep -F) pattern syntax, where the “or” operator is a newline which is annoying to pass as a parameter.

You can pass multiple patterns by repeating the -e option, or by using the -f option with a multi-line file (or multiple times).

2
  • Thanks. For -F, the manual of grep says 'Interpret the pattern as a list of fixed strings, separated by newlines, any of which is to be matched.' (1) Does 'any of which is to be matched' mean an OR operator between fixed strings? (2) Is 'a list of fixed strings, separated by newlines' specified after -F in command line, or specified in a file? If former, doesn't the shell interpret every part between two new line as a complete command?
    – Tim
    Commented Feb 27, 2017 at 9:36
  • @Tim (1) Yes. (2) With -e, the argument can contain newlines and a newline is an “or” operator. (My answer was wrong earlier, this is possible with all regex syntaxes.) If you run grep from a shell, you have to quote the newline to pass it in an argument, same as any other character that has a special meaning to the shell. Commented Feb 27, 2017 at 12:18

You must log in to answer this question.

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