指点成金-最美分享吧

登录

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

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了linux 中 grep命令匹配空格和制表符相关的知识,希望对你有一定的参考价值。

 

001、匹配空格

[root@PC1 test4]# lsa.txt[root@PC1 test4]# cat a.txt        ## 测试数据1_aa bb2_ccdd3_ee   ff4_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 bb3_ee   ff

 

002、匹配制表符

[root@PC1 test4]# lsa.txt[root@PC1 test4]# cat a.txt1_aa bb2_ccdd3_ee   ff4_gg    hh      kk[root@PC1 test4]# sed -n l a.txt1_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]# lsa.txt[root@PC1 test4]# cat a.txt           ## 测试数据1_aa bb2_ccdd3_ee   ff4_gg    hh      kk[root@PC1 test4]# sed -n l a.txt1_aa bb$2_ccdd$3_ee   ff$4_gg\thh\tkk$[root@PC1 test4]# grep "\s" a.txt1_aa bb3_ee   ff4_gg    hh      kk[root@PC1 test4]# grep "[[:blank:]]" a.txt1_aa bb3_ee   ff4_gg    hh      kk[root@PC1 test4]# grep "[[:space:]]" a.txt1_aa bb3_ee   ff4_gg    hh      kk

 

Linux 下 grep 命令常用方法简介

1、从单个文件中搜索指定的字符串:

$ grep "literal-string" filename

该命令会输出字符串在文件中所在行的内容,如果字符串没有空格,则可以不加双引号。filename 可以是多个文件,每个文件用空格隔开。

 -i 参数可以忽略大小写。 加 -u 参数搜索一个单词而不是搜索含该单词的字符串

2、显示匹配行附近的多行:

  • -A 显示匹配行之后的n行

$ grep -A n "string" filename

  • -B 显示匹配行之前的n行

$ grep -B n "string" filename

  • -C 显示匹配行前后的n行

$ grep -C n "string" filename

3、递归搜索:-r

$ grep -r "this" *

搜索当前目录以及子目录下含“this”的全部文件。

4、不匹配搜索:-v

$ grep -v "go" demo_text

显示不含搜索字符串“go”的行。

5、统计匹配的行数:-c

$ grep -c "go" filename

统计文件中含“go”字符串的行数。

6、只显示含字符串的文件的文件名:-l

$ grep -l "this" filename

显示含“this”字符串的文件的文件名。

7、输出时显示行号:

grep -n "this" filename

显示含文件中含“this”字符串的行的行号。

以上是关于linux 中 grep命令匹配空格和制表符的主要内容,如果未能解决你的问题,请参考以下文章