Linux 如何grep美元符号($)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3759926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to grep for the dollar symbol ($)?
提问by Lazer
% cat temp
$$$ hello1
$$ hello2
hello3
## hello4
hello5 $$$
% cat temp | grep "$$$"
Illegal variable name.
% cat temp | grep "$$$"
Variable name must contain alphanumeric characters.
%
I want to grep for $$$
and I expect the result to be
我想 grep for$$$
并且我希望结果是
% cat temp | grep <what should go here?>
$$$ hello1
hello5 $$$
%
To differentiate, I have marked the prompt as %
.
为了区分,我将提示标记为%
.
- What is the problem here?
- What should the grep stringbe?
- 这里有什么问题?
- 应该采取什么grep的字符串是什么?
采纳答案by Konrad Rudolph
The problem is that the shell expands variable names inside double-quoted strings. So for "$$$"
it tries to read a variable name starting with the first $
.
问题在于 shell 在双引号字符串中扩展变量名。因此,"$$$"
它尝试读取以第一个开头的变量名称$
。
In single quotes, on the other hand, variables are not expanded. Therefore, '$$$'
wouldwork –?if it were not for the fact that $
is a special character in regular expressions denoting the line ending. So it needs to be escaped: '\$\$\$'
.
另一方面,在单引号中,变量不会被扩展。因此,如果不是正则表达式中表示行尾的特殊字符,它'$$$'
会起作用$
。所以它需要被转义:'\$\$\$'
.
回答by F.P
How about ^.*[$]{3}.*$
怎么样 ^.*[$]{3}.*$
回答by tdammers
Works for me:
对我有用:
user@host:~$ cat temp | grep '$$$'
$$$ hello1
hello5 $$$
user@host:~$
回答by Gadolin
When you use double quotes "
or none use double\: "\\\$\\\$\\\$"
当您使用双引号"
或不使用双引号时\: "\\\$\\\$\\\$"
cat t | grep \$\$\$
if you use in single quotes ' you may use:
如果您在单引号中使用 ' 您可以使用:
cat t | grep '$$$'
回答by jowi
$ grep '$$$' temp
$$$ hello1
hello5 $$$
There's a superflous 'cat' in your command.
你的命令中有一个多余的“猫”。
回答by haykp
Same issue with this command
这个命令有同样的问题
complete -W "\`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|$)' Makefile \`"
Illegal variable name.
非法的变量名。