Shashank Shekher Shashank Shekher - 1 month ago 7
Bash Question

Parse below URL in bash

I have a URL like "http://host:port/cgi-bin/hw.sh/some/path/to/data/".
For the above URL i only need to get the value "/some/path/to/data/".
How can I fetch the required value from the above URL in a shell script.

Answer

If what you want is "everything after hw.sh", it's very easy:

#!/bin/sh
url='http://host:port/cgi-bin/hw.sh/some/path/to/data/'
path=${url#*hw.sh}
echo $path

Which will give you:

/some/path/to/data/

See the "Parameter expansion" section of the bash man page for details.