1

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???

5
  • Your download tool doesn't support Content-Disposition? Commented Sep 5, 2011 at 9:45
  • i am feeding a set of links to my download program which would download it with some random sequence of chars which i would like to rename it with that present in the url Commented Sep 5, 2011 at 9:48
  • You should really say how the URL looks, how you get it and what you want to extract from it. Commented Sep 5, 2011 at 9:55
  • my url will be like this: youtube.com/watch?v=Od3xkrxcsE8&feature=relmfu and of which i would like to extract Od3xkrxcsE8 Commented Sep 5, 2011 at 10:03
  • 1
    @nikhil: Do not comment, edit the question! Commented Sep 5, 2011 at 10:51

3 Answers 3

1

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 of parameter is substituted.

   

${parameter:offset}
${parameter:offset:length}

Substring Expansion. Expands to up to length characters of parameter starting at the character specified by offset.

Sign up to request clarification or add additional context in comments.

2 Comments

-1: Shell does not have anything like substring expansion!
It does work in bash. But that's far from synonym to shell these days. For interactive use, there are much more capable shells (ok, it works in zsh too, but than there are much better ways there) and for scripting use, there much faster shells. Where this does not work.
0

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

Depends. Should the file be saved as 'path' or 'foo'?
what is that basename??? and my url will be like this: youtube.com/watch?v=Od3xkrxcsE8&feature=relmfu and of which i would like to extract Od3xkrxcsE8
Browsers would default to path (and ignore the query)
would like to extract content present in between ? and & without v=..simply stating i need only Od3xkrxcsE if my input url is the above one
0
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.

2 Comments

-1: You didn't escaped your assignment. It executes feature=relmfu in a subshell
arnaud576875: Right. Fixed quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.