shell字串處理總結

臨江仙我亦是行人發表於2021-04-08

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,,}

相關文章