shell字串處理總結

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

1. 字串切片

1.1 基於偏移量取字串

返回字串 string 的長度 ${#string}

示例

[[email protected] script]#str=" I Love Python "
[[email protected] script]#echo ${#str}
15
[[email protected] script]#

返回字串變數var中從第offset個字元後(不包括第offset個字元)的字元開始,到最後的部分, offset的取值在0 到 ${#var}-1 之間(bash4.2後,允許為負值)
${var:offset}

[[email protected] script]#str="I Love Python"
[[email protected] script]#echo ${str:2}        # 從第2個字元開始取,預設取後面字元的全部,第2個字元不包含在內
Love Python
[[email protected] script]#

返回字串變數var中從第offset個字元後(不包括第offset個字元)的字元開始,長度為number的部分 ${var:offset:number}

[[email protected] script]#str="I Love Shell"
[[email protected] script]#echo ${str:2:4}   # 從第2個字元開始取,取4個字元
Love
[[email protected] script]#

示例

[[email protected] script]#str="I am CEO I am CTO"
[[email protected] script]#echo ${str:0:$((${#str}-8))}
I am CEO
[[email protected] script]#

示例:年月日

[[email protected] script]#today=$(date +"%Y%m%d")
[[email protected] script]#echo $today
20210408
[[email protected] script]#Y_m_d=${today:0:4}"-"${today:4:2}"-"${today:6:2}
[[email protected] script]#echo $Y_m_d
2021-04-08
[[email protected] script]#month=${today:0:6}
[[email protected] script]#echo $month
202104
[[email protected] script]#

取字串的最右側幾個字元,取字串的最右側幾個字元, 注意:冒號後必須有一空白字元 ${var: -length}

[[email protected] script]#str="I Love Shell"
[[email protected] script]#echo ${str:-3}
I Love Shell
[[email protected] script]#echo ${str: -3}
ell
[[email protected] script]#

從最左側跳過offset字元,一直向右取到距離最右側lengh個字元之前的內容,即:掐頭去尾 ${var:offset:-length}

[[email protected] script]#str="I Love Shell"
[[email protected] script]#echo ${str:2:-3}
Love Sh
[[email protected] script]#

先從最右側向左取到length個字元開始,再向右取到距離最右側offset個字元之間的內容,注意:- length前空格
${var: -length:-offset}

[[email protected] script]#str="I Love Shell"
[[email protected] script]#echo ${str: -2:-3}
-bash: -3: substring expression < 0
[[email protected] script]#echo ${str: -3:-2}
e
[[email protected] script]#echo ${str:-3:-2}
I Love Shell
[[email protected] script]#echo ${str: -3:-2}
e
[[email protected] script]#echo ${str: -5:-2}
She
[[email protected] script]#

1.2 基於模式取子串

# 其中word可以是指定的任意字元,自左而右,查詢var變數所儲存的字串中,第一次出現的word, 刪除字 符串開頭至第一次出現word字串(含)之間的所有字元
${var#*word}


# 同上,貪婪模式,不同的是,刪除的是字串開頭至最後一次由word指定的字元之間的所有內容
${var##*word}

示例

[[email protected] script]#file="var/log/messages"
[[email protected] script]#echo ${file#*/}
log/messages
[[email protected] script]#echo ${file##*/}
messages
[[email protected] script]#
# 其中word可以是指定的任意字元,功能:自右而左,查詢var變數所儲存的字串中,第一次出現的word, 刪除字串最後一個字元向左至第一次出現word字串(含)之間的所有字元
${var%word*}

#  同上,只不過刪除字串最右側的字元向左至最後一次出現word字元之間的所有字元 
${var%%word*}

示例

[[email protected] script]#file="var/log/messages"
[[email protected] script]#echo ${file%/*}
var/log
[[email protected] script]#echo ${file%%/*}
var
[[email protected] script]#

示例

[[email protected] script]#url=http://www.baidu.com:8080
[[email protected] script]#echo ${url##*:}
8080
[[email protected] script]#echo ${url%%:*}
http
[[email protected] script]#

2. 查詢替換

查詢 string 所表示的字串中,使用 replace 替換第一次被 substring 所匹配到的字串

${string/substring/replace}     

示例

[[email protected] script]#str="I LOVE Shell,I LOVE Python"
[[email protected] script]#echo ${str/LOVE/like}
I like Shell,I LOVE Python
[[email protected] script]#

查詢 string 所表示的字串中,使用 replace 替換,所有能被 substring 所匹配到的字串

${string//substring/replace}

示例

[[email protected] script]#str="I LOVE Shell,I LOVE Python"
[[email protected] script]#echo ${str//LOVE/like}
I like Shell,I like Python
[[email protected] script]#

查詢 string 所表示的字串中,以 replace 替換,行首被 substring 所匹配到的字串,

${string/#substring/replace}   

示例

[[email protected] script]#str="I Love Go I Love Go"
[[email protected] script]#echo ${str/#Go/Python}
I Love Go I Love Go
[[email protected] script]#

查詢 string 所表示的字串中,以 replace 替換,行尾被 substring 所匹配到的字串,

${string/%substring/replace}    

示例

[[email protected] script]#str="I Love Go I Love Go"
[[email protected] script]#echo ${str/%Go/Python}
I Love Go I Love Python
[[email protected] 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}

[[email protected] script]#test="xufengnian"
[[email protected] script]#result=${test:=UNSET}
[[email protected] script]#echo $result
xufengnian
[[email protected] script]#echo $test
xufengnian
[[email protected] script]#

${value:?word}

[[email protected] script]#test="xufengnian"
[[email protected] script]#result=${test:?UNSET}
[[email protected] script]#echo $result
xufengnian
[[email protected] script]#echo $test
xufengnian
[[email protected] script]#

${value:+word}

[[email protected] script]#test="xufengnian"
[[email protected] script]#result=${test:+UNSET}
[[email protected] script]#echo $result
UNSET
[[email protected] script]#echo $test
xufengnian
[[email protected] script]#

3. 查詢並刪除

# 從變數$string開頭開始刪除最短匹配$substring子串
${string#substring}            

[[email protected] script]#str="I am xufengnian I am"
[[email protected] script]#echo ${str#I am}
xufengnian I am
[[email protected] script]#
# 從變數$string開頭開始刪除最長匹配$substring子串
${string##substring}    

[[email protected] script]#str="I am xufengnian I am"
[[email protected] script]#echo ${str##I am}
xufengnian I am
[[email protected] script]#
# 從變數$string結尾開始刪除最短匹配$substring子串
${string%substring}            

[[email protected] script]#str="I am xufengian I am"
[[email protected] script]#echo ${str%I am}
I am xufengian
[[email protected] script]#

# 從變數$string結尾開始刪除最長匹配$substring子串
${string%%substring}           

[[email protected] script]#str="I am xufengnian I am"
[[email protected] script]#echo ${str%%I am}
I am xufengnian
[[email protected] script]#
# 刪除var表示的字串中第一次被pattern匹配到的字串 
${var/pattern}

[[email protected] script]#str="I am xufengnian I am"
[[email protected] script]#echo ${str/I am}
xufengnian I am
[[email protected] script]#
# 刪除var表示的字串中所有被pattern匹配到的字串 
${var//pattern}

[[email protected] script]#str="I am xufengnian I am"
[[email protected] script]#echo ${str//I am}
xufengnian
[[email protected] 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,,}