【问题标题】:How to grep for the dollar symbol ($)?如何grep美元符号($)?
【发布时间】:2011-04-15 04:00:30
【问题描述】:
% cat temp
$$$ hello1
$$  hello2
    hello3
##  hello4
    hello5 $$$
% cat temp | grep "$$$"
Illegal variable name.
% cat temp | grep "\$\$\$"
Variable name must contain alphanumeric characters.
%

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

% cat temp | grep <what should go here?>
$$$ hello1
    hello5 $$$
%

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

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

【问题讨论】:

  • 你的第二个命令是正确的(用 $ 转义),但你需要使用单引号,而不是双引号。

标签: linux unix grep


【解决方案1】:

当您使用双引号 " 或不使用双引号时,请使用双引号\: "\\\$\\\$\\\$"

cat t | grep \\\$\\\$\\\$ 

如果你在单引号中使用 ' 你可以使用:

cat t | grep '\$\$\$'

【讨论】:

    【解决方案2】:

    问题在于 shell 扩展了双引号字符串中的变量名。因此对于"$$$",它会尝试读取以第一个$ 开头的变量名。

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

    【讨论】:

      【解决方案3】:
      $ grep '\$\$\$' temp
      $$$ hello1
      hello5 $$$
      

      你的命令中有一个多余的“猫”。

      【讨论】:

        【解决方案4】:

        为我工作:

        user@host:~$ cat temp | grep '\$\$\$'
        $$$ hello1
        hello5 $$$
        user@host:~$ 
        

        【讨论】:

          【解决方案5】:

          ^.*[$]{3}.*$怎么样

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-07-13
            • 1970-01-01
            • 2010-12-14
            • 2015-08-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多