0

I am writing a simple bash script where user input the domain name (example.com) and it greps the email address from whois command output.

I want only grep the email with the same input domain ([email protected]).

Below command works if I write the domain (example.com) but does not work with $domain. I do not know how to use the $domain variable within the grep function. Any help is very much appreciated, thank you.

 whois $domain | grep -m1 -EiEio '\b[A-Z0-9._%+-]+@[$domain]+\.[A-Z]{2,4}\b' 
0

    1 Answer 1

    3

    Variables are not expanded within single quotes. In this case, simply switching to double quotes would probably be enough. Also, remove the [ and ] around $domain. With the square brackets, you would create a bracket expression that would match any single character in the domain name (but not the actual domain name itself).

    In the end, you would end up with something like

    whois "$domain" | grep -m1 -Eio "\b[[:alnum:]._%+-]+@$domain\b" 

    I deleted the \.[A-Z]{2,4} bit at the end of your expression because I didn't actually see any use with it, and I used a symbolic name for matching alphanumeric before the @ sign.

    An additional enhancement would be to replace any dot in $domain with \. before using it in the pattern:

    whois "$domain" | grep -m1 -Eio "\b[[:alnum:]._%+-]+@${domain//./\\.}\b" 

    This requires bash or any other shell that understands the non-standard ${variable//pattern/word} parameter substitution.

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.