简体   繁体   中英

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

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 !

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

Alternatively you could use agrep (approximate grep):

$ agrep -By paypal file
agrep: 2 words match within 1 error
pay-pal-secure.com
payppal.net

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM