Tracy
Tracy

Reputation: 1998

What is the meaning of the -F option in grep manual

-F is an option of grep, from the manual below:

interpret pattern as a list of fixed strings,separated by newlines,any of which is to be matched

My question is

  1. How to separated multiple fixed strings, what is the newline character, \n or \?
  2. It seems grep -F a\nh file is not valid if I want to find lines which starts with a character a or h.

Thanks in advance !

Upvotes: 3

Views: 3608

Answers (2)

heemayl
heemayl

Reputation: 42097

In grep, -F will cause patterns to match literally i.e. no Regex interpretation is done on the pattern(s).

Multiple patterns can be inputted by \n i.e. newline separation.

Not all shells convert \n to newline by default, you can use $'a\nh' in that case.

Example:

$ echo $'foo\nf.o\nba.r\nbaar\n'
foo
f.o
ba.r
baar

$ grep -F $'f.o\nba.r' <<<$'foo\nf.o\nba.r\nbaar\n'
f.o
ba.r

Upvotes: 5

Andreas Louv
Andreas Louv

Reputation: 47119

By default the pattern is a Basic Regular Expressions (BRE) pattern, but with -F it will be interpreted as a literal string with no metacharacters.

You can also use -E which will enable Extended Regular Expressions (ERE).

% grep -F '..' <<< $'hello\nworld\n...'
...
% grep '..' <<< $'hello\nworld\n...'
hello
world
...

Upvotes: 1

Related Questions