using linux grep with look ahead regexp

Viewed 30

I have string in txt file

cat list.txt
userone@ex.com, usertwo@ex.com, userthree@ex.com

and i want to print every user login w/o @ex.com each new line and try to use regexp with linux grep

grep -oe '[a-z](?=@ex.com,)' list.txt

but nothing happens, why? It will be like:

userone
usertwo
userthree

Thanks.

1 Answers

Without grep -P, you can use grep + cut:

grep -oE '[^@ ]+@ex\.com' list.txt | cut -d@ -f1

userone
usertwo
userthree

With gnu grep:

grep -oP '[^@ ]+(?=@ex\.com)' list.txt

userone
usertwo
userthree
Related