Friday, December 20, 2013

bash: extract tokens from a string using parameter expansion for example domain or host from url

There is literally no developer or sysadmin which will not understand what $var is but parameter expansion in bash is more than just a simple variable. When it comes to parsing strings most developers are familiar with high level functions that allow to tokenize them. Bash parameter expansion can help with this task using substring removal. Double hash-mark and double percentage-mark are your friends.

Below is a solution for the typical problem related to parsing urls:
url="http://sample.com/path/to/res?p1=1&p2=2"
url_no_params=${url%%\?*}
echo $url_no_params 
params=${url##*\?}
echo $params
host_and_path=${url##*\/\/}
echo $host_and_path
host=${host_and_path%%\/*}
echo $host
The above will result in:
$url="http://sample.com/path/to/res?p1=1&p2=2"
$url_no_params=${url%%\?*}
$echo $url_no_params 
http://sample.com/path/to/res
$params=${url##*\?}
$echo $params
p1=1&p2=2
$host_and_path=${url##*\/\/}
$echo $host_and_path
sample.com/path/to/res?p1=1&p2=2
$host=${host_and_path%%\/*}
$echo $host
sample.com

No comments:

Followers