I have url like:
sftp://user@host.net/some/random/path
I want to extract user, host and path from this string. Any part can be random length.
I have url like:
sftp://user@host.net/some/random/path
I want to extract user, host and path from this string. Any part can be random length.
I did further parsing, expanding the solution given by @Shirkrin:
[EDIT 2019] This answer is not meant to be a catch-all, works for everything solution it was intended to provide a simple alternative to the python based version and it ended up having more features than the original.
It answered the basic question in a bash-only way and then was modified multiple times by myself to include a hand full of demands by commenters. I think at this point however adding even more complexity would make it unmaintainable. I know not all things are straight forward (checking for a valid port for example requires comparing
hostport
andhost
) but I would rather not add even more complexity.[Original answer]
Assuming your URL is passed as first parameter to the script:
I must admit this is not the cleanest solution but it doesn't rely on another scripting language like perl or python. (Providing a solution using one of them would produce cleaner results ;) )
Using your example the results are:
This will also work for URLs without a protocol/username or path. In this case the respective variable will contain an empty string.
[EDIT]
If your bash version won't cope with the substitutions (${1/$proto/}) try this:
This solution in principle works the same as Adam Ryczkowski's, in this thread - but has improved regular expression based on RFC3986, (with some changes) and fixes some errors (e.g. userinfo can contain '_' character). This can also understand relative URIs (e.g. to extract query or fragment).
I did not like above methods and wrote my own. It is for ftp link, just replace
ftp
withhttp
if your need it. First line is a small validation of link, link should look likeftp://user:pass@host.com/path/to/something
.My actual goal was to check ftp access by url. Here is the full result:
Using Python (best tool for this job, IMHO):
Further reading:
os.environ
urlparse.urlparse()
If you have access to Node.js:
This will output: