Shashank Shekher
Shashank Shekher

Reputation: 897

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.

Upvotes: 0

Views: 2045

Answers (2)

Zlemini
Zlemini

Reputation: 4963

You can use the awk -F option to specify "hw.sh" as the field input separator and print the second field:

$ echo "http://host:port/cgi-bin/hw.sh/some/path/to/data/" | awk -F"hw.sh" '{print $2}'

/some/path/to/data/

Or a bash script:

#!/bin/bash

awk -F"hw.sh" '{print $2}' <<< "http://host:port/cgi-bin/hw.sh/some/path/to/data/"

Upvotes: 2

larsks
larsks

Reputation: 311606

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.

Upvotes: 1

Related Questions