shell字符串处理总结
1. 字符串切片
1.1 基于偏移量取字符串
返回字符串 string 的长度 ${#string}
示例
[root@centos8 script]#str=" I Love Python "
[root@centos8 script]#echo ${#str}
15
[root@centos8 script]#
返回字符串变量var中从第offset个字符后(不包括第offset个字符)的字符开始,到最后的部分, offset的取值在0 到 ${#var}-1 之间(bash4.2后,允许为负值)
${var:offset}
[root@centos8 script]#str="I Love Python"
[root@centos8 script]#echo ${str:2} # 从第2个字符开始取,默认取后面字符的全部,第2个字符不包含在内
Love Python
[root@centos8 script]#
返回字符串变量var中从第offset个字符后(不包括第offset个字符)的字符开始,长度为number的部分 ${var:offset:number}
[root@centos8 script]#str="I Love Shell"
[root@centos8 script]#echo ${str:2:4} # 从第2个字符开始取,取4个字符
Love
[root@centos8 script]#
示例
[root@centos8 script]#str="I am CEO I am CTO"
[root@centos8 script]#echo ${str:0:$((${#str}-8))}
I am CEO
[root@centos8 script]#
示例:年月日
[root@centos8 script]#today=$(date +"%Y%m%d")
[root@centos8 script]#echo $today
20210408
[root@centos8 script]#Y_m_d=${today:0:4}"-"${today:4:2}"-"${today:6:2}
[root@centos8 script]#echo $Y_m_d
2021-04-08
[root@centos8 script]#month=${today:0:6}
[root@centos8 script]#echo $month
202104
[root@centos8 script]#
取字符串的最右侧几个字符,取字符串的最右侧几个字符, 注意:冒号后必须有一空白字符 ${var: -length}
[root@centos8 script]#str="I Love Shell"
[root@centos8 script]#echo ${str:-3}
I Love Shell
[root@centos8 script]#echo ${str: -3}
ell
[root@centos8 script]#
从最左侧跳过offset字符,一直向右取到距离最右侧lengh个字符之前的内容,即:掐头去尾 ${var:offset:-length}
[root@centos8 script]#str="I Love Shell"
[root@centos8 script]#echo ${str:2:-3}
Love Sh
[root@centos8 script]#
先从最右侧向左取到length个字符开始,再向右取到距离最右侧offset个字符之间的内容,注意:- length前空格
${var: -length:-offset}
[root@centos8 script]#str="I Love Shell"
[root@centos8 script]#echo ${str: -2:-3}
-bash: -3: substring expression < 0
[root@centos8 script]#echo ${str: -3:-2}
e
[root@centos8 script]#echo ${str:-3:-2}
I Love Shell
[root@centos8 script]#echo ${str: -3:-2}
e
[root@centos8 script]#echo ${str: -5:-2}
She
[root@centos8 script]#
1.2 基于模式取子串
# 其中word可以是指定的任意字符,自左而右,查找var变量所存储的字符串中,第一次出现的word, 删除字 符串开头至第一次出现word字符串(含)之间的所有字符
${var#*word}
# 同上,贪婪模式,不同的是,删除的是字符串开头至最后一次由word指定的字符之间的所有内容
${var##*word}
示例
[root@centos8 script]#file="var/log/messages"
[root@centos8 script]#echo ${file#*/}
log/messages
[root@centos8 script]#echo ${file##*/}
messages
[root@centos8 script]#
# 其中word可以是指定的任意字符,功能:自右而左,查找var变量所存储的字符串中,第一次出现的word, 删除字符串最后一个字符向左至第一次出现word字符串(含)之间的所有字符
${var%word*}
# 同上,只不过删除字符串最右侧的字符向左至最后一次出现word字符之间的所有字符
${var%%word*}
示例
[root@centos8 script]#file="var/log/messages"
[root@centos8 script]#echo ${file%/*}
var/log
[root@centos8 script]#echo ${file%%/*}
var
[root@centos8 script]#
示例
[root@centos8 script]#url=http://www.baidu.com:8080
[root@centos8 script]#echo ${url##*:}
8080
[root@centos8 script]#echo ${url%%:*}
http
[root@centos8 script]#
2. 查找替换
查找 string 所表示的字符串中,使用 replace 替换第一次被 substring 所匹配到的字符串
${string/substring/replace}
示例
[root@centos8 script]#str="I LOVE Shell,I LOVE Python"
[root@centos8 script]#echo ${str/LOVE/like}
I like Shell,I LOVE Python
[root@centos8 script]#
查找 string 所表示的字符串中,使用 replace 替换,所有能被 substring 所匹配到的字符串
${string//substring/replace}
示例
[root@centos8 script]#str="I LOVE Shell,I LOVE Python"
[root@centos8 script]#echo ${str//LOVE/like}
I like Shell,I like Python
[root@centos8 script]#
查找 string 所表示的字符串中,以 replace 替换,行首被 substring 所匹配到的字符串,
${string/#substring/replace}
示例
[root@centos8 script]#str="I Love Go I Love Go"
[root@centos8 script]#echo ${str/#Go/Python}
I Love Go I Love Go
[root@centos8 script]#
查找 string 所表示的字符串中,以 replace 替换,行尾被 substring 所匹配到的字符串,
${string/%substring/replace}
示例
[root@centos8 script]#str="I Love Go I Love Go"
[root@centos8 script]#echo ${str/%Go/Python}
I Love Go I Love Python
[root@centos8 script]#
示例
touch {a..d}.html
for file in `ls *.html`
do
mv $file ${file/%html/HTML}
done
${value:-word} 当变量未定义或者值为空时,返回值为word内容,否则返回变量的值
result=${test:-UNSET}
echo $result
echo $test
示例:防止变量无值
# path="/var/log"
find ${path:-/tmp} -type f -name "*.tar.gz" -exec rm -f {} \;
# 当/var/log目录不存在时,删除/tmp目录下的内容,主要是防止/var/log目录不存在时会将root或者根删除
${value:=word} 若变量未定义或者值为空时,在返回word的值的同时,将word赋给value
注:变量替换的值也可以是``括起来的命令:$USERDIR={$Mydir:-pwd}
[root@centos8 script]#test="xufengnian"
[root@centos8 script]#result=${test:=UNSET}
[root@centos8 script]#echo $result
xufengnian
[root@centos8 script]#echo $test
xufengnian
[root@centos8 script]#
${value:?word}
[root@centos8 script]#test="xufengnian"
[root@centos8 script]#result=${test:?UNSET}
[root@centos8 script]#echo $result
xufengnian
[root@centos8 script]#echo $test
xufengnian
[root@centos8 script]#
${value:+word}
[root@centos8 script]#test="xufengnian"
[root@centos8 script]#result=${test:+UNSET}
[root@centos8 script]#echo $result
UNSET
[root@centos8 script]#echo $test
xufengnian
[root@centos8 script]#
3. 查找并删除
# 从变量$string开头开始删除最短匹配$substring子串
${string#substring}
[root@centos8 script]#str="I am xufengnian I am"
[root@centos8 script]#echo ${str#I am}
xufengnian I am
[root@centos8 script]#
# 从变量$string开头开始删除最长匹配$substring子串
${string##substring}
[root@centos8 script]#str="I am xufengnian I am"
[root@centos8 script]#echo ${str##I am}
xufengnian I am
[root@centos8 script]#
# 从变量$string结尾开始删除最短匹配$substring子串
${string%substring}
[root@centos8 script]#str="I am xufengian I am"
[root@centos8 script]#echo ${str%I am}
I am xufengian
[root@centos8 script]#
# 从变量$string结尾开始删除最长匹配$substring子串
${string%%substring}
[root@centos8 script]#str="I am xufengnian I am"
[root@centos8 script]#echo ${str%%I am}
I am xufengnian
[root@centos8 script]#
# 删除var表示的字符串中第一次被pattern匹配到的字符串
${var/pattern}
[root@centos8 script]#str="I am xufengnian I am"
[root@centos8 script]#echo ${str/I am}
xufengnian I am
[root@centos8 script]#
# 删除var表示的字符串中所有被pattern匹配到的字符串
${var//pattern}
[root@centos8 script]#str="I am xufengnian I am"
[root@centos8 script]#echo ${str//I am}
xufengnian
[root@centos8 script]#
示例:把下面所有系统中文件的文件名的finished内容去掉
touch 1_finished.jpg 2_finished.jpg 3_finished.jpg 4_finished.jpg 5_finished.jpg
# 方法一:
for file in `ls *.jpg`
do
mv $file ${file%finished*}.jpg
done
# 方法二:
ls *.jpg|awk -F "finished" '{print "mv " $0,$1""$2}'|bash
# 方法三:
rename "finished" "" *.jpg
4. 字符大小写转换
# 把var中的所有小写字母转换为大写
${var^^}
# 把var中的所有大写字母转换为小写
${var,,}
shell字符串处理总结的更多相关文章
- linux shell 字符串操作(长度,查找,替换)详解
linux shell 字符串操作(长度,查找,替换)详解 在做shell批处理程序时候,经常会涉及到字符串相关操作.有很多命令语句,如:awk,sed都可以做字符串各种操作. 其实shell内置一系 ...
- Shell 字符串比较
转自网络 Shell字符串比较 收藏 Shell 中整数比较方法及字符串的比较方法,如等于,不等于,大于,大于等于,小于,等等. 二元比较操作符,比较变量或者比较数字.注意数字与字符串的区别. --- ...
- shell字符串操作详解
shell字符串操作详解的相关资料. 1.shell变量声明的判断 表达式 含义 ${var} 变量var的值, 与$var相同 ${var-DEFAULT} 如果var没有被声明, 那么就以$DE ...
- 【转】shell字符串截取
shell字符串的截取的问题: 一.Linux shell 截取字符变量的前8位,有方法如下: 1.expr substr “$a” 1 8 2.echo $a|awk ‘{print substr( ...
- shell字符串的用法
shell字符串的用法 注意:shell4.2和shell4.1会有差别,较低版本的shell可能不支持某些功能 获取字符串长度:${#string} 获取子串: 注:(左边的第一个字符是用 0 表示 ...
- Linux脚本shell字符串处理
Linux脚本shell字符串处理,基本都有了,看着搜吧 TLDP教堂 shell中if条件字符串.数字比对,[[ ]]和[ ]区别 Linux 之 shell 比较运算符 Linux Shell编程 ...
- 【Linux】shell字符串分割、数组访问、条件判断
参考资料: shell字符串分割再循环:http://www.shangxueba.com/jingyan/1633455.html linux shell中 if else以及大于.小于.等于逻辑表 ...
- shell 字符串中定位字符位置 获取字符位置
linux shell 字符串操作(长度,查找,替换)详解 该博文中描述的如下两个字符串操作, ${string:position} #在$string中, 从位置$position开始提取子串 ${ ...
- Shell字符串操作
@1:子串削除 ${string#substring} 从$string 的开头位置截掉最短匹配的$substring. ${string##substring} 从$string 的开头位置截掉最长 ...
- Linux Shell系列教程之(五)Shell字符串
本文是Linux Shell系列教程的第(五)篇,更多shell教程请看:Linux Shell系列教程 字符串是Shell编程中最常用最有用的数据类型,今天,Linux大学网就为大家介绍一下在She ...
随机推荐
- TypeScript Learning Paths
TypeScript Learning Paths TypeScript Expert refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以 ...
- ES6 & import * & import default & import JSON
ES6 & import * & import default & import JSON import json & default value bug api.js ...
- .NET 6 Preview 1 发布
前言 2021 年 2 月 17 日微软发布了 .NET 6 的 Preview 1 版本,那么来看看都有什么新特性和改进吧,由于内容太多了因此只介绍一些较为重点的项目. 统一和扩展 .NET 6 在 ...
- lock free(无锁并发)是什么
一.非阻塞同步(Non-blocking Synchronization) 1. 无锁编程 / lock-free / 非阻塞同步 无锁编程,即不使用锁的情况下实现多线程之间的变量同步,也就是在没有线 ...
- [转]ubuntu系统重新分区、根目录扩容
原文地址:https://blog.csdn.net/code_segment/article/details/79237500,转载主要方便随时查阅,如有版权要求,请及时联系. gparted是一款 ...
- 18_MySQL之HAVING字句的使用
本节涉及的sql语句: -- HAVING -- 错误示例 SELECT deptno FROM t_emp WHERE AVG(sal)>=2000 GROUP BY deptno; 因为wh ...
- 1.代码规范之 if 语句编写
最近在看项目代码的时候, 看到需要判断的地方,出现了if的多重嵌套, 甚至是出现了十几层的嵌套, 代码的阅读性非常之差. 简单的举个例子(这里只是两层的嵌套): public class demo ...
- TERSUS无代码开发(笔记07)-简单实例手机端后台逻辑开发
提交申请逻辑开发 1.添加父级对象引用(从父级对象中获取前端输入框的值) 1.设计数据库表(表名和字段名称不能用中文) 2.设计置数据库主键(可设联合主键) 3.传值形成数据实列处理 4.服务器端处理 ...
- 使用OWASP Dependency-Check对应用做个安检
俗话说"人怕出名猪怕壮",当系统小有名气以后就会被一些黑客盯上,三天两头的用各种漏洞扫描工具做渗透,如果不希望某天你负责的系统因为安全问题而出名,那就提前行动起来吧,这就是今天要讲 ...
- 剑指 Offer 42. 连续子数组的最大和 + 动态规划
剑指 Offer 42. 连续子数组的最大和 题目链接 状态定义: 设动态规划列表 \(dp\) ,\(dp[i]\) 代表以元素 \(4nums[i]\) 为结尾的连续子数组最大和. 为何定义最大和 ...