linux中shell截取字符串方法总结

shell中截取字符串的方法有很多中,可以分为两大类。第一种获取特定的字符或字符串的左边或者右边的字字符串,java中实现需要先用indexOf来确定特定字符串的位置,然后再用substring来获取结果;第二种类似java中的substring

shell中截取字符串的方法有很多中,

${expression}一共有9种使用方法。
${parameter:-word}
${parameter:=word}
${parameter:?word}
${parameter:+word}
上面4种可以用来进行缺省值的替换。
${#parameter}
上面这种可以获得字符串的长度。
${parameter%word} 最小限度从后面截取word
${parameter%%word} 最大限度从后面截取word
${parameter#word} 最小限度从前面截取word
${parameter##word} 最大限度从前面截取word
上面4个就是用来截取字符串的方法了。

有了着四种用法就不必使用cut命令来截取字符串了
第一种又可以分为四种情况,下面一一介绍。

1、使用 # 号操作符。用途是从左边开始删除第一次出现子字符串即其左边字符,保留右边字符。用法为#*substr,例如:

str=’http://www.你的域名.com/cut-string.html’

echo ${str#*//}

得到的结果为www.你的域名.com/cut-string.html,即删除从左边开始到第一个”//”及其左边所有字符2、使用 ## 号操作符。用途是从左边开始删除最后一次出现子字符串即其左边字符,保留右边字符。用法为##*substr,例如:

str=’http://www.你的域名.com/cut-string.html’

echo ${str##*/}

得到的结果为cut-string.html,即删除最后出现的”/”及其左边所有字符3、使用 % 号操作符。用途是从右边开始删除第一次出现子字符串即其右边字符,保留左边字符。用法为%substr*,例如:

str=’http://www.你的域名.com/cut-string.html’

echo ${str%/*}

得到的结果为http://www.你的域名.com,即删除从右边开始到第一个”/”及其右边所有字符4、使用 %% 号操作符。用途是从右边开始删除最后一次出现子字符串即其右边字符,保留左边字符。用法为%%substr*,例如:

str=’http://www.你的域名.com/cut-string.html’

echo ${str%%/*}

得到的结果为http://www.你的域名.com,即删除从右边开始到最后一个”/”及其右边所有字符第二种也分为四种,分别介绍如下:

1、从左边第几个字符开始以及字符的个数,用法为:start:len,例如:

str=’http://www.你的域名.com/cut-string.html’

echo ${var:0:5}

其中的 0 表示左边第一个字符开始,5 表示字符的总个数。
结果是:http:2、从左边第几个字符开始一直到结束,用法为:start,例如:

str=’http://www.你的域名.com/cut-string.html’

echo ${var:7}

其中的 7 表示左边第8个字符开始
结果是:www.你的域名.com/cut-string.html3、从右边第几个字符开始以及字符的个数,用法:0-start:len,例如:

str=’http://www.你的域名.com/cut-string.html’
echo ${str:0-15:10}

其中的 0-6 表示右边算起第6个字符开始,10 表示字符的个数。
结果是:cut-string3、从右边第几个字符开始一直到结束,用法:0-start,例如:

str=’http://www.你的域名.com/cut-string.html’
echo ${str:0-4}

其中的 0-6 表示右边算起第6个字符开始,10 表示字符的个数。
结果是:html注:(左边的第一个字符是用 0 表示,右边的第一个字符用 0-1 表示)

网上其它参考内容
一、Linux shell 截取字符变量的前8位,有方法如下:
1.expr substr “$a” 1 8
2.echo $a|awk ‘{print substr(,1,8)}’
3.echo $a|cut -c1-8
4.echo $
5.expr $a : ‘(.\).*’
6.echo $a|dd bs=1 count=8 2>/dev/null

二、按指定的字符串截取
1、第一种方法:
${varible##*string} 从左向右截取最后一个string后的字符串
${varible#*string}从左向右截取第一个string后的字符串
${varible%%string*}从右向左截取最后一个string后的字符串
${varible%string*}从右向左截取第一个string后的字符串
“*”只是一个通配符可以不要

例子:
$ MYVAR=foodforthought.jpg
$ echo ${MYVAR##*fo}
rthought.jpg
$ echo ${MYVAR#*fo}
odforthought.jpg

2、第二种方法:${varible:n1:n2}:截取变量varible从n1到n2之间的字符串。

可以根据特定字符偏移和长度,使用另一种形式的变量扩展,来选择特定子字符串。试着在 bash 中输入以下行:
$ EXCLAIM=cowabunga
$ echo ${EXCLAIM:0:3}
cow
$ echo ${EXCLAIM:3:7}
abunga

这种形式的字符串截断非常简便,只需用冒号分开来指定起始字符和子字符串长度。

三、按照指定要求分割:
比如获取后缀名
ls -al | cut -d “.” -f2

20 Responses so far.

  1. yiluyingxiao says:
    提前祝您新年快乐。
    乙未年(羊)冬月廿一 2015-12-31
  2. wo says:
    Happy New Year.
  3. quest bars says:
    This design is spectacular! You obviously know how to keep a reader amused.

    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent
    job. I really loved what you had to say, and more than that, how you presented it.
    Too cool!

  4. Quest bars says:
    Everyone loves what you guys are usually up too.
    This kind of clever work and exposure! Keep up the amazing works guys I’ve included you guys to blogroll.
  5. quest Bars says:
    Thanks for your marvelous posting! I definitely enjoyed reading it, you will
    be a great author. I will remember to bookmark your blog and
    definitely will come back from now on. I want to encourage
    yourself to continue your great writing, have a nice holiday weekend!
  6. You need to be a part of a contest for one of the best websites on the internet.
    I most certainly will recommend this web site!
  7. I must thank you for the efforts you have put in penning this website.
    I really hope to view the same high-grade blog posts by you in the future as well.
    In fact, your creative writing abilities has motivated me
    to get my own, personal blog now 😉
  8. I love what you guys are up too. Such clever work and exposure!
    Keep up the good works guys I’ve included you
    guys to my blogroll.
  9. If some one desires expert view regarding blogging and site-building afterward i
    recommend him/her to visit this webpage, Keep up the good job.
  10. Excellent post! We will be linking to this particularly great content on our site.

    Keep up the good writing.

  11. Superb, what a blog it is! This website provides helpful information to us, keep it
    up.
  12. Hello, i feel that i saw you visited my web site so i
    got here to go back the favor?.I am trying to to find
    things to improve my website!I guess its good enough to use some of
    your ideas!!
  13. Very rapidly this web site will be famous among all blog people, due to it’s good posts
  14. Ahaa, its fastidious discussion about this post here at this webpage, I have read
    all that, so now me also commenting at this place.
  15. Unquestionably believe that which you said. Your favorite justification seemed to be on the internet the
    simplest thing to be aware of. I say to you, I certainly get irked while people consider worries that they plainly don’t know about.
    You managed to hit the nail upon the top and defined out the whole thing
    without having side effect , people can take a signal. Will probably be back to get more.
    Thanks
  16. I visited various web pages but the audio quality for audio songs present at this web site is genuinely fabulous.
  17. mom fuck says:
    Very good blog post.Much thanks again. Great.
  18. I was reading some of your articles on this internet site and I believe this site is rattling informative ! Retain posting.
  19. nonton movie says:
    I really enjoy the article. Keep writing.

Leave a Reply to minecraft.exe Cancel reply