带有&符号的Linux grep

时间:2019-12-05 21:07:20

标签: grep

我可能在这里错过了一些愚蠢的东西,但是我无法弄清楚。基本上,我有一个SAS宏变量的列表,我想知道在我的代码库中使用它们的位置。对于那些不了解SAS并基本上使用宏变量的人,可以使用&符号进行处理,因此,如果将该变量命名为var1,则可以通过&var1(或&var1。来引用它),但这对这个问题并不重要。

所以我一直在使用grep进行搜索,如下所示:

grep -iRw "&macro_variable" *

我无法复制/粘贴确切的结果,但是我运行了以下命令,并返回了0个结果:

grep -iRw "&lead_year" *

然后我运行以下内容:

grep -iRw "lead_year" *

它返回了这一行:

  

parameters.csv:HPI_Hist_File,rreap_hpi_data_&lead_year。&lead_month。&lead_day.txt,rreap_hpi_data_&lead_year。&lead_month。&lead_day..txt

当我搜索lead_year而不搜索&lead_year时,为什么grep会查找前面带有&符号的文件?这也不是一贯的,对于我大多数使用&号开头的搜索,它都能成功找到所有结果,到目前为止,这只是一个问题。

1 个答案:

答案 0 :(得分:2)

这是因为您的-w。

   -w, --word-regexp
          Select only those lines containing matches that form whole words.  The test is that the matching substring must either be at the beginning of the line, or preceded by
          a non-word constituent character.  Similarly, it must be either at the end of the line or followed by a non-word constituent character.   Word-constituent  characters
          are letters, digits, and the underscore.

关键部分是“在非单词组成字符之前”。在您的示例中,&lead_year之前带有下划线。

相关问题