Pattern to get string between two specific words/characters using grep [duplicate]

Using grep -oP:

s="<some text> [email protected], <some text>"
grep -oP '(?<=from=).*?(?=,)' <<< "$s"
[email protected]

OR else avoid lookbehind by using \K:

grep -oP 'from=\K.*?(?=,)' <<< "$s"
[email protected]

In case your grep doesn’t support -P (PCRE) use this sed:

sed 's/.*from=\(.*\),.*/\1/' <<< "$s"
[email protected]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)