I am trying to parse a url to extract some text from it so that i can use the same to rename my file when i download it. Basically i want to write to a shell script to do this. I would like to collect the url into a string and then parse it character by character. How could this be done in shell script???
3 Answers
You can read a string char-by-char by using the Substring Expansion syntax:
${parameter:offset:length}
Example:
str="abcd"
char=${str:1:1} # => "b"
And get the length of the string with the Parameter length syntax:
${#parameter}
Example:
${#str}
So you can iterate over the characters by using this:
for (( i = 0; i < ${#str}; ++i)); do
echo "${str:$i:1}"
done
From the bash manual:
${#parameter}
Parameter length. The length in characters of the value ofparameteris substituted.
${parameter:offset}
${parameter:offset:length}
Substring Expansion. Expands to up to length characters ofparameterstarting at the character specified byoffset.
2 Comments
This will give you the filename to save to:
url="http://www.example.com/path?foo#bar"
echo $(basename "${url%%[?#]*}")
# => "path"
How it works:
"${url%%[?#]*}"removes any thing after?and#(it removes the query and the hash)$(basename "...")returns the last path component (the part after the last /)
4 Comments
path (and ignore the query)url='http://youtube.com/watch?v=Od3xkrxcsE8&feature=relmfu'
url=${url#*v=}
url=${url%%&*}
or you can use sed, which is less efficient (starts external command), but more flexible and for more complicated cases also more readable.
url='http://youtube.com/watch?v=Od3xkrxcsE8&feature=relmfu'
url=$(printf '%s' "$url" | sed 's+.*v=\([^&]*\).*+\1+')
Note, that in shell (/bin/sh), the ${var#prefix-pattern} and ${var%suffix-pattern} are the only string manipulation functions available. In bash or zsh you have many more, but always be aware that you are using such extension, because some systems have simpler shell installed as /bin/sh and some (usually other Unix flavours than Linux or embedded systems) systems don't have bash at all.
Content-Disposition?