如何grep美元符号($)?

Laz*_*zer 66 unix linux grep

% cat temp
$$$ hello1
$$  hello2
    hello3
##  hello4
    hello5 $$$
% cat temp | grep "$$$"
Illegal variable name.
% cat temp | grep "\$\$\$"
Variable name must contain alphanumeric characters.
%
Run Code Online (Sandbox Code Playgroud)

我想要grep $$$,我希望结果是

% cat temp | grep <what should go here?>
$$$ hello1
    hello5 $$$
%
Run Code Online (Sandbox Code Playgroud)

为了区分,我已将提示标记为%.

  • 这里有什么问题?
  • 应该采取什么grep的字符串是什么?

Kon*_*lph 88

问题是shell扩展了双引号字符串中的变量名.因此,"$$$"它尝试从第一个开始读取变量名称$.

另一方面,在单引号中,变量不会扩展.因此,如果不是因为正则表达式中的特殊字符表示行结束,'$$$' 那么它将起作用$.所以它需要被转义:'\$\$\$'.


Gad*_*lin 22

当你使用双引号"或没有使用双\: "\\\$\\\$\\\$"

cat t | grep \\\$\\\$\\\$ 
Run Code Online (Sandbox Code Playgroud)

如果您使用单引号,您可以使用:

cat t | grep '\$\$\$'
Run Code Online (Sandbox Code Playgroud)


小智 8

$ grep '\$\$\$' temp
$$$ hello1
hello5 $$$
Run Code Online (Sandbox Code Playgroud)

你的命令中有一个超级'猫'.


tda*_*ers 5

适合我:

user@host:~$ cat temp | grep '\$\$\$'
$$$ hello1
hello5 $$$
user@host:~$ 
Run Code Online (Sandbox Code Playgroud)