Linux Shell 截取字符串
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]
Linux Shell 截取字符串的更多相关文章
- shell截取字符串的方法
参考文献: linux中shell截取字符串方法总结 [Linux]如何在Shell脚本中计算字符串长度? 截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${ex ...
- shell截取字符串方法
shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法.${parameter:-word}${parameter:=word}${parameter:?word}${p ...
- shell截取字符串的8种方法
参考文献: linux中shell截取字符串方法总结 [Linux]如何在Shell脚本中计算字符串长度? 截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${ex ...
- Linux shell去除字符串中所有空格
Linux shell去除字符串中所有空格 echo $VAR | sed 's/ //g'
- linux中shell截取字符串方法总结
截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=wo ...
- inux中shell截取字符串方法总结
shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=word} ${parameter:?word} ...
- shell 截取字符串(转)
linux中对字符串的处理: 1.字符串分割例如 AAAAA-BBBBBB 按-分割去前后两部分 cut : [rich@localhost ~]$ str=AAAAA-BBBBBB[rich@l ...
- linux 中截取字符串
shell中截取字符串的方法有很多中,${expression}一共有9种使用方法.${parameter:-word}${parameter:=word}${parameter:?word}${pa ...
- Linux shell 获得字符串所在行数及位置
shell 获得字符串所在行数及位置 01 获取字符串所在的行数 方式一:用grep -n [root@root]# cat test apple bit create delect exe flow ...
随机推荐
- 使用spring的AOP时产生的异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' ...
- Objective-C中的浅拷贝和深拷贝(转载)
本文转自:http://segmentfault.com/blog/channe/1190000000604331 浅拷贝 浅拷贝就是对内存地址的复制,让目标对象指针和源对象指向同一片内存空间.如: ...
- PHP Ueditor 富文本编辑器
2016年12月11日 08:46:59 星期日 百度的简版富文本编辑器umeditor很久没更新了 全功能版本的配置项跟umeditor还是有区别的, 这里说下ueditor怎么对接到项目中去, 主 ...
- selenium 常见面试题以及答案(Java版)
1.怎么 判断元素是否存在? 判断元素是否存在和是否出现不同, 判断是否存在意味着如果这个元素压根就不存在, 就会抛出NoSuchElementException 这样就可以使用try catch,如 ...
- c/c++ 缓冲区的刷新
利用string 对象查看缓冲区的变化,因为每个string对象在输入时会以空格作为分界. #include<iostream> #include<string> using ...
- 关于VPN的一些问题
在今在外出差,必须得连上公司的数据库,那就得使用VPN了,设置起来还算比较简单(我是win10), 简单介绍一下: 打开设置或者直接点击右下角的
- MongoDB 效率
写入: 插入100万条数据:用InsertMany,耗时16s左右. 读取: 读取300万条数据,耗时3600毫秒.
- Git 常用命令详解
Git 是一个很强大的分布式版本管理工具,它不但适用于管理大型开源软件的源代码(如:linux kernel),管理私人的文档和源代码也有很多优势(如:wsi-lgame-pro) Git 的更多介绍 ...
- 【纯css】左图右文列表,左图外框宽度占一定百分比的正方形,右上下固定,右中自动响应高度。支持不规则图片。
查看演示 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- 2048游戏_QT实现
#ifndef GAMEWIDGET_H #define GAMEWIDGET_H #include <QWidget> #include <QMouseEvent> #inc ...