Combine the lines based on particular pattern

Hi,
I've a weird problem to be solved. Assume i have a file like this:

  1. <timestamp> UID: 12345 <junk> DevID: V123
  2. <timestamp>DevID: V123 <junk> DuID: VP
  3. ...
  4. ....
  5. <timestamp> UID: 789 <junk> DevID: S456
  6. <timestamp>DevID: S456 <junk> DuID: VP....
  7. .....

Say if i search for "VP", the output should look like
<timestamp> UID: 12345 DevID: V123 DuID: VP
<timestamp> UID: 789 DevID: S456 DuID: VP

Similarly I should do this for last 20 occurrences of "VP".

I need a command which gets the above result

Appreciate your help

grep VP file | tail -20

I'm not suere if you want the las 20ocurrency of VP in this case try:

$grep VP name_file | tail -20

If you want all the ocurrencies:

$grep VP name_file

Thanks for ur immediate response.

But along with the lines which has the string "VP", i need the lines which also share the same DevID along with the line it Contain "VP".... In this case, it is V123. I need to combine these lines. Can you help it out?

  1. <timestamp> UID: 12345 <junk> DevID: V123
  2. <timestamp>DevID: V123 <junk> DuID: VP