命令格式
grep [选项] [模式] [文件]
命令功能
grep
命令用于在文件中查找指定模式的字符串,并输出包含该模式的行。
常用选项及操作实例
-
-i
:不区分大小写地查找。grep -i "apple" file.txt
-
-r
:递归查找目录下的文件。grep -r "apple" /path/to/directory
-
-n
:显示匹配行的行号。grep -n "apple" file.txt
-
-v
:显示不包含匹配模式的行。grep -v "apple" file.txt
-
-w
:匹配整个单词。grep -w "apple" file.txt
-
-E
:使用扩展正则表达式。grep -E "apple|orange" file.txt
-
-c
:输出匹配的行数。grep -c "apple" file.txt
-
-A
:输出匹配行及其后面的 n 行。grep -A 3 "apple" file.txt
-
-B
:输出匹配行及其前面的 n 行。grep -B 3 "apple" file.txt
-
-C
:输出匹配行及其前后的 n 行。grep -C 3 "apple" file.txt