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. full page screen capture in js

    full page screen capture in js html2canvas https://html2canvas.hertzen.com/ https://github.com/nikla ...

  2. Dva & Umi

    Dva & Umi Dva.js & Umi.js React & Redux https://dvajs.com/ React and redux based, lightw ...

  3. Chrome 80 & SameSite & cookie

    Chrome 80 & SameSite & cookie chrome://settings/help https://developers.google.com/web/updat ...

  4. qt 获取窗口句柄的线程id和进程id GetWindowThreadProcessId

    int lpdwProcessId; int id = GetWindowThreadProcessId((HWND)0x707d6, (LPDWORD)&lpdwProcessId); qD ...

  5. 什么是USDN稳定币?USDN的应用价值是什么?

    9月22日,美国货币监理署(OCC)发布了一项稳定币指南,主要内容围绕的是稳定币的监管及相关规定.一时间,稳定币得到了市场上广泛的关注.那么,什么是稳定币呢?什么又是USDN稳定币呢? 1.什么是稳定 ...

  6. RTPS解析

    资料参考: https://blog.csdn.net/HBS2011/article/details/102520704

  7. centos 修改系统时间

    centos 修改系统时间 [echo0282@instance-1 ~]$ sudo timedatectl set-timezone Asia/Shanghai   timedatefctl li ...

  8. 小白养成记——MySQL中的排名函数

    1.ROW_NUMBER() 函数 依次排序,没有并列名次.如 SELECT st.ID '学号', st.`NAME` '姓名', sc.SCORE '成绩', ROW_NUMBER() OVER( ...

  9. 在16G笔记本上安装GaussDB 200

    云主机太贵(最便宜的每月几千吧),长期如果需要GaussDB200有个功能测试或学习环境,那么性价比最高的方式还是在自己的笔记本电脑上尝试安装一个本地的数据库进行学习和功能验证. 01 安装环境信息 ...

  10. 后端程序员之路 52、A Tour of Go-2

    # flowcontrol    - for        - for i := 0; i < 10; i++ {        - for ; sum < 1000; {        ...