bash 字符串處理

bash 字符串處理

 

字符串切片:${var:offset:length}
示例:
[root@localhost ~]#mypath="/etc/sysconfig/network-scripts/"    #定義一個變量,等會切這個變量
[root@localhost ~]# echo ${mypath:5}       #偏移5個字符顯示
sysconfig/network-scripts/
[root@localhost ~]# echo ${mypath:10}     #偏移10個字符顯示
nfig/network-scripts/
[root@localhost ~]# echo ${mypath:5:5}    #偏移5個字符,取5個字符 
sysco

取出字符串的最後幾個字符:${var: -length}
注意:-length以前有空白字符;
[root@localhost ~]# echo ${mypath: -10}
k-scripts/

基於模式取子串:
${var#*word}:自左而右,查找var變量中存儲的字符串中第一次出現的由word所指明的字符,刪除此字符及其左側的全部內容;
示例:
[root@localhost ~]#mypath="/etc/sysconfig/network-scripts"
[root@localhost ~]# echo ${mypath#*/}
etc/sysconfig/network-scripts

${var##*word}:自左而右,查找var變量中存儲的字符串中最後一次出現的由word所指明的字符,刪除此字符及其左側的全部內容;
[root@localhost ~]#mypath="/etc/sysconfig/network-scripts"
[root@localhost ~]# echo ${mypath##*/}
network-scripts

再舉兩個示例:
[root@localhost ~]#mypath="/etc/sysconfig/network-scripts"
[root@localhost ~]# echo ${mypath##*c}
ripts
[root@localhost ~]# echo ${mypath#*c}
/sysconfig/network-scripts

${var%word*}:自右而左,查找var變量中存儲的字符串中第一次出現的由word所指明的字符,刪除此字符及其右側的全部內容;
${var%%word*}:自右而左,查找var變量中存儲的字符串中最後一次出現的由word所指明的字符,刪除此字符及其右側的全部內容;

示例:
[root@localhost ~]#mypath="/etc/sysconfig/network-scripts"
[root@localhost ~]# echo ${mypath%c*}
/etc/sysconfig/network-s
[root@localhost ~]# echo ${mypath%%c*}
/et

示例:取一個URL的協議和端口
[root@localhost ~]#url="http://www.baidu.com:80"
[root@localhost ~]# echo ${url##*:}
80
[root@localhost ~]# echo ${url%%:*}
http

查找替換:   
${var/pattern/replacement}:查找var變量存儲的字符中第一次由pattern匹配到的內容,並替換爲replacement;
[root@localhost ~]#url="http://www.baidu.com:80"
[root@localhost ~]# echo ${url/www/WWW}
http://WWW.baidu.com:80
[root@localhost ~]# echo ${url/w/W}
http://Www.baidu.com:80

${var//pattern/replacement}:查找var變量存儲的字符中全部可以由pattern匹配到的內容,並替換爲replacement;
[root@localhost ~]# echo ${url//w/W}
http://WWW.baidu.com:80

${var/#pattern/replacement}:查找var變量存儲的字符中最開始處可以由pattern匹配到的內容,並替換爲replacement;
[root@localhost ~]# userinfo="root:x:0:0:rootuser:/root:/bin/bash"
[root@localhost ~]# echo ${userinfo/#root/ROOT}
ROOT:x:0:0:root user:/root:/bin/bash

${var/%pattern/replacement}:查找var變量存儲的字符中最後位置可以由pattern匹配到的內容,並替換爲replacement;
[root@localhost ~]# userinfo="root:x:0:0:rootuser:/root:/bin/root"
[root@localhost ~]# echo ${userinfo/%root/ROOT}
root:x:0:0:root user:/root:/bin/ROOT

查找刪除:   
${var/pattern}:查找var變量存儲的字符中第一次由pattern匹配到的內容,並刪除;
${var//pattern}:查找var變量存儲的字符中全部可以由pattern匹配到的內容,並刪除;
${var/#pattern}:查找var變量存儲的字符中最開始處可以由pattern匹配到的內容,並刪除;
${var/%pattern}:查找var變量存儲的字符中最後位置可以由pattern匹配到的內容,並刪除;
示例:
[root@localhost ~]# userinfo="root:x:0:0:rootuser:/root:/bin/root"
[root@localhost ~]# echo ${userinfo/root}
:x:0:0:root user:/root:/bin/root
[root@localhost ~]# echo ${userinfo//root}
:x:0:0: user:/:/bin/
[root@localhost ~]# echo ${userinfo/#root}
:x:0:0:root user:/root:/bin/root
[root@localhost ~]# echo ${userinfo/%root}
root:x:0:0:root user:/root:/bin/


字符串大小寫轉換:
${var^^}:把var變量中的全部小寫字母,通通替換爲大寫;
${var,,}:把var變量中的全部大寫字母,通通替換爲小寫;
示例:
[root@localhost ~]# echo $userinfo
root:x:0:0:root user:/root:/bin/root
[root@localhost ~]# myinfo=${userinfo^^}
[root@localhost ~]# echo $myinfo
ROOT:X:0:0:ROOT USER:/ROOT:/BIN/ROOT
[root@localhost ~]# echo ${myinfo,,}
root:x:0:0:root user:/root:/bin/root

變量賦值:   
${var:-word}:若是變量var爲空或未聲明,則返回word所表示的字符串;不然,則返回var變量的值;
[root@localhost ~]# echo $name
                      #這行的值爲空
[root@localhost ~]# echo ${name:-tom}
tom
[root@localhost ~]# name=hello 
[root@localhost ~]# echo ${name:-tom}
hello

${var:=word}:若是變量var爲空或未聲明,則返回word所表示的字符串,而且把word賦值爲var變量;不然,則返回var變量的值;
[root@localhost ~]# echo $name
                     #這行的值爲空
[root@localhost ~]# name=${name:-tom}
[root@localhost ~]# echo $name
tom
[root@localhost ~]# name=${name:-jerry}
[root@localhost ~]# echo $name
tom

${var:?error}:若是變量var爲空或未聲明,則返回error爲錯誤信息;不然,則返回var變量的值;
[root@localhost ~]# echo "User's name is${name:?wrong}"
-bash: name: wrong
[root@localhost ~]# name=tom
[root@localhost ~]# echo "User's name is${name:?wrong}"
User's name is tom

${var:+word}:若是變量var爲空或未聲明,忽略;不然,則返回word;
[root@localhost ~]# unset name
[root@localhost ~]# echo "User's name is${name:+wrong}"
User's name is 
[root@localhost ~]# name=tom
[root@localhost ~]# echo "User's name is${name:+wrong}"
User's name is wrongbash

 

============== Endurl

相關文章
相關標籤/搜索