在文件中搜索关键字的特定部分和 return 个完整关键字

Search for specific part of keyword in a file and return complete keyword

我有一个格式的文本文件:

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

我需要搜索这个数字和 return 完整的单词,即 1234567_45674345

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

执行此操作的合适方法是什么? 文件大小顺序为10MB

grep -oP '1234567[^ ]+' inputFile
1234567_45674345

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

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