grep手册中的-F选项是什么意思

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

-Fgrep 的一个选项,来自以下手册:

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

我的问题是

  1. 如何分隔多个固定字符串,换行符是什么,\n还是\
  2. 如果我想查找以字符 ah.
  3. 开头的行,似乎 grep -F a\nh file 无效

提前致谢!

默认情况下,模式是基本正则表达式 (BRE) 模式,但是 -F 它将被解释为没有元字符的文字字符串。

您还可以使用 -E,这将启用扩展正则表达式 (ERE)。

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

grep 中,-F 将导致模式按字面匹配,即不对模式进行正则表达式解释。

可以输入多种模式\n即换行

并不是所有的 shell 默认都会将 \n 转换为换行符,在这种情况下您可以使用 $'a\nh'

示例:

$ 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