Mandeep Singh
Mandeep Singh

Reputation: 308

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

I have a text file of format :

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

Now I have input number which is before underscore 1234567_45674345 for example in this case 1234567

I need to search this number and return the complete word i.e. 1234567_45674345

What I tried : I used grep command but it returns complete line i.e. hello world line 1 1234567_45674345 new line this is hello world

What is the suitable way to do this ? The file size is in order of 10MB

Upvotes: 0

Views: 29

Answers (1)

P....
P....

Reputation: 18391

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

In case you have input number in a variable.

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

Upvotes: 1

Related Questions