【问题标题】:grep with variable and string combined [duplicate]grep与变量和字符串组合[重复]
【发布时间】:2016-05-07 05:55:19
【问题描述】:

我希望能够使用 grep 结合变量和字符串进行搜索。

test="HTTP/[0-9].[0-9]" 
grep '$test [0-9][0-9][0-9] ||' some_file

尝试将变量放入 ${test} 中,但没有帮助。

如何将 grep 参数与变量和字符串结合起来?

【问题讨论】:

    标签: regex bash variables grep


    【解决方案1】:

    您必须用双引号将您的模式括起来以启用变量扩展:

    test="HTTP/[0-9].[0-9]" 
    grep "$test [0-9][0-9][0-9] ||" some_file
    

    test='expanded'
    echo '$test' # will echo $test
    echo "$test" # will echo expanded
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 2014-07-20
      • 2011-01-07
      • 1970-01-01
      • 2018-09-23
      相关资源
      最近更新 更多