提取URL文件名和路径在bash脚本 [英] Extract filename and path from URL in bash script
问题描述
在我的bash脚本我需要从给定的URL中提取人的路。
例如,从含变量字符串:
In my bash script I need to extract just the path from the given URL. For example, from the variable containing string:
<一个href=\"http://login:password@example.com/one/more/dir/file.exe?a=sth&b=sth\">http://login:password@example.com/one/more/dir/file.exe?a=sth&b=sth
我要提取其他一些变量只有:
I want to extract to some other variable only the:
/one/more/dir/file.exe
/one/more/dir/file.exe
一部分。当然登录,密码,文件名和参数都是可选的。
part. Of course login, password, filename and parameters are optional.
由于我是新来的sed和awk我请你帮忙。请指点我该怎么办呢。谢谢!
Since I am new to sed and awk I ask you for help. Please, advice me how to do it. Thank you!
推荐答案
在bash中:
URL='http://login:password@example.com/one/more/dir/file.exe?a=sth&b=sth'
URL_NOPRO=${URL:7}
URL_REL=${URL_NOPRO#*/}
echo "/${URL_REL%%\?*}"
只有工作,如果URL与开头是http://
或具有相同长度的协议
否则,它可能更容易使用正则表达式与 SED
,的grep
或剪切
...
Works only if URL starts with http://
or a protocol with the same length
Otherwise, it's probably easier to use regex with sed
, grep
or cut
...
这篇关于提取URL文件名和路径在bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!