33

I need to extract email address from a string like this (I'm making a log parser): <some text> from=someuser@somedomain.com, <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

jww
  • 97,681
  • 90
  • 411
  • 885
Shirker
  • 1,233
  • 2
  • 18
  • 30

3 Answers3

58

Using grep -oP:

s='<some text> from=someuser@somedomain.com, <some text>'
grep -oP '(?<=from=).*?(?=,)' <<< "$s"
someuser@somedomain.com

OR else avoid lookbehind by using \K:

grep -oP 'from=\K.*?(?=,)' <<< "$s"
someuser@somedomain.com

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

sed 's/.*from=\(.*\),.*/\1/' <<< "$s"
someuser@somedomain.com
anubhava
  • 761,203
  • 64
  • 569
  • 643
29

Try awk

echo '<text> from=someuser@somedomain.com, <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
0x8BADF00D
  • 7,138
  • 2
  • 41
  • 34
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
18

A purely bash solution, requires two steps to strip prefix & suffix separately (but probably runs faster, because no subprocesses):

#!/bin/bash
orig='from=someuser@somedomain.com, <some text>'
one=${orig#*from=}
two=${one%,*}

printf "Result:\n"
printf "$orig\n"
printf "$one\n"
printf "$two\n"

Output:

Result:
from=someuser@somedomain.com, <some text>
someuser@somedomain.com, <some text>
someuser@somedomain.com

Notes:

  • ${var#*pattern} using # strips from the start of $var up to pattern
  • ${var%pattern*} using % strips from end of $var, up to pattern
  • similar could be accomplished with ${var/pattern/replace} (and leaving replace blank), but it's trickier since full regexp isn't supported (ie, can't use ^ or '$'), so you can't do (for example) /^from=//, but you could do in step one ${var/*from=/} and then in step two, do ${var/,*/} (depending on your data, of course).
  • see also: http://www.tldp.org/LDP/abs/html/parameter-substitution.html
michael
  • 9,161
  • 2
  • 52
  • 49
  • This is elegant. it's approachable for newbie like me, and I can fully understand it after reading it. Thanks for your answer! – NeoZoom.lua Jun 15 '22 at 14:52