Skip to content

Instantly share code, notes, and snippets.

@leodotcloud
Created March 9, 2018 18:55
Show Gist options
  • Save leodotcloud/c4cbedaabeba0a06956188a04022e9ce to your computer and use it in GitHub Desktop.
Save leodotcloud/c4cbedaabeba0a06956188a04022e9ce to your computer and use it in GitHub Desktop.
Extract just the domain name or IP address from URL string
#!/bin/bash
get_domain_name() {
echo "$1" | awk -F/ '{print $3}' | sed 's/:.*//'
}
URLS=$(cat <<-END
http://1.1.1.1
http://1.1.1.1:8443
https://1.1.1.1
https://1.1.1.1:8443
http://hello-world
http://hello-world.com
http://sub.hello-world.com
http://hello-world:8080
http://hello-world.com:8080
http://sub.hello-world.com:8080
https://hello-word/foo/bar
END
)
for TEST_URL in ${URLS}; do
echo "For URL:${TEST_URL}, domain name:$(get_domain_name ${TEST_URL})"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment