开发者

grep match exact substring ignoring regex syntax [duplicate]

This question already has answers here: 开发者_如何学C Grep for literal strings (6 answers) Closed 6 years ago.

Is there some way to make grep match an exact string, and not parse it as a regex? Or is there some tool to escape a string properly for grep?

$ version=10.4
$ echo "10.4" | grep $version
10.4
$ echo "1034" | grep $version # shouldn't match
1034


Use grep -F or fgrep.

$ echo "1034" | grep -F $version # shouldn't match
$ echo "10.4" | grep -F $version
10.4

See man page:

   -F, --fixed-strings
         Interpret PATTERN as a list of fixed strings, separated
         by newlines, any of which is to be matched.

I was looking for the term "literal match" or "fixed string".

(See also Using grep with a complex string and How can grep interpret literally a string that contains an asterisk and is fed to grep through a variable?)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜