`

grep 实用例子

 
阅读更多
首先创建示例文件
$ cat demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.
And this is the last line.

例1 从单个文件中搜索特定字符串
Syntax:
grep "literal_string" filename
$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
例2 在多个文件中检查特定字符串
Syntax:
grep "string" FILE_PATTERN

$ cp demo_file demo_file1

$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.

例3 忽略大小写
Syntax:
grep -i "string" FILE

$ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.

例4 正则表达式匹配
Syntax:
grep "REGEX" filename

$ grep "lines.*empty" demo_file
Two lines above this line is empty.

From documentation of grep: A regular expression may be followed by one of several repetition operators:

? The preceding item is optional and matched at most once.
* The preceding item will be matched zero or more times.
+ The preceding item will be matched one or more times.
{n} The preceding item is matched exactly n times.
{n,} The preceding item is matched n or more times.
{,m} The preceding item is matched at most m times.
{n,m} The preceding item is matched at least n times, but not more than m times.

例5 全单词匹配 Checking for full words, not for sub-strings using grep -w
$ grep -i "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.

$ grep -iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

例6 显示之前之后或者临近行
Displaying lines before/after/around the match using grep -A, -B and -C

先创建对应文件
$ cat demo_text
4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

* e - go to the end of the current word.
* E - go to the end of the current WORD.
* b - go to the previous (before) word.
* B - go to the previous (before) WORD.
* w - go to the next word.
* W - go to the next WORD.

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.


6.1 Display N lines after match
-A is the option which prints the specified N lines after the match as shown below.
Syntax:
grep -A <N> "string" FILENAME

$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

6.2 Display N lines before match
-B is the option which prints the specified N lines before the match.

Syntax:
grep -B <N> "string" FILENAME

$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD


6.3 Display N lines around match
-C is the option which prints the specified N lines before the match. In some occasion you might want the match to be appeared with the lines from both the side. This options shows N lines in both the side(before & after) of match.

$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD

例7 高亮显示
$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'

$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

例8 搜索所有文件
Searching in all files recursively using grep -r

$ grep -r "ramesh" *

例9 搜索非匹配内容
Invert match using grep -v
$ grep -v "go" demo_text
4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

例10 多个表达式非匹配
display the lines which does not matches all the given pattern.
Syntax:
grep -v -e "pattern" -e "pattern"

$ cat test-file.txt
a
b
c
d

$ grep -v -e "a" -e "b" -e "c" test-file.txt
d

例11 计数求和
Counting the number of matches using grep -c
Syntax:
grep -c "pattern" filename

$ grep -c "go" demo_text
6

When you want do find out how many lines that does not match the pattern

$ grep -v -c this demo_file
4
例12 只显示匹配的文件名称
$ grep -l this demo_*
demo_file
demo_file1

例13 显示匹配内容在行内的位置
Syntax:
grep -o -b "pattern" file

$ cat temp-file.txt
12345
12345

$ grep -o -b "3" temp-file.txt
2:3
8:3

例14 显示行号
$ grep -n "go" demo_text
5: * e - go to the end of the current word.
6: * E - go to the end of the current WORD.
7: * b - go to the previous (before) word.
8: * B - go to the previous (before) WORD.
9: * w - go to the next word.
10: * W - go to the next WORD.
分享到:
评论

相关推荐

    grep:一个行人,非常快的 grep 实用程序:一个类 Unix 的,非常快的 grep 实用程序,用于在任何类型的文件中查找字符串-matlab开发

    一个快速的 grep 实用程序,它在文件夹及其子文件夹中的所有或选定的任何类型的文件中查找字符串模式(如果设置了递归标志 -r)。 文件访问和字符串匹配由低级 i/o 和字符串例程优化。 递归算法 (-r) 不使用 ML 的 ...

    rustg:Rust 中的简单 grep 实用程序

    Rustg 是一个用 Rust 编写的简单 grep 实用程序。 安装 Rust。 编译 rustc rustg.rs 执行 grep 例子: 1.目标文件 rustg line example.txt 2. 多个目标文件 rustg line example.txt example1.txt 3. 管道 ls |...

    grep使用指南 青蛙书

    ### grep使用指南知识点详解 #### 一、简介 `grep` 是一款强大的文本搜索工具,其功能在于查找文件中与设定的模式匹配的行并输出。本书《grep使用指南》(又名“青蛙书”)由 John Bambenek 和 Agnieszka Klus ...

    Hadoop-MapReduce-Distributed-Grep:使用 Hadoop MapReduce 实现分布式 grep

    总之,“Hadoop-MapReduce-Distributed-Grep”项目是一个实用的例子,展示了如何利用 Hadoop 的 MapReduce 框架解决大数据场景下的搜索问题。通过学习这个项目,你可以深入了解 MapReduce 的工作原理,以及如何用 ...

    python正则表达式例子

    根据提供的文件信息,我们...通过以上几个例子可以看出,正则表达式在实际编程中非常实用,可以帮助我们快速定位和提取数据,提高程序的效率和可维护性。掌握正则表达式的规则和技巧对于开发人员来说是一项宝贵的技能。

    jQuery中$.grep() 过滤函数 数组过滤

    在JavaScript的世界里,jQuery库提供了一系列实用的工具函数,其中之一就是`$.grep()`。这个函数主要用于数组的过滤操作,能够帮助开发者根据特定条件筛选数组中的元素。本文将深入讲解jQuery中`$.grep()`函数的工作...

    神奇的perl例子perl例子100个

    "神奇的Perl例子100个"这个资源显然旨在提供一系列实践性的示例,帮助学习者掌握Perl语言的核心概念和实用技巧。以下是一些可能涵盖的知识点: 1. **变量和数据类型**:Perl支持多种数据类型,包括标量、数组、哈希...

    search:受用 nodejs 编写的“ack”启发的 grep-ish 搜索实用程序

    搜索用 nodejs 编写的受“ack”启发的 grep-ish 搜索实用程序。安装 $ npm install -g search用法Usage: search [options] &lt; query&gt; [path ...]Options: -h, --help output usage information -v, --version output ...

    几个shell编程的小例子

    在Shell编程中,这些都可以通过内置的命令和函数来实现,例如,使用`grep`查找特定文本,`sed`进行文本替换,或者`awk`进行复杂的行处理。 在学习和实践这些Shell编程小例子时,理解基本的语法结构、命令以及如何...

    bingrep:grep的一个小实用程序,用于在内存中转储指针和二进制数据,转储实时进程内存

    宾格grep的一个小实用程序,用于在内存转储或实时进程内存中获取指针和二进制数据。用法Exactly one of -f or -p has to be specified.Usage: 1) bingrep -f &lt;filename&gt; [pattern] 2) bingrep -p &lt;pid&gt; [-i ] ...

    实用指令linux

    在这个例子中,我们不仅监控`nohup.out`文件,还通过`grep`命令进一步过滤出包含字符串"M1 Notice Controller"的行。这对于分析具体的服务或组件的运行状态非常有用。 #### 总结 通过以上实例,我们可以看出`tail`...

    jquery数组过滤筛选方法grep()简介

    jQuery中的grep()方法是一个非常实用的工具,尤其在处理数组数据时,它允许你根据特定条件筛选出符合或者不符合要求的元素。grep()方法的主要作用是遍历数组,并基于提供的回调函数判断每个元素是否应该保留在结果...

    linux命令大全,以及例子

    在Linux操作系统中,掌握命令行工具是至关重要的,因为大多数高级操作和系统管理任务都是通过命令行接口(CLI)来完成的。...记住,Linux的学习是一个持续的过程,随着经验的积累,你会逐渐发现更多实用的命令和技巧。

    浅谈Linux grep与正则表达式

    在Linux系统中,grep是一个非常实用的命令行工具,用于在文件或标准输入中搜索符合特定模式的行。这个工具结合了正则表达式的力量,使得文本搜索变得极其强大和灵活。下面我们将深入探讨grep的基本用法和正则表达式...

    一个批量域名解析的Shell脚本例子

    标题中的“一个批量域名解析的Shell脚本例子”是指通过Shell脚本来实现对大量域名进行DNS解析的功能。在IT行业中,这种操作常用于网络监控、安全分析或数据收集等场景。下面将详细介绍这个主题。 首先,`...

    shellbash script基础入门及例子(附代码)持续更新

    本文将深入讲解Bash脚本的基础知识,并提供实用的例子和代码,帮助初学者快速入门。我们将涵盖以下几个关键知识点: 1. **变量**:在Bash中,变量用于存储数据。声明变量时不需要指定类型,只需给变量赋值即可,...

    Linux下ps命令的10个基本例子.docx

    Linux下的`ps`命令是系统管理员和...在实际使用中,还可以结合其他shell工具如`grep`、`awk`等进行更复杂的过滤和分析,以满足不同的监控需求。不断实践和探索`ps`命令的不同组合,将使你在Linux运维中更加得心应手。

    linux常用程序例子

    "Linux常用程序例子"这个主题涵盖了大量实用的命令行工具和应用程序,这些工具可以帮助用户在Linux环境中完成各种任务。在这个压缩包中,我们有三个子文件,分别是"linux"、"value"和"sun",它们可能代表不同的示例...

    掌握:使用clojure.spec regexes的Grep Clojure代码

    总结,`clojure.spec` 不仅仅是一个验证工具,其结合正则表达式可以为Clojure开发者提供更丰富的数据处理和分析手段,实现诸如Grep这样的实用功能。理解和熟练运用 `clojure.spec` 对提升Clojure代码质量和维护性...

    adb实用抓取日志到电脑

    文中给出的例子为`adb pull sdcard/lock.xml`。 #### 总结 通过ADB工具,我们可以轻松地在Android设备和电脑之间传输文件,以及抓取设备日志以便进行更深入的调试和分析。掌握了这些基本操作之后,可以极大地提高...

Global site tag (gtag.js) - Google Analytics