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字符串处理总结的更多相关文章

  1. linux shell 字符串操作(长度,查找,替换)详解

    linux shell 字符串操作(长度,查找,替换)详解 在做shell批处理程序时候,经常会涉及到字符串相关操作.有很多命令语句,如:awk,sed都可以做字符串各种操作. 其实shell内置一系 ...

  2. Shell 字符串比较

    转自网络 Shell字符串比较 收藏 Shell 中整数比较方法及字符串的比较方法,如等于,不等于,大于,大于等于,小于,等等. 二元比较操作符,比较变量或者比较数字.注意数字与字符串的区别. --- ...

  3. shell字符串操作详解

    shell字符串操作详解的相关资料. 1.shell变量声明的判断  表达式 含义 ${var} 变量var的值, 与$var相同 ${var-DEFAULT} 如果var没有被声明, 那么就以$DE ...

  4. 【转】shell字符串截取

    shell字符串的截取的问题: 一.Linux shell 截取字符变量的前8位,有方法如下: 1.expr substr “$a” 1 8 2.echo $a|awk ‘{print substr( ...

  5. shell字符串的用法

    shell字符串的用法 注意:shell4.2和shell4.1会有差别,较低版本的shell可能不支持某些功能 获取字符串长度:${#string} 获取子串: 注:(左边的第一个字符是用 0 表示 ...

  6. Linux脚本shell字符串处理

    Linux脚本shell字符串处理,基本都有了,看着搜吧 TLDP教堂 shell中if条件字符串.数字比对,[[ ]]和[ ]区别 Linux 之 shell 比较运算符 Linux Shell编程 ...

  7. 【Linux】shell字符串分割、数组访问、条件判断

    参考资料: shell字符串分割再循环:http://www.shangxueba.com/jingyan/1633455.html linux shell中 if else以及大于.小于.等于逻辑表 ...

  8. shell 字符串中定位字符位置 获取字符位置

    linux shell 字符串操作(长度,查找,替换)详解 该博文中描述的如下两个字符串操作, ${string:position} #在$string中, 从位置$position开始提取子串 ${ ...

  9. Shell字符串操作

    @1:子串削除 ${string#substring} 从$string 的开头位置截掉最短匹配的$substring. ${string##substring} 从$string 的开头位置截掉最长 ...

  10. Linux Shell系列教程之(五)Shell字符串

    本文是Linux Shell系列教程的第(五)篇,更多shell教程请看:Linux Shell系列教程 字符串是Shell编程中最常用最有用的数据类型,今天,Linux大学网就为大家介绍一下在She ...

随机推荐

  1. Python module all in one

    Python module all in one Python Modules https://docs.python.org/3/tutorial/modules.html Fibonacc # F ...

  2. 灰度发布 & A/B 测试

    灰度发布 & A/B 测试 http://www.woshipm.com/pmd/573429.html 8 https://testerhome.com/topics/15746 scree ...

  3. js in depth: event loop & micro-task, macro-task & stack, queue, heap & thread, process

    js in depth: event loop & micro-task, macro-task & stack, queue, heap & thread, process ...

  4. NGK:价值对标比特币,上线暴涨4558%,下一个财富暴增风口

    近期,美股行情多变,一直饱受争议的比特币也成了其中的"弄潮儿".看多者认为,机构的兴趣有助于支撑比特币作为对冲美元疲软和通胀的工具. 特别是今年1月底的时候,马斯克将推特简介更改为 ...

  5. Masterboxan INC金融:在区块链技术基础上推动业务模式的变革创新

    10月初,2020年国际区块链技术与应用大会在硅谷开幕,全球内外区块链技术项目团队.行业领导.专家等共聚一堂,围绕区块链技术与应用展开讨论交流.美国Masterboxan INC万事达资产管理有限公司 ...

  6. 使用docker mediawiki,搭建网页wiki

    我只是想做一个大家都能访问的wiki,用于成员间共享和维护一些文档.找到了docker的mediawiki,这里记录一下我怎么搭的吧. 首先,如果你在一个局域网里,有公用的可以访问的服务器,那可以直接 ...

  7. SQL EXPLAIN解析

    本文转载自MySQL性能优化最佳实践 - 08 SQL EXPLAIN解析 什么是归并排序? 如果需要排序的数据超过了sort_buffer_size的大小,说明无法在内存中完成排序,就需要写到临时文 ...

  8. 读取excel数据生成sql脚本

    package com.interact.util; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.b ...

  9. oracle startup startup nomount startup mount 的区别

    startup nomount选项启动实例,但不安装 数据库.当数据库以这个模式启动时,参数文件被读取:后台进程和内存结构被启动:但它们不被附加或与数据库的磁盘结构进行通信.当实例处于这个状态时sta ...

  10. Java文件字节流

    //输出和输入流 package com.kangkang.IO; import com.sun.xml.internal.ws.util.xml.CDATA; import java.io.File ...