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. docker笔记

    安装...不说了 docker info 查看信息 docker pull ...拉取镜像 docker run -it [镜像名] 运行 docker ps查看当前运行的容器  docker ps ...

  2. Unity NGUI添加UIRoot

    导入NGUI包后,菜单多出一个选项 "NGUI",选择其子选项 "options" -- "Reset Prefab ToolBar" ,在 ...

  3. kvm虚拟机静态和动态迁移

    一.kvm虚拟机静态迁移 1.静态迁移就是虚拟机在关机状态下,拷贝虚拟机虚拟磁盘文件与配置文件到目标虚拟主机中,实现的迁移. (1)虚拟主机各自使用本地存储存放虚拟机磁盘文件 本文实现基于本地磁盘存储 ...

  4. 419. Battleships in a Board

    https://leetcode.com/problems/battleships-in-a-board/ 给定一个N×N的棋盘,有任意数量的1×N或N×1大小的"船",注意船船之 ...

  5. 五种方式让你在java中读取properties文件内容不再是难题

    一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...

  6. jquery 替换文本内容

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  7. c++中4个与类型转换相关的关键字分析

    static_cast reinterpret_cast dynamic_cast const_cast 1.**static_cast------运算符完成相关类型之间的转换** 使用场景:如在同一 ...

  8. .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别

    WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...

  9. Oracle REGEXP_SUBSTR()

    REGEXP_SUBSTR函数格式如下: function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier)       _ ...

  10. ppmoney 总结二

    1. return false   ES6函数的扩展:箭头函数  数组 arr.map()   arr.filter() <!DOCTYPE html> <html lang=&qu ...