Monday, November 1, 2021

[SOLVED] How to get match of a pattern even if it is splitted by characters using a bash command (similar to grep)?

Issue

I'm trying to output all the lines of a file which contain a specific word/pattern even if it contains other characters between its letters.

Let's say we have a bunch of domain names and we want to filter out all those that contain "paypal" inside, I would like to have this kind of output :

pay-pal-secure.com
payppal.net
etc...

I was wondering if this is possible with grep or does it exist something else that might do it.

Many thanks !


Solution

Replace paypal with regexp p.*a.*y.*p.*a.*l to allow all characters between the letters.

Update:

Use extended regular expression p.{0,2}a.{0,2}y.{0,2}p.{0,2}a.{0,2}l to limit characters between the letters to none to two.

Example: grep -E 'p.{0,2}a.{0,2}y.{0,2}p.{0,2}a.{0,2}l' file

See: The Stack Overflow Regular Expressions FAQ



Answered By - Cyrus