linux系统中正则表达式记录

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 g 8 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、

原文地址:https://www.cnblogs.com/liujiaxin2018/p/15584037.html