linux 中 grep命令匹配空格和制表符

发布时间 2023-05-28 23:55:48作者: 小鲨鱼2018

 

001、匹配空格

[root@PC1 test4]# ls
a.txt
[root@PC1 test4]# cat a.txt        ## 测试数据
1_aa bb
2_ccdd
3_ee   ff
4_gg    hh      kk
[root@PC1 test4]# sed -n l a.txt   ## 显示出空格和制表符
1_aa bb$
2_ccdd$
3_ee   ff$
4_gg\thh\tkk$
[root@PC1 test4]# grep " " a.txt   ## 仅匹配空格
1_aa bb
3_ee   ff

 

002、匹配制表符

[root@PC1 test4]# ls
a.txt
[root@PC1 test4]# cat a.txt
1_aa bb
2_ccdd
3_ee   ff
4_gg    hh      kk
[root@PC1 test4]# sed -n l a.txt
1_aa bb$
2_ccdd$
3_ee   ff$
4_gg\thh\tkk$
[root@PC1 test4]# grep $'\t' a.txt       ## 匹配制表符
4_gg    hh      kk

 

003、同时匹配空格和制表符

[root@PC1 test4]# ls
a.txt
[root@PC1 test4]# cat a.txt           ## 测试数据
1_aa bb
2_ccdd
3_ee   ff
4_gg    hh      kk
[root@PC1 test4]# sed -n l a.txt
1_aa bb$
2_ccdd$
3_ee   ff$
4_gg\thh\tkk$
[root@PC1 test4]# grep "\s" a.txt
1_aa bb
3_ee   ff
4_gg    hh      kk
[root@PC1 test4]# grep "[[:blank:]]" a.txt
1_aa bb
3_ee   ff
4_gg    hh      kk
[root@PC1 test4]# grep "[[:space:]]" a.txt
1_aa bb
3_ee   ff
4_gg    hh      kk