5

Linux grep命令详细教程 - 天外归云

 1 year ago
source link: https://www.cnblogs.com/LanTianYou/p/17359397.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Linux grep命令详细教程

[本文出自天外归云的博客园]

Linux grep命令是一种非常常用的文本搜索工具,它可以在给定的文件中搜索匹配的字符串,并输出匹配的行。grep是全称“global search regular expression print”,可以识别正则表达式,并使用正则表达式进行搜索。

以下是 grep 的所有选项:

grep [options] [pattern] [files]
  • -a, --text: 将二进制文件作为文本文件处理。
  • -c, --count: 显示匹配行数,而不是行本身。
  • -e pattern, --regexp=pattern: 查找指定的模式,支持多个模式。
  • -f file, --file=file: 从文件中读取模式,每个模式占一行。
  • -i, --ignore-case: 忽略大小写。
  • -l, --files-with-matches: 只打印文件名,而不是行本身。
  • -n, --line-number: 在每行的前面打印行号。
  • -r, --recursive: 遍历子目录中的文件。
  • -v, --invert-match: 输出不匹配的行。
  • -x, --line-regexp: 仅匹配整行。
  • -w, --word-regexp: 仅匹配整个单词。

pattern 通常是一个正则表达式,用于匹配指定的文本模式。

files 可以是若干个文件,也可以是目录。

以上是 grep 命令的所有选项,更多信息可以使用 man grep 在终端中查看。

常用grep选项举例

下面是常用的grep选项及其举例:

  1. 搜索指定字符串

可以使用grep搜索包含指定字符串的文件或文件夹,例如:

grep "hello" file.txt
grep "hello" folder/file.txt
  1. 搜索多个字符串

使用-E选项可以在同一行中搜索多个字符串。

grep -E "hello|world" file.txt
  1. 忽略大小写

使用-i选项可以忽略大小写的差异。

grep -i "hello" file.txt

使用-n选项可以输出匹配字符串所在行的行号。

grep -n "hello" file.txt

使用-v选项可以输出不包含指定字符串的行。

grep -v "hello" file.txt
  1. 搜索某个范围内的行

使用 -A, -B, 或-C选项搜索特定范围内的行。

grep -A 2 "hello" file.txt  #输出包含“hello”的行以及后两行。
grep -B 2 "hello" file.txt  #输出包含“hello”的行以及前两行。
grep -C 2 "hello" file.txt  #输出包含“hello”的行以及前后两行。
  1. 搜索整个单词

使用-w选项可以搜索指定单词作为整个单词匹配。

grep -w "hello" file.txt
  1. 统计匹配次数

使用-c选项可以统计匹配字符串的个数。

grep -c "hello" file.txt
  1. 搜索指定文件类型

使用通配符可以搜索特定类型的文件或使用 --include选项来仅搜索指定文件类型。

grep "hello" *.txt
grep "hello" --include "*.txt" folder/
  1. 搜索子目录

使用-r或-R选项可以搜索子目录的文件。

grep -r "hello" folder/
grep -R "hello" folder/
  1. 不忽略二进制文件

使用-a选项可以强制grep搜索二进制文件。

grep -a "hello" binary_file.bin
  1. 搜索时忽略特定目录

使用 --exclude-dir选项来忽略特定目录的搜索。

grep -r "hello" folder/ --exclude-dir=log/
  1. 搜索特定行数

使用 -m 选项指定只搜索文件中的前几行。

grep -m 10 'hello' file.txt # 只搜索文件中的前10行
  1. 输出匹配字符串前后的内容

使用 -o 选项仅输出匹配字符串,而 -A 和 -B 选项可以输出字符串前后的内容。

grep -o 'hello' file.txt # 只输出匹配到的 'hello' 字符串,而不包含它前后的内容
grep -A 3 'hello' file.txt # 输出包含 'hello' 字符串的行以及后三行
grep -B 2 'hello' file.txt # 输出包含 'hello' 字符串的行以及前两行
  1. 显示不匹配行

使用 -L 选项输出不匹配指定字符串的行。

grep -L 'hello' file.txt # 输出不匹配 'hello' 字符串的行
  1. 显示匹配行前几行和后几行的内容

使用 -C 选项,可以同时输出匹配字符串前后几行的内容。

grep -C 2 'hello' file.txt # 输出包含 'hello' 字符串的行以及前后两行内容
  1. 搜索多个文件

可以一次性搜索多个文件。

grep 'hello' file1.txt file2.txt file3.txt # 搜索 file1.txt, file2.txt, file3.txt 文件中的 'hello' 字符串
  1. 搜索时忽略空白字符

使用 -w 选项,可以忽略匹配字符串前后的空格、制表符等空白字符。

grep -w 'hello' file.txt # 忽略匹配字符串前后的空格、制表符等空白字符
  1. 搜索时查看匹配字符串的上文或下文

使用 -B 和 -A 选项,可查看匹配字符串上下文的内容。

grep -B 2 'hello' file.txt # 输出包含 ‘hello’ 字符串的行以及匹配字符串前2行
grep -A 3 'hello' file.txt # 输出包含 ‘hello’ 字符串的行以及匹配字符串后3行

以上就是grep的所有功能举例。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK