I need to extract email address from a string like this (I'm making a log parser):
<some text> [email protected], <some text>
with egrep (or grep -Eo). So the string needs to be pulled out only between "from=" and "," , because the other parts of log contain email addresses too, like to= and etc
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]
Try awk
echo '<text> [email protected], <text>' | awk -F[=,] '{print $2}'
Here $2 can be a different number based on its position.
Sample for word between symbols "(", ")":
echo "Linux Foundation Certified Engineer (LFCE-JP)" | awk -F[\(\)] '{print $2}'
LFCE-JP
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With