Pergunta

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

Foi útil?

Solução

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

Outras dicas

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top