2017-07-12 46 views
0

我有格式的文本文件:搜寻关键字的特定部分的文件,并返回完整的关键字

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

回答

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

为了万一你有一个变量输入号码。

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