0

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.

1
  • Is the hw.sh part fixed? Commented Nov 2, 2016 at 11:30

2 Answers 2

2

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/"

Sign up to request clarification or add additional context in comments.

2 Comments

The OP said, "How can I fetch the required value from the above URL in a shell script." not from the Command Line.
True, script added.
1

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.

2 Comments

Thanks! This worked for me. Also, as I am new to shell scripting, could you/anyone suggest some guide or link to have a look at the way parsing is done in bash. This would be of great help. TIA.
I'm not sure what you mean by "parsing", but I find that the bash man page is a good place to start, in particular because it has lots of details about how variable expansion works. The bash faq is also a good resource.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.