简体   繁体   English

grep手册中-F选项的含义是什么

[英]What is the meaning of the -F option in grep manual

-F is an option of grep , from the manual below: -Fgrep一个选项,来自以下手册:

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 \\ ? 如何分隔多个固定字符串,换行符\\n\\什么?
  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 . 如果我要查找以字符ah开头的行,似乎grep -F a\\nh file无效。

Thanks in advance ! 提前致谢 !

In grep , -F will cause patterns to match literally ie no Regex interpretation is done on the pattern(s). grep-F将导致模式从字面上进行匹配,即,不对模式进行正则表达式解释。

Multiple patterns can be inputted by \\n ie newline separation. \\n可以输入多个模式,即换行符。

Not all shells convert \\n to newline by default, you can use $'a\\nh' in that case. 并非所有的shell默认都将\\n转换为换行符,在这种情况下,您可以使用$'a\\nh'

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

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. 默认情况下,该模式是基本正则表达式(BRE)模式,但使用-F它将被解释为不带任何元字符的文字字符串。

You can also use -E which will enable Extended Regular Expressions (ERE). 您也可以使用-E来启用扩展正则表达式(ERE)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM