28

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
  • 90,984
  • 81
  • 374
  • 818
Shirker
  • 1,081
  • 2
  • 17
  • 30

3 Answers3

50

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
  • 713,503
  • 59
  • 514
  • 593
24

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
  • 6,454
  • 2
  • 39
  • 34
Shiplu Mokaddim
  • 54,465
  • 14
  • 131
  • 183
15

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
  • 7,805
  • 2
  • 45
  • 48