This question already has an answer here:

I have a script that conducts a Whois lookup on each domain in a list and I need to extract the creation date and the expiration date. I have the first part but can't seem to get the expiration date also. I don't want to run the domain through whois twice...

while IFS="," read domain rdate; do                     # Domain name and received date 
        cdate=( $(whois $domain | grep -m 1 -i Creation| awk -F" " '{print $3}') )
                # Harvest creation date from whois record for each domain 
                # expdate=??????
        printf "$domain,$cdate,$expdate \n"
done < "temp1.txt" 2> /dev/null # Sets the file to read in from and suppress error messages

I tried to set the whois as "whod" like so, with no luck:

whod=$(whois $domain|awk '{print $0}')
expdate=( $(echo $whod |grep -m 1 -i 'Expiration Date' | awk -F" " '{print $3}') )
echo $expdate

marked as duplicate by G-Man, Jakuje, taliezin, Stephen Kitt, Jeff Schaller Feb 1 '16 at 14:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


First of all, there's no point in awk '{print $0}', that just prints each input line. Use whod=$(whois "$domain") instead. Anyway, your main problem is that you're not quoting your variables. This causes all sorts of issues, most importantly that when you echo $whod, what you get is a single line with all the data. Therefore, your awk '{print $3}' is being applied to this line (shorted for clarity):

Whois Server Version 2.0 Domain names in the .com and .net [ ... ]

And the 3rd word is Version which, presumably, is what you get. You're also using arrays (expdate=( $( echo $whod ... ) )) and I don't understand why. Can there be multiple matches? Anyway, a working version of your script is:

while IFS="," read domain rdate; do
  whod="$(whois "$domain")"
  cdate="$(echo "$whod" | grep -m 1 -i Creation| awk -F" " '{print $3}')"
  expdate="$(echo "$whod" |grep -m 1 -i 'Expiration Date' | awk -F" " '{print $3}')"
  printf "$domain,$cdate,$expdate \n"
done < "temp1.txt"

Not the answer you're looking for? Browse other questions tagged or ask your own question.