linux系統中正則表示式記錄
阿新 • • 發佈:2021-11-21
1、[^xx] 表示取反
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt ## 測試資料 333 d g 8 3 d g ! _ d g ! ! ! , . ? root@PC1:/home/test# grep -v "3" a.txt ## 只要匹配到3就排除 d g ! _ d g ! ! ! , . ? root@PC1:/home/test# grep "[^3]" a.txt ## 只有全部為3才排除 d g 8 3 d g ! _ d g ! ! ! , . ?
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt 333 d g 8 3 d g ! 555 _ d g ! ! ! , . ? root@PC1:/home/test# grep "[^3]" a.txt d g 8 3 d g ! 555 _ d g ! ! ! , . ? root@PC1:/home/test# grep "[^35]" a.txt ## 這裡3和5是或的關係 d g 8 3 d g ! _ d g ! ! ! , . ?
2、-w 表示匹配字元數字
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt 333 d g8 3 d g ! 555 _ d g ! ! ! , . ? root@PC1:/home/test# grep "\w" a.txt 333 d g 8 3 d g ! 555 _ d g
-W表示匹配非字元數字:
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt 333 d g 8 3 d g ! 555 _ d g !!! ,.? root@PC1:/home/test# grep "\W" a.txt d g 8 3 d g ! _ d g !!! ,.?
3、