Getting Root Domain for String in Shell9 months ago
I was looking for a quick way to determine the root domain of a host passed to me in a shell script, this is what i came up with:
awk -F . '{print $(NF-1)"."$NF}'
Examples
njp-mbp: ~> echo "domain.com" | awk -F . '{print $(NF-1)"\."$NF}'
domain.com
njp-mbp: ~> echo "sub.domain.com" | awk -F . '{print $(NF-1)"\."$NF}'
domain.com
njp-mbp: ~> echo "longer.sub.domain.com" | awk -F . '{print $(NF-1)"\."$NF}'
domain.com
njp-mbp: ~> echo "longer-domain.com" | awk -F . '{print $(NF-1)"\."$NF}'
longer-domain.com



