【轉】說說{}與[]這兩個符號有什麼區別?
阿新 • • 發佈:2018-11-02
【轉】出自: http://blog.51cto.com/lidao/1926390
1.題目
說說{}與[]這兩個符號有什麼區別?
2.參考答案
這兩個看似簡單的符號,其實內容還不少。我們一起來看看。
2.1 萬用字元中
萬用字元在linux中通常用來匹配/找檔名或目錄名。
最常用的就是 ls -l *.txt
顯示出所有以.txt結尾的檔案。
2.1.1 {} 花括號,大括號,生產序列
[[email protected] regular]# echo {a..z} {0..9} a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9
用來生產數字,字母序列或者他們的組合
[[email protected] regular]# echo stu{0..9} stu0 stu1 stu2 stu3 stu4 stu5 stu6 stu7 stu8 stu9
2.1.2 []表示一個筐或括號表示式
[abc]表示a或者b或者c,即字母abc中的任何一個
[a-z]表示字母a到z中的任何一個字母
[[email protected] test]# touch oldboy{0..20}.txt [[email protected] test]# ls oldboy[0-9]*oldboy0.txt oldboy12.txt oldboy15.txt oldboy18.txt oldboy20.txt oldboy4.txt oldboy7.txt oldboy10.txt oldboy13.txt oldboy16.txt oldboy19.txt oldboy2.txt oldboy5.txt oldboy8.txt oldboy11.txt oldboy14.txt oldboy17.txt oldboy1.txt oldboy3.txt oldboy6.txt oldboy9.txt [[email protected] test]# ls oldboy[0-9].txt oldboy0.txt oldboy2.txt oldboy4.txt oldboy6.txt oldboy8.txt oldboy1.txt oldboy3.txt oldboy5.txt oldboy7.txt oldboy9.txt
2.2正則表示式中
正則表示式主要用於在檔案中查詢內容。
2.2.1 {n,m} 花括號 重複前面一個字元n次到m次
也可以理解為 前一個符號連續出現至少n次,最多m次。
[[email protected] test]# egrep "o{1,3}" /server/files/regular/oldboy.txt I am oldboy teacher! I like badminton ball ,billiard ball and chinese chess! my blog is http://oldboy.blog.51cto.com our size is http://blog.oldboyedu.com not 4900000448. my god ,i am not oldbey,but OLDBOY! [[email protected] test]# cat /server/files/regular/oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://oldboy.blog.51cto.com our size is http://blog.oldboyedu.com my qq is 49000448not 4900000448. my god ,i am not oldbey,but OLDBOY! [[email protected] test]# egrep "0{1,3}" /server/files/regular/oldboy.txt my qq is 49000448 not 4900000448. [[email protected] test]# egrep -o "0{1,3}" /server/files/regular/oldboy.txt 000 000 00
表示重複數字0至少1次最多3次。
或者數字0連續出現了至少1次最多3次。
2.2.2 [] 表示筐 [abc] 表示a或者b或者c
其實在正則表示式中和萬用字元中[]的意思是類似的。都表示一個筐 筐裡面的東西[abc] a或者b或者c.
[oldboy]表示的是oldboy這幾個字字母的任何一個字母。
[[email protected] test]# grep "[abc]" /server/files/regular/oldboy.txtI am oldboy teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://oldboy.blog.51cto.com our size is http://blog.oldboyedu.com my god ,i am not oldbey,but OLDBOY! [[email protected] test]# grep -o "[abc]" /server/files/regular/oldboy.txta b a c a c b a b a b a b a a c c b b b c c b b c a b b
2.3 總結
{}最常用的就是生成序列echo {a..z}
[]最常用的功能是正則表示式中的筐,表示或者 [abc] [a-z] [0-9]
3.統計資訊
今天是每日一題陪伴大家的第51天,期待你的進步。
對於題目和答案的任何疑問,請在部落格評論區留言。
往期題目索引
http://lidao.blog.51cto.com/3388056/1914205
【老鳥分享】Linux命令列終端提示符多種實用技巧!
https://mp.weixin.qq.com/s/g-bX6WVjJubv3ShY7jqs0Q