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

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]

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

#!/bin/bash
orig='[email protected], <some text>'
one=${orig#*from=}
two=${one%,*}

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

Output:

Result:
[email protected], <some text>
[email protected], <some text>
[email protected]

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

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

Tags:

Regex

Bash

Grep