Question

I have a variable as below

a="My name is A. Her Name is B. His Name is C"

I need to print the text between "name" and "."

Expected output is

is A
is B
is C

EDIT: Solution should work both on Solaris and Linux Machine

Was it helpful?

Solution

This works with GNU grep

$ grep -ioP '(?<=name ).+?(?=\.|$)' <<< "$a"
is A
is B
is C

This works with on solaris 5.8 with /usr/xpg4/bin/awk

$ echo "$a" | awk -F '[nN]ame |\\.' '{for (i=2; i<=NF; i+=2) print $i}'
is A
is B
is C

OTHER TIPS

Using awk:

echo "$a"|awk -F '[nN]ame ' '{print $2}' RS='.'
is A
is B
is C

Assuming you have bash on Solaris, the following should work:

while read -r l; do
  echo "${l//*[Nn]ame /}";
done < <(tr '.' '\n' <<< "$a")

For your input, it'd produce:

is A
is B
is C
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top