Issue
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:[email protected]/one/more/dir/file.exe?a=sth&b=sth" rel="noreferrer">http://login:[email protected]/one/more/dir/file.exe?a=sth&b=sth
I want to extract to some other variable only the:
/one/more/dir/file.exe
part. Of course login, password, filename and parameters are optional.
Since I am new to sed and awk I ask you for help. Please, advice me how to do it. Thank you!
Solution
In bash:
URL='http://login:[email protected]/one/more/dir/file.exe?a=sth&b=sth'
URL_NOPRO=${URL:7}
URL_REL=${URL_NOPRO#*/}
echo "/${URL_REL%%\?*}"
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
...
Answered By - saeedgnu Answer Checked By - Gilberto Lyons (WPSolving Admin)