zoukankan      html  css  js  c++  java
  • Shell 截取字符串

    转载自:https://www.cnblogs.com/fengbohello/p/5954895.html    (作者:郝峰波  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 : [61]

    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}]"
    
    #其中的 0 表示左边第一个字符开始,7 表示子字符的总个数。
    substr=${str:0:7}
    echo "substr : [${substr}]"
    复制代码

    执行结果:

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

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

    语法:

    ${var:7}

    示例代码:

    复制代码
    str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
    echo "string : [${str}]"
    
    #其中的 7 表示左边第8个字符开始
    substr=${str:7}
    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:0-start:len}

    示例代码:

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

    执行结果:

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

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

    语法:

    ${var:0-start}

    示例代码:

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

    执行结果:

    string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
    substr : [string]
  • 相关阅读:
    PyTorch中view的用法
    pytorch lstm crf 代码理解 重点
    Pytorch Bi-LSTM + CRF 代码详解
    pytorch入坑一 | Tensor及其基本操作
    error C2440:“类型转换":无法从“void (__thiscall Play1::* )(int *)”转换为“cocos2d::SEL_CallFucN
    一行命令搞定VS2012无法安装cocos2d-x-2.1.4及创建跨平台项目(二)
    VS2012无法安装cocos2d-x-2.1.4 解决方法及VS2012新建coco2d-x项目(一)
    更换VS2012序列号的方法
    【iOS开发必备指南合集】申请企业级IDP、真机调试、游戏接入GameCenter 指南(实现仿官方的成就提示)、游戏接入OpenFeint指南;
    Xcode快照——管理应用程序版本
  • 原文地址:https://www.cnblogs.com/liuyihua1992/p/10989522.html
Copyright © 2011-2022 走看看