sed中標識符\b和\B的用法

[root@linux01 ~]# cat file1.txt 
test 1
one two three proce
test 2
abc bca twher tw file
tw wsr239wfgrte
fw9efs0art4 tw
4tesd8w40-sdgd
test 3
43q8wsfjtwrskdpoht
rear8ut9spgoogtw
45678
s34otjvsa;
test 4
twfdoetw
[root@linux01 ~]# sed 's/\btw/\U&/g' file1.txt 
test 1
one TWo three proce
test 2
abc bca TWher TW file
TW wsr239wfgrte
fw9efs0art4 TW
4tesd8w40-sdgd
test 3
43q8wsfjtwrskdpoht
rear8ut9spgoogtw
45678
s34otjvsa;
test 4
TWfdoetw
[root@linux01 ~]# sed 's/tw\b/\U&/g' file1.txt 
test 1
one two three proce
test 2
abc bca twher TW file
TW wsr239wfgrte
fw9efs0art4 TW
4tesd8w40-sdgd
test 3
43q8wsfjtwrskdpoht
rear8ut9spgoogTW
45678
s34otjvsa;
test 4
twfdoeTW
[root@linux01 ~]# sed 's/\btw\b/\U&/g' file1.txt 
test 1
one two three proce
test 2
abc bca twher TW file
TW wsr239wfgrte
fw9efs0art4 TW
4tesd8w40-sdgd
test 3
43q8wsfjtwrskdpoht
rear8ut9spgoogtw
45678
s34otjvsa;
test 4
twfdoetw
[root@linux01 ~]# sed 's/\Btw/\U&/g' file1.txt 
test 1
one two three proce
test 2
abc bca twher tw file
tw wsr239wfgrte
fw9efs0art4 tw
4tesd8w40-sdgd
test 3
43q8wsfjTWrskdpoht
rear8ut9spgoogTW
45678
s34otjvsa;
test 4
twfdoeTW
[root@linux01 ~]# sed 's/tw\B/\U&/g' file1.txt 
test 1
one TWo three proce
test 2
abc bca TWher tw file
tw wsr239wfgrte
fw9efs0art4 tw
4tesd8w40-sdgd
test 3
43q8wsfjTWrskdpoht
rear8ut9spgoogtw
45678
s34otjvsa;
test 4
TWfdoetw
[root@linux01 ~]# sed 's/\Btw\B/\U&/g' file1.txt 
test 1
one two three proce
test 2
abc bca twher tw file
tw wsr239wfgrte
fw9efs0art4 tw
4tesd8w40-sdgd
test 3
43q8wsfjTWrskdpoht
rear8ut9spgoogtw
45678
s34otjvsa;
test 4
twfdoetw

[root@linux01 ~]# sed 's/\btw\B/\U&/g' file1.txt 
test 1
one TWo three proce
test 2
abc bca TWher tw file
tw wsr239wfgrte
fw9efs0art4 tw
4tesd8w40-sdgd
test 3
43q8wsfjtwrskdpoht
rear8ut9spgoogtw
45678
s34otjvsa;
test 4
TWfdoetw
[root@linux01 ~]# sed 's/\Btw\b/\U&/g' file1.txt 
test 1
one two three proce
test 2
abc bca twher tw file
tw wsr239wfgrte
fw9efs0art4 tw
4tesd8w40-sdgd
test 3
43q8wsfjtwrskdpoht
rear8ut9spgoogTW
45678
s34otjvsa;
test 4
twfdoeTW
  • 解釋:
    **1. 以特殊符號分隔開來的字符串識別爲單詞 **
    2. \b表示匹配文本中單詞的開頭或結尾字符,同時匹配字符組成的單詞,\bxx表示匹配xx字符開頭單詞的xx位置,xx\b表示匹配xx字符結尾單詞的xx位置,\bxx\b表示匹配單獨以xx字符組成的單詞 **3. \B表示匹配文本中非單詞開頭和結尾字符,\Bxx表示匹配xx字符結尾單詞的xx位置,xx\B表示匹配xx字符開頭單詞的xx位置,\Bxx\B表示匹配xx字符在文本中非單詞開頭和結尾的xx位置 ** **4. \bxx\B表示匹配xx字符開頭單詞但不包括xx字符組成的單詞,定位單詞的xx位置 ** **5. \Bxx\b表示匹配xx字符結尾單詞但不包括xx字符組成的單詞,定位單詞的xx位置 **
相關文章
相關標籤/搜索