日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

bash字符串处理

發(fā)布時間:2025/4/16 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 bash字符串处理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

字符串切片:

${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變量中存儲的字符串中第一次出現(xiàn)的由word所指明的字符,刪除此字符及其左側(cè)的所有內(nèi)容;

?[root@localhost?~]#mypath="/etc/sysconfig/network-scripts" [root@localhost?~]#?echo?${mypath#*/} etc/sysconfig/network-scripts

${var##*word}:自左而右,查找var變量中存儲的字符串中最后一次出現(xiàn)的由word所指明的字符,刪除此字符及其左側(cè)的所有內(nèi)容;

[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變量中存儲的字符串中第一次出現(xiàn)的由word所指明的字符,刪除此字符及其右側(cè)的所有內(nèi)容;

${var%%word*}:自右而左,查找var變量中存儲的字符串中最后一次出現(xiàn)的由word所指明的字符,刪除此字符及其右側(cè)的所有內(nèi)容;

示例:

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

示例:取一個URL的協(xié)議和端口

[root@localhost?~]#url="http://www.baidu.com:80" [root@localhost?~]#?echo?${url##*:} 80 [root@localhost?~]#?echo?${url%%:*} http

?

查找替換:

??? ${var/pattern/replacement}:查找var變量存儲的字符中第一次由pattern匹配到的內(nèi)容,并替換為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匹配到的內(nèi)容,并替換為replacement;

[root@localhost?~]#?echo?${url//w/W} http://WWW.baidu.com:80

??? ${var/#pattern/replacement}:查找var變量存儲的字符中最開始處能夠由pattern匹配到的內(nèi)容,并替換為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匹配到的內(nèi)容,并替換為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匹配到的內(nèi)容,并刪除;

??? ${var//pattern}:查找var變量存儲的字符中所有能夠由pattern匹配到的內(nèi)容,并刪除;

??? ${var/#pattern}:查找var變量存儲的字符中最開始處能夠由pattern匹配到的內(nèi)容,并刪除;

??? ${var/%pattern}:查找var變量存儲的字符中最后位置能夠由pattern匹配到的內(nèi)容,并刪除;

[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/

字符串大小寫轉(zhuǎn)換:

??? ${var^^}:把var變量中的所有小寫字母,統(tǒng)統(tǒng)替換為大寫;

??? ${var,,}:把var變量中的所有大寫字母,統(tǒng)統(tǒng)替換為小寫;

[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?wrong

轉(zhuǎn)載于:https://blog.51cto.com/ximenfeibing/1663396

總結(jié)

以上是生活随笔為你收集整理的bash字符串处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。