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.
2 Answers
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/"
2 Comments
user3439894
The OP said, "How can I fetch the required value from the above URL in a shell script." not from the Command Line.
Zlemini
True, script added.
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
Shashank Shekher
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.
hw.shpart fixed?