shell 字符串截取

Posted by wxianfeng Sat, 28 Feb 2009 21:04:00 GMT
环境:ubuntu 10.10

上次写了安装所有gems,如果系统已经安装了 就跳过安装的 shell脚本,其中最主要的使用了 字符串的截取,发现bash里截取字符串非常之麻烦,请教了 一个 搞嵌入式的 家伙(同学) @liuqun,给了我 几个指点,发现原来也有很多办法,只是不是那么顺手.

上次的那个 shell 脚本:

#!/bin/bash 
# 安装所有的gems,如果已经安装了就不安装

cd /usr/local/system/ruby/lib/ruby/gems/1.8/cache 


for i in `ls`;do
        gem=`echo $i | awk -F'-' '{print $1}'`
        version=`echo $i | grep -o "\-[0-9].*" | sed 's/^-//;s/.gem//'`
        is_gem_exist=`gem list $gem -v=$version`
        if [ -z "$is_gem_exist" ]; then # 注意[] 内部两边留空格
              `gem install $i`
        else
                echo "$i have installed"
        fi
done
结合 grep awk sed cut 等等,看看有多少办法吧:

1:
wxianfeng@ubuntu:~$ echo "action-i18n-0.4.1.gem" | grep -o "\-[0-9].*" | sed 's/^-//;s/.gem//'
0.4.1
grep -o 是只显示匹配出来的

sed 's/^-//' 把开头的 - 变为空

sed 's/.gem//' 是把.gem 变为空

2,
wxianfeng@ubuntu:~$ STR="actionmailer-2.3.5.gem"; STR=${STR##*-}; echo ${STR%\.*}
2.3.5
${STR##*-} 从左向右截取最后一个 - 后的字符串

${STR%\.*} 从右向左截取第一个 . 后的字符串

3,
wxianfeng@ubuntu:~$ STR="actionmailer-2.3.5.gem"; echo ${STR:13:5}
2.3.5
${STR:13:5} 从index 13 偏移量,5是长度

4,
wxianfeng@ubuntu:~$ echo actionmailer-2.3.5.gem | sed 's/[^0-9]*\(.*\).gem/\1/'
2.3.5
sed 's/[^0-9]*\(.*\).gem/\1/' \1 匹配出来的是 2.3.5 被替换的是 actionmailer-2.3.5.gem

5,
wxianfeng@ubuntu:~$ STR="actionmailer-2.3.5.gem"  ; echo ${STR:13:5}
2.3.5
${STR:13:5} 13 是偏移量,5是长度

6,
wxianfeng@ubuntu:~$ echo "action-i18n-0.4.1.gem" | awk '{print substr($0,13,5)}'
0.4.1
$0 就是指 action-i18n-0.4.1.gem

13 是偏移量

5 是 长度

7,
wxianfeng@ubuntu:~$ echo "action-i18n-0.4.1.gem" | cut -d- -f 3 | cut -d . -f 1-3
0.4.1
-d- 是以 - 分割

-f 3 是第三断

-f 1-3 第一到第三断

see: http://tech.foolpig.com/2008/07/09/linux-shell-char/
http://linux.sheup.com/linux/linux5426.htm

This entry was posted on Sat, 28 Feb 2009 21:04:00 GMT and Posted in , . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags


Trackbacks

Use the following link to trackback from your own site:
http://blog.wxianfeng.com/trackbacks?article_id=113

Comments

  1. Avatar
    raja domino 88 almost {{count}} years ago:

    I don’t even know the way I ended up here, however I believed
    this submit used to be good. I do not recognize who you are but definitely you are
    going to a well-known blogger in case you aren’t already. Cheers!

  2. Avatar
    raja domino 88 almost {{count}} years ago:

    I don’t even know the way I ended up here, however I believed
    this submit used to be good. I do not recognize who you are
    but definitely you are going to a well-known blogger in case you aren’t already.
    Cheers!

  3. Avatar
    domino almost {{count}} years ago:

    I’d been honored to obtain a call coming from a friend when he found
    out the important tips shared on your own site.
    Examining your blog article is a real wonderful experience.
    Thank you for thinking of readers like me, and I desire for you
    the best of success like a professional in this area.

  4. Avatar
    domino almost {{count}} years ago:

    I’d been honored to obtain a call coming from a friend when he found out the important tips shared on your own site.
    Examining your blog article is a real wonderful experience.
    Thank you for thinking of readers like me, and I desire for you the best of
    success like a professional in this area.

  5. Avatar
    Asian Poker88 almost {{count}} years ago:

    What i don’t realize is in fact how you are now not really much more well-liked than you
    may be now. You are so intelligent. You realize therefore considerably in the case of this subject, made me in my opinion believe
    it from numerous varied angles. Its like men and women don’t seem to be
    fascinated unless it’s one thing to do with Girl
    gaga! Your own stuffs great. All the time handle it up!

  6. Avatar
    Asian Poker88 almost {{count}} years ago:

    What i don’t realize is in fact how you are now not really much more well-liked than you may be
    now. You are so intelligent. You realize therefore considerably in the case of this
    subject, made me in my opinion believe it from numerous varied angles.

    Its like men and women don’t seem to be fascinated unless it’s one thing to do with Girl gaga!

    Your own stuffs great. All the time handle it up!

  7. Avatar
    https://ninjapkv.com almost {{count}} years ago:

    Hey there, You’ve performed a fantastic job. I’ll
    definitely digg it and in my opinion recommend to my
    friends. I’m confident they will be benefited from this site.

  8. Avatar
    https://ninjapkv.com almost {{count}} years ago:

    Hey there, You’ve performed a fantastic job. I’ll definitely digg it and
    in my opinion recommend to my friends. I’m confident they will be benefited
    from this site.

  9. Avatar
    ninjapkv about {{count}} years ago:

    I gotta bookmark this internet site it seems extremely helpful
    extremely helpful.

  10. Avatar
    ninjapkv about {{count}} years ago:

    I gotta bookmark this internet site it seems extremely helpful extremely helpful.

  11. Avatar
    togel about {{count}} years ago:

    Hey! Ι just wanted to ask iff үoս ever have any trouble with hackerѕ?
    My last bloɡ (wordρresѕ) was hacked and I endeⅾ up losіng several weeks of hard wok due to no
    backup. Do you have any solutions to stoр hackеrs?

Leave a comment