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 ...
随机推荐
- React Native 三端同构
React Native 三端同构 https://www.ibm.com/developerworks/cn/web/wa-universal-react-native/index.html rea ...
- js group objects in an array
js group objects in an array js group objects in an array var groupBy = function(xs, key) { return x ...
- CSS flex waterfall layout
CSS flex waterfall layout https://github.com/YoneChen/waterfall-flexbox https://css-tricks.com/snipp ...
- Puppeteer: 虚拟键盘
文档 main.js const pptr = require('puppeteer'); const gotoUrl = 'http://127.0.0.1:5500/index.html'; (a ...
- Byte Buddy学习笔记
本文转载自Byte Buddy学习笔记 简介 Byte Buddy是一个JVM的运行时代码生成器,你可以利用它创建任何类,且不像JDK动态代理那样强制实现一个接口.Byte Buddy还提供了简单的A ...
- CVer想知道的都在这里了,一起分析下《中国计算机视觉人才调研报告》吧!
最近闲来无事,老潘以一名普通算法工程师的角度,结合自身以及周围人的情况,理性也感性地分析一下极市平台前些天发布的2020年度中国计算机视觉人才调研报告. 以下的"计算机视觉人才"简 ...
- Java多线程并发编程/锁的理解
一.前言 最近项目遇到多线程并发的情景(并发抢单&恢复库存并行),代码在正常情况下运行没有什么问题,在高并发压测下会出现:库存超发/总库存与sku库存对不上等各种问题. 在运用了 限流/加锁等 ...
- pwn篇:攻防世界进阶welpwn,LibcSearcher使用
攻防世界welpwn (搬运一篇自己在CSDN写的帖子) 链接:https://blog.csdn.net/weixin_44644249/article/details/113781356 这题主要 ...
- oracle 查看 FK constraint referenced_table及columns
select uc.table_name, uc.r_constraint_name, ucc.table_name, listagg(ucc.column_name, ',') within gro ...
- JVM相关 - 深入理解 System.gc()
本文基于 Java 17-ea,但是相关设计在 Java 11 之后是大致一样的 我们经常在面试中询问 System.gc() 究竟会不会立刻触发 Full GC,网上也有很多人给出了答案,但是这些答 ...