1

I feel like beating my head off my desk so any help is appreciated.

#!/bin/bash
echo 'wahegru.com mail is handled by 1 ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 10 ALT4.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 5 ALT1.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 10 ALT3.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 5 ALT2.ASPMX.L.GOOGLE.com.' |  grep '(?<=10 )(\.*+\w)*+'

Now I know this regex works:

(?<=10 )(\.*+\w)*+

Because I can paste the text into https://regex101.com/ and the regex and it finds what I need as a full match.

Why doesn't it work for grep though?

Edit, tried this:

echo 'wahegru.com mail is handled by 1 ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 10 ALT4.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 5 ALT1.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 10 ALT3.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 5 ALT2.ASPMX.L.GOOGLE.com.' |  grep -P '(?<=10 )(\.*+\w)*+'

But it gives me this:

usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]

Tried

echo 'wahegru.com mail is handled by 1 ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 10 ALT4.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 5 ALT1.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 10 ALT3.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 5 ALT2.ASPMX.L.GOOGLE.com.' | perl -nle'print if m{(?<=10 )(\.*+\w)*+}'

But that outputs the wrong stuff:

wahegru.com mail is handled by 1 ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 10 ALT4.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 5 ALT1.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 10 ALT3.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 5 ALT2.ASPMX.L.GOOGLE.com.
Jack Robson
  • 2,184
  • 4
  • 27
  • 50

1 Answers1

1

You may use

echo 'wahegru.com mail is handled by 1 ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 10 ALT4.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 5 ALT1.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 10 ALT3.ASPMX.L.GOOGLE.com. wahegru.com mail is handled by 5 ALT2.ASPMX.L.GOOGLE.com.' | \ 
  grep -o '10 [^ .]*\(\.[^ .][^ .]*\)*' | head -1 | sed 's/^10 //'

See the grep demo online

Details

  • o option extracts the matches found in the input string
  • 10 [^ .]*\(\.[^ .][^ .]*\)* matches
    • 10 - a literal substring
    • [^ .]* - 0+ chars other than space and .
    • \(\.[^ .][^ .]*\)* - 0 or more repetitions of
      • \. - a dot
      • [^ .][^ .]* - 1 or more chars other than . and space
  • head -1 - gets the first match
  • sed 's/^10 //' removes the initial 10 substring.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563