Linux Shell 截取字符串


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

${var#*/}
${var##*/}
${var%/*}
${var%%/*}
${var:start:len}
${var:start}
${var:0-start:len}
${var:0-start}

下面用几个例子展示一下:

1) 获得字符串的长度

语法:

${#var}

示例代码:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" length=${#str}
echo "length : [${length}]"

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
length : []

2) 使用 # 和 ## 获取尾部子字符串

2.1) # 最小限度从前面截取word

语法:

${parameter#word}  

示例代码:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" #分割符为'/'
substr=${str#*/}
echo "substr : [${substr}]"

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [/www.fengbohello.xin3e.com/blog/shell-truncating-string]

2.2) ## 最大限度从前面截取word

语法:

${parameter##word}

示例代码:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" #分割符为'/'
substr=${str##*/}
echo "substr : [${substr}]"

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [shell-truncating-string]

3) 使用 % 和 %% 获取头部子字符串

3.1) % 最小限度从后面截取word

语法:

${parameter%word} 

示例代码:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" substr=${str%/*}
echo "substr : [${substr}]"

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http://www.fengbohello.xin3e.com/blog]

3.2) %% 最大限度从后面截取word

语法:

${parameter%%word}

示例代码:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" substr=${str%%/*}
echo "substr : [${substr}]"

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http:]

4)使用 ${var:} 模式获取子字符串

4.1) 指定从左边第几个字符开始以及子串中字符的个数

语法:

${var:start:len}

示例代码:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" #其中的 表示左边第一个字符开始, 表示子字符的总个数。
substr=${str::}
echo "substr : [${substr}]"

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http://]

4.2) 从左边第几个字符开始一直到结束

语法:

${var:}

示例代码:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" #其中的 表示左边第8个字符开始
substr=${str:}
echo "substr : [${substr}]"

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [www.fengbohello.xin3e.com/blog/shell-truncating-string]

4.3) 从右边第几个字符开始以及字符的个数

语法:

${var:-start:len}

示例代码:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" #其中的 - 表示右边算起第23个字符开始, 表示字符的个数
substr=${str:-:}
echo "substr : [${substr}]"

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [shell]

4.4) 从右边第几个字符开始一直到结束

语法:

${var:-start}

示例代码:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]" #其中的 - 表示右边算起第6个字符开始
substr=${str:-}
echo "substr : [${substr}]"

执行结果:

string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [string]

同步发表:http://www.fengbohello.top/point/p/629

Linux Shell 截取字符串的更多相关文章

  1. shell截取字符串的方法

    参考文献: linux中shell截取字符串方法总结 [Linux]如何在Shell脚本中计算字符串长度? 截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${ex ...

  2. shell截取字符串方法

    shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法.${parameter:-word}${parameter:=word}${parameter:?word}${p ...

  3. shell截取字符串的8种方法

    参考文献: linux中shell截取字符串方法总结 [Linux]如何在Shell脚本中计算字符串长度? 截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${ex ...

  4. Linux shell去除字符串中所有空格

    Linux shell去除字符串中所有空格 echo $VAR | sed 's/ //g'

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

    截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=wo ...

  6. inux中shell截取字符串方法总结

    shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=word} ${parameter:?word} ...

  7. shell 截取字符串(转)

    linux中对字符串的处理: 1.字符串分割例如  AAAAA-BBBBBB  按-分割去前后两部分 cut : [rich@localhost ~]$ str=AAAAA-BBBBBB[rich@l ...

  8. linux 中截取字符串

    shell中截取字符串的方法有很多中,${expression}一共有9种使用方法.${parameter:-word}${parameter:=word}${parameter:?word}${pa ...

  9. Linux shell 获得字符串所在行数及位置

    shell 获得字符串所在行数及位置 01 获取字符串所在的行数 方式一:用grep -n [root@root]# cat test apple bit create delect exe flow ...

随机推荐

  1. Linux下用户组、文件权限详解

    在linux中的每个用户必须属于一个组,不能独立于组外.在linux中每个文件有所有者.所在组.其它组的概念 - 所有者 - 所在组 - 其它组 - 改变用户所在的组 所有者 一般为文件的创建者,谁创 ...

  2. Unity 碰撞的例子

    1.新建一个3D的Unity工程. 2.菜单 "GameObject" - "3D Object",选择"Cube" 和 "Cap ...

  3. 关于imageOrientation

    用相机拍出来的照片都含有EXIF信息,UIImage的imageOrientation属性指的就是EXIF中的orientation信息.如果我们忽略orientation信心,而直接对照片进行想速处 ...

  4. js_事件委托

    起因: 1.这是前端面试的经典题型,要去找工作的小伙伴看看还是有帮助的: 2.其实我一直都没弄明白,写这个一是为了备忘,二是给其他的知其然不知其所以然的小伙伴们以参考: 概述: 那什么叫事件委托呢?它 ...

  5. DataSet装换为泛型集合 222

    #region DataSet装换为泛型集合 /// <summary> /// 利用反射和泛型 /// </summary> /// <param name=" ...

  6. ios语音识别

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000; min-height: 15.0px } p.p ...

  7. 利用代码添加autolayout约束

    1.概述 通常我们通过storyboard能够完成的,代码也能够完成,所以这里介绍下代码实现约束的添加,通常我们不这么干(在不使用第三方框架的情况下,使用系统自带的类添加约束特别繁琐),所以这里仅仅简 ...

  8. 扩展方法 1 简单的string扩展方法

    这里是关于 String的简单扩展方法 (静态类 静态方法 this 类型 这里是string) static class Program { static void Main(string[] ar ...

  9. Android中surface,surfaceview,sufaceholder以及surface客户端的关系

    这里以照相机camera功能的实现来解释surface,surfaceview,sufaceholder以及surface客户端(本例子中指的是camera)的关系,surface及其client(客 ...

  10. pk8和x509.pem转换成keystore

    转自:http://www.cnblogs.com/platte/p/3511814.html 一 在github上下载工具 https://github.com/getfatday/keytool- ...