如何在不转义的情况下grep星号? [英] How to grep asterisk without escaping?

查看:927
本文介绍了如何在不转义的情况下grep星号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个文件 abc.txt ,其中包含行 ab * cd 。当我 grep 时,该模式 ab * cd 带引号但没有转义星号的情况不起作用:

Suppose I have a file abc.txt which contains line ab*cd. When I grep that pattern ab*cd with quotes but without escaping the asterisk it does not work:


> grep ab*c abc.txt 
> grep "ab*c" abc.txt 
> grep 'ab*c' abc.txt 

当我同时使用 引号和转义确实可以

When I use both quotes and escaping it does work


> grep "ab\*c" abc.txt 
ab*cd
> grep 'ab\*c' abc.txt 
ab*cd

现在我想知道为什么引号不起作用以及是否可以使用

Now I wonder why the quotes do not work and if I can use only quotes without escaping the asterisk.

推荐答案

使用标记 -F 搜索固定字符串-而不是正则表达式。从 man grep

Use the flag -F to search for fixed strings -- instead of regular expressions. From man grep:

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

例如:

$ grep -F "ab*c" <<< "ab*c"
ab*c

这篇关于如何在不转义的情况下grep星号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆