开发者

Parse string character by character in shell script

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 don开发者_如何学Pythone in shell script???


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.


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 /)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜