Friday, July 22, 2022

[SOLVED] using linux grep with look ahead regexp

Issue

I have string in txt file

cat list.txt
[email protected], [email protected], [email protected]

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]([email protected],)' list.txt

but nothing happens, why? It will be like:

userone
usertwo
userthree

Thanks.


Solution

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


Answered By - anubhava
Answer Checked By - Marie Seifert (WPSolving Admin)