美文网首页
grep命令详解

grep命令详解

作者: supermanto | 来源:发表于2020-04-06 10:11 被阅读0次

1 简介

grep:基于正则表达式查找到满足条件的行

2 用法

grep patttern file
grep- i pattern file 忽略大小写
grep -v pattern file 不显示匹配行
grep -o pattern file 把每个匹配的内容用独立的行显示
grep -E pattern file 使用扩展正则表达式
grep -A -B -C pattern file 打印命中数据的上下文
grep -n 或 --line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。
grep pattern -r dir/ 递归搜索
ps:一般不会过滤文件,而是用管道|从上游获取输入

3 例子

创建一个hello.txt文件,文件内容如下:

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ cat hello.txt
hello from hogwarts
Hello from hogwarts
hello from sevenriby
hello from testerhome

(1) 最简单的用法:查找包含“hello”关键词的行

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ grep hello hello.txt
hello from hogwarts
hello from sevenriby
hello from testerhome

(2) grep -i ,不区分大小写

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ grep -i hello hello.txt
hello from hogwarts
Hello from hogwarts
hello from sevenriby
hello from testerhome

(3)grep -v:查找不包含关键词的行

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ grep -v hello hello.txt
Hello from hogwarts

(4) grep -o :只把该行匹配的内容输出

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ grep -o hello hello.txt
hello
hello
hello

(5)grep pattern -r dir/ 递归搜索:当要在某在目录下查找时

Administrator@PC-20141114NHWZ MINGW64 /e/shell
$ grep hello -r file_folder/
file_folder/hello.txt:hello from hogwarts
file_folder/hello.txt:hello from sevenriby
file_folder/hello.txt:hello from testerhome

4 正则表达式

以上例子都是根据字符串进行查找,下面学习一下匹配正则表达式来查找。

4.1 正则介绍

我把正则表达式分为两类,第一类称为基本表达式,基本表达式包括了典型的正则标识符。

^表示开头;
$表示结尾;
[]表示任意匹配项;
*表示0个或多个;
.表示任意字符。

第二类是扩展表达式,它在基础表达式的基础上做了一些扩展,支持了更高级的语法和更复杂的条件。

?表示非贪婪匹配;
+表示一个或多个;
() 表示分组;
{} 表示一个范围的约束;
| 表示匹配多个表达式中的任何一个。

1 输出包含href的行

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ curl https://testing-studio.com/ | grep href
Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ grep href filtering.txt

2 只显示匹配内容

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ curl https://testing-studio.com/ | grep -o href

3 查询该页面包含的所有链接

Administrator@PC-20141114NHWZ MINGW64 /e/shell/file_folder
$ curl https://testing-studio.com/ | grep href | grep -o "http[^\"']*"

4 grep 同时满足多个关键字
① grep -E "word1|word2|word3" file.txt
满足任意条件(word1、word2和word3之一)将匹配。
② grep word1 file.txt | grep word2 |grep word3
必须同时满足三个条件(word1、word2和word3)才匹配。

5 grep 同时排除多个关键字
排除 abc.txt 中的 mmm nnn
grep -v 'mmm|nnn' abc.txt

相关文章

  • Shell常见命令

    Shell常见命令 grep 命令格式详解 : grep [参数] [关键字] <文件名>参数详解-c:只输出匹配...

  • ps -ef|grep详解

    ps -ef|grep详解 ps命令将某个进程显示出来 grep命令是查找 中间的|是管道命令 是指ps命令与gr...

  • grep 命令详解

    [TOC] 介绍 grep (global search regular expression(RE) and p...

  • grep 命令详解

    1. 简介 grep: Gloabal Search Regular Expression and Print o...

  • grep命令详解

    1 简介 grep:基于正则表达式查找到满足条件的行 2 用法 grep patttern filegrep- i...

  • grep命令详解

    grep命令是文本搜索命令,它可以正则表达式搜索文本,也可从一个文件中的内容作为搜索关键字。 grep的工作方式是...

  • linux grep命令详解

    简介 grep (global search regular expression(RE) and print o...

  • Linux grep 命令详解

    Linux grep 命令用于查找文件里符合条件的字符串。 grep 指令用于查找内容包含指定的范本样式的文件,如...

  • 十、文件查找,打包压缩

    文件查找 grep: 文件内容过滤 查找命令 查询命令和配置文件的位置 一、find详解: 文件查找,针对文...

  • shell命令整理(六)

    文件查找 grep: 文件内容过滤 查找命令 查询命令和配置文件的位置 一、find详解: 文件查找,针对文...

网友评论

      本文标题:grep命令详解

      本文链接:https://www.haomeiwen.com/subject/lodlqhtx.html