【问题标题】:How to use varible as search string in egrep如何在 egrep 中使用变量作为搜索字符串
【发布时间】:2017-04-14 11:31:07
【问题描述】:

我想使用 egrep 从文件中执行多字符串搜索,但使用变量来存储搜索字符串值。这将帮助我以编程方式更改搜索字符串。示例:-

SEARCHSTRING='typesetting industry|printer took|specimen'
cat /tmp/input.file|egrep $SEARCHSTRING > /tmp/output.file

input.file 内容:

*Lorem Ipsum is simply dummy text of the printing and typesetting industry.  
Lorem Ipsum has been the industry's standard dummy text ever since the  
1500s, when an unknown printer took a galley of type and scrambled it to  
make a type specimen book. It has survived not only five centuries,   
but also the leap into electronic*  

output.file 内容(预期):

*Lorem Ipsum is simply dummy text of the printing and typesetting industry.  
1500s, when an unknown printer took a galley of type and scrambled it to  
make a type specimen book. It has survived not only five centuries,*   

请帮忙

【问题讨论】:

    标签: bash shell unix scripting grep


    【解决方案1】:

    双引号变量,也使用grep -E而不是egrep

    egrep  "$SEARCHSTRING" inputfile
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    1500s, when an unknown printer took a galley of type and scrambled it to
    make a type specimen book. It has survived not only five centuries,
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 2014-12-18
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      相关资源
      最近更新 更多