A simple command-line tool written in Go to extract specific components from a given URL. Especially useful in Bash scripts or automation workflows where URL processing is required.
- Extract various components of a URL, including:
- Host
- Domain
- Subdomain
- Top-level domain (TLD)
- Port
- Path
- Fragment
- Scheme (protocol)
- Registered domain from a subdomain (
domain.tld
) - Query parameter values
Use wget to download pre-compiled binaries:
For instance, VERSION=v1.0.0 and BINARY=url-parser-linux-amd64
wget https://github.com/singh-inder/url-parser/releases/download/${VERSION}/${BINARY} -O /usr/local/bin/url-parser &&\
chmod +x /usr/local/bin/url-parser
wget https://github.com/singh-inder/url-parser/releases/latest/download/url-parser-linux-amd64 -O /usr/local/bin/url-parser &&\
chmod +x /usr/local/bin/url-parser
url-parser --url <URL> --get <component>
--url
: The URL to parse. Example:https://example.com
.--get
: The component to extract. Options:host
: url host (port number if present is included) (e.g.,subdomain.example.com:1234
)hostWithoutPort
: url host (port number removed) (eg.,subdomain.example.com
)domain
: Domain name (e.g.,example
)subdomain
: Subdomain (e.g.,sub
)tld
: Top-level domain (e.g.,com
)port
: Port (if specified, e.g.,8080
)path
: URL path (e.g.,/path/to/resource
)fragment
: Fragment (e.g.,section1
)scheme
: URL scheme (e.g.,https
)registeredDomain
: Registered domain from a subdomain (e.g.,example.com
)query.<paramName>
: Value of a specific query parameter (e.g.,query.user
extracts the value of theuser
parameter).
url-parser --url "https://sub.example.com/path?user=123" --get domain
# Output: example
url-parser --url "https://example.com/path?user=123" --get query.user
# Output: 123
url-parser --url "https://example.com" --get tld
# Output: com
url-parser --url "https://example.com" --get scheme
# Output: https
url-parser --url "https://sub.example.com" --get host
# Output: sub.example.com
url-parser --url "https://sub.example.com" --get registeredDomain
# Output: example.com
Feel free to submit issues or create pull requests to improve the project. Contributions are always welcome!
This project is licensed under the MIT License.
This cli uses go-tld package for extracting URL components.