linux文本搜索工具是什么-grep 命令使用与实例

linux文本搜索工具是什么-grep 命令使用与实例

linux grep 命令

Linux 中的 grep(global regular expression)命令主要用于在文件中查找符合特定条件的字符串正则表达式内容。

该命令可用于搜索包含指定模板样式的文件内容,若某文件中存在匹配的样式,默认情况下 grep 会输出该行内容。如果不指定具体文件名,或者文件名为“-”,则命令将从标准输入读取数据。

命令格式

<pre class="brush:php;toolbar:false">grep [options] pattern [files] 或 grep [-abcEFGhHilLnqrsvVwxy][-A][-B][-C][-d][-e][-f][--help][范本样式][文件或目录...]
  • pattern:要搜索的字符串或正则表达式。
  • files:需要搜索的文件名,可同时处理多个文件。若未指定,则默认从标准输入获取内容。

常用选项说明:

  • -i

    :忽略大小写进行匹配。

  • -v

    :反向查找,仅输出不匹配的行。

  • -n

    :显示匹配行的行号。

  • -r

    递归搜索子目录中的文件。

  • -l

    :仅输出匹配的文件名。

  • -c

    :统计匹配的行数。

更多参数说明:

  • -a 或 –text : 不忽略二进制数据。
  • -A 或 –after-context= : 显示匹配行及其之后的内容。
  • -b 或 –byte-offset : 输出匹配行前第一个字符的位置编号。
  • -B 或 –before-context= : 显示匹配行及其之前的内容。
  • -c 或 –count : 统计匹配行的数量。
  • -C 或 –context=或- : 显示匹配行前后的内容。
  • -d 或 –directories= : 若查找目标为目录,必须使用此参数。
  • -e 或 –regexp= : 指定用于查找的字符串模式。
  • -E 或 –extended-regexp : 使用扩展正则表达式。
  • -f 或 –file= : 指定规则文件,每行为一个规则。
  • -F 或 –fixed-regexp : 将搜索模式视为固定字符串列表。
  • -G 或 –basic-regexp : 使用基本正则表达式。
  • -h 或 –no-filename : 不显示文件名。
  • -H 或 –with-filename : 显示文件名。
  • -i 或 –ignore-case : 忽略大小写差异。
  • -l 或 –file-with-matches : 列出包含匹配项的文件名。
  • -L 或 –files-without-match : 列出不包含匹配项的文件名。
  • -n 或 –line-number : 显示行号。
  • -o 或 –only-matching : 仅显示匹配部分。
  • -q 或 –quiet或–silent : 不输出任何信息。
  • -r 或 –recursive : 递归搜索目录。
  • -s 或 –no-messages : 不显示错误信息。
  • -v 或 –invert-match : 显示不匹配的行。
  • -V 或 –version : 显示版本信息。
  • -w 或 –word-regexp : 只匹配完整单词。
  • -x –line-regexp : 只匹配整行完全匹配的情况。
  • -y : 效果同
    -i

示例演示

1、在 file.txt 文件中查找 “hello” 字符串,并输出匹配的行:

<pre class="brush:php;toolbar:false">grep hello file.txt

2、递归查找 dir 目录下所有文件中符合 “pattern” 的内容,并显示文件名和行号:

<pre class="brush:php;toolbar:false">grep -r -n pattern dir/

3、从标准输入中查找 “world”,并仅输出匹配行的数量:

<pre class="brush:php;toolbar:false">echo "hello world" | grep -c world

4、在当前目录中查找后缀为 file 的文件中是否包含 test 字符串,并输出相关行:

<pre class="brush:php;toolbar:false">grep test *file 

执行结果如下:

<pre class="brush:php;toolbar:false">$ grep test test*   testfile1:This a Linux testfile!   testfile_2:This is a linux testfile!   testfile_2:Linux test 

5、递归查找 /etc/acpi 目录下的所有文件中是否包含 “update”,并输出匹配行内容:

<pre class="brush:php;toolbar:false">grep -r update /etc/acpi 

输出示例:

<pre class="brush:php;toolbar:false">$ grep -r update /etc/acpi   /etc/acpi/ac.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of IO.) Rather than   /etc/acpi/resume.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of IO.) Rather than   /etc/acpi/events/thinkpad-cmos:action=/usr/sbin/thinkpad-keys--update 

6、反向查找:查找文件名含 test 的文件中不含 test 的行:

<pre class="brush:php;toolbar:false">grep -v test *test*

输出如下:

<pre class="brush:php;toolbar:false">$ grep-v test*   testfile1:helLinux!   testfile1:Linis a free Unix-type operating system.   testfile1:Lin   testfile_1:HELLO LINUX!   testfile_1:LINUX IS A FREE UNIX-TYPE OPTERATING SYSTEM.   testfile_1:THIS IS A LINUX TESTFILE!   testfile_2:HELLO LINUX!   testfile_2:Linux is a free unix-type opterating system.  

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享