【问题标题】:Search for specific part of keyword in a file and return complete keyword在文件中搜索关键字的特定部分并返回完整的关键字
【发布时间】:2023-04-04 00:02:01
【问题描述】:

我有一个格式为:

的文本文件
hello world line 1 1234567_45674345 new line this is hello world 
this is line 2 and new hello world 980765789_32345332
this is line 3 8976578_45345678 end of file

现在我输入了下划线 1234567_45674345 之前的数字,例如在本例中为 1234567

我需要搜索这个号码并返回完整的单词,即1234567_45674345

我尝试了什么: 我使用了grep 命令,但它返回完整的行,即hello world line 1 1234567_45674345 new line this is hello world

这样做的合适方法是什么? 文件大小顺序为10MB

【问题讨论】:

    标签: file search grep keyword-search


    【解决方案1】:
    grep -oP '1234567[^ ]+' inputFile
    1234567_45674345
    

    如果您在变量中输入数字。

    var=1234567
    echo "$x" |grep -oP "$var[^ ]+"
    1234567_45674345
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-31
      • 2019-05-12
      • 2014-05-09
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      相关资源
      最近更新 更多