首页 > 技术文章 > 【Linux命令】grep命令

wsx2019 2021-11-17 09:58 原文

# n: 显示搜索关键字在文章中第几行
grep -n '1987-10-10' 2.txt

# c: 显示匹配到的个数
grep -c '1987' 2.txt

# A: 显示匹配到关键字下面几行
grep -A 2 '1987-10-10' 2.txt 

# B: 显示匹配到关键字上面几行
grep -B 2 '1987-10-10' 2.txt 

# C: 显示匹配到关键字上下几行
grep -C 2 '1987-10-10' 2.txt

# i: 不区分大小写
grep -i 'DiscoveryClient-InstanceInfoReplicator-0' apollo-assembly.log

# l: 显示包含关键字的文件
grep -l 'exception' *.log

# L: 显示不包含关键字的文件
grep -L '孙' *.txt

##### 正则
# `^`表示行首
grep --color '^1993-01' 2.txt

# `$` 表示行尾
grep '17$' 2.txt
# 使用 w 或者 使用`\<`和`\>`来准确匹配关键字
grep -color '\<registration status\>' apollo-assembly.log
grep -color -w 'registration status' apollo-assembly.log

# --include 选项,一般配合 -r 递归同时使用
grep  -rl --include="*.js" ./ #列出当前以及子目录中所有"js"后缀的文件
grep -r --include=*.js --include=*.java  ./
grep -rl --include=*.{java,vue} re .  #多后缀使用正则,注意必须带上re


推荐阅读