grep的用法首先创建我们练习grep命令时需要用到的demo文件demo_file。

  1. $ cat demo_file
  2. THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
  3. this line is the 1st lower case line in this file.
  4. This Line Has All Its First Character Of The Word With Upper Case.
  5. Two lines above this line is empty.
  6. And this is the last line.

1.从单个文件中搜索指定的字串

grep的基础用法是如下例的从指定的文件中搜索特定的字串。

语法:

  1. grep "literal_string" filename
  1. $ grep "this" demo_file
  2. this line is the 1st lower case line in this file.
  3. Two lines above this line is empty.
  4. And this is the last line.

2. 在多个文件中检索指定的字串语法:

  1. grep "string" FILE_PATTERN

先拷贝demo_file为demo_file1。

grep的结果在符合条件的行前将包括文件名。

当文件名包含元字符时,linux shell会将匹配的所有文件作为输入到grep中去。

  1. $ cp demo_file demo_file1
  2. $ grep "this" demo_*
  3. demo_file:this line is the 1st lower case line in this file.
  4. demo_file:Two lines above this line is empty.
  5. demo_file:And this is the last line.
  6. demo_file1:this line is the 1st lower case line in this file.
  7. demo_file1:Two lines above this line is empty.
  8. demo_file1:And this is the last line.

3. 用 grep -i 进行大小写无关的搜索语法:

  1. grep -i "string" FILE

也是一个基本用法,

对搜索的字串忽略大小写,因此下例中匹配“the”, “THE” and “The”。

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

4. 使用用正则表达式语法:

  1. grep "REGEX" filename

如果你能有效地利用正则表达式,这是个很有用的特点。

在下面的例子中,搜索全部以“lines”开始以“empty”结束的字串,

如搜索“lines[之间任意字]empty” ,并且忽略大小写。

  1. $ grep -i "lines.*empty" demo_file
  2. Two lines above this line is empty.

正则表达式遵循的几个重复的操作

  • ? 最多匹配一次
  • * 匹配零次或者任意多次
  • + 匹配一次以上
  • {n} 匹配n次
  • {n,} 最少匹配n次
  • {,m} 最多匹配m次
  • {n,m} 匹配n到m次

5. 用grep -w搜索整个词,而不是词中的部分字串

使用-w选项搜索一个单词,并且避免搜索到词中的部分字串。

下例搜索"is"。

如果不加-w选项,将显示“is”, “his”, “this” 等所有包含“is”的行。

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

下例使用了-w选项,

请注意结果中不包含

“This Line Has All Its First Character Of The Word With Upper Case”,

虽然 “This”中包含“is”。

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

6. 使用grep -A, -B and -C显示之前、之后、前后的几行

当使用grep搜索大文件时,

显示匹配行附近的多行数据是一个很有用的功能。

创建如下文件

  1. $ cat demo_text
  2. 4. Vim Word Navigation
  3. You may want to do several navigation in relation to the words, such as:
  4. * e - go to the end of the current word.
  5. * E - go to the end of the current WORD.
  6. * b - go to the previous (before) word.
  7. * B - go to the previous (before) WORD.
  8. * w - go to the next word.
  9. * W - go to the next WORD.
  10. WORD - WORD consists of a sequence of non-blank characters, separated with white space.
  11. word - word consists of a sequence of letters, digits and underscores.
  12. Example to show the difference between WORD and word
  13. * 192.168.1.1 - single WORD
  14. * 192.168.1.1 - seven words.

6.1 显示匹配行之后的N行

-A

语法:

  1. grep -A "string" FILENAME

下例显示匹配行和之后的3行数据

  1. $ grep -A 3 -i "example" demo_text
  2. Example to show the difference between WORD and word
  3. * 192.168.1.1 - single WORD
  4. * 192.168.1.1 - seven words.

6.2显示匹配行之前的N行

-B

语法:

  1. grep -B "string" FILENAME

下例显示匹配行和之前的2行数据

  1. $ grep -B 2 "single WORD" demo_text
  2. Example to show the difference between WORD and word
  3. * 192.168.1.1 - single WORD

6.3显示匹配行前后的N行

-C

显示之前的n行,之后的n行数据.

  1. $ grep -C 2 "Example" demo_text
  2. word - word consists of a sequence of letters, digits and underscores.
  3. Example to show the difference between WORD and word
  4. * 192.168.1.1 - single WORD

7.通过GREP_OPTIONS高亮显示搜索的字串

如果你希望搜索的字串高亮显示在结果中,可以试用以下的办法。

通过修改GREP_OPTIONS对搜索字串高亮显示。

  1. $ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
  2. $ grep this demo_file
  3. this line is the 1st lower case line in this file.
  4. Two lines above this line is empty.
  5. And this is the last line.

8. 用grep -r递归搜索全部的文件

如果想查找当前目前以及其子目录的全部文件时,

可以使用 -r 选项。

如下例

  1. $ grep -r "ramesh" *

9. 使用grep -v进行不匹配

可以使用-v选项显示不匹配搜索字串的行。

下例显示demo_text文件中不包含“go”的行

  1. $ grep -v "go" demo_text
  2. 4. Vim Word Navigation
  3. You may want to do several navigation in relation to the words, such as:
  4. WORD - WORD consists of a sequence of non-blank characters, separated with white space.
  5. word - word consists of a sequence of letters, digits and underscores.
  6. Example to show the difference between WORD and word
  7. * 192.168.1.1 - single WORD
  8. * 192.168.1.1 - seven words.

10. 显示不匹配全部模式的行语法:

  1. grep -v -e "pattern" -e "pattern"

创建如下例子文件

  1. $ cat test-file.txt
  2. a
  3. b
  4. c
  5. d
  6. $ grep -v -e "a" -e "b" -e "c" test-file.txt
  7. d

11.用grep -c 统计匹配的行数语法:

  1. grep -c "pattern" filename
  2. $ grep -c "go" demo_text
  3. 6

统计不匹配的行数

  1. $ grep -v -c this demo_file
  2. 4

12. 用grep -l 只显示文件名

  1. $ grep -l this demo_*
  2. demo_file
  3. demo_file1

13. 只显示匹配的字串

缺省显示匹配字串的所在行,

可以使用-o选项只显示匹配的字串。

这项功能当使用正则表达式时比较有用处。

  1. $ grep -o "is.*line" demo_file
  2. is line is the 1st lower case line
  3. is line
  4. is is the last line

14. 显示匹配的位置语法:

  1. grep -o -b "pattern" file
  2. $ cat temp-file.txt
  3. 12345
  4. 12345
  5. $ grep -o -b "3" temp-file.txt
  6. 0:3
  7. 6:3

注意: 以上输出显示的不是行内的位置,而是整个文件中的字节byte位置

15. 用 grep -n 在输出时显示行号

行号从1开始

  1. $ grep -n "go" demo_text
  2. 5: * e - go to the end of the current word.
  3. 6: * E - go to the end of the current WORD.
  4. 7: * b - go to the previous (before) word.
  5. 8: * B - go to the previous (before) WORD.
  6. 9: * w - go to the next word.
  7. 10: * W - go to the next WORD.

原文地址:http://www.cnblogs.com/xuxm2007/archive/2011/01/10/1932288.html

grep的用法的更多相关文章

  1. grep常用用法

    grep常用用法 [root@www ~]# grep [-acinv] [--color=auto] '搜寻字符串' filename 选项与参数: -a :将 binary 文件以 text 文件 ...

  2. linux grep的用法

    linux grep的用法<pre>[root@iZ23uewresmZ ~]# cat /home/ceshis.txtb124230 b034325 a081016 m7187998 ...

  3. 文本三剑客之grep的用法

    第1章         正则表达式 1.1    正则表达式的介绍               正则是用来过滤文件内容               为处理大量文本|字符串而定义的一套规则和方法.    ...

  4. linux中grep的用法

    http://www.9usb.net/200902/linux-grep.html http://blog.51yip.com/linux/1008.html http://blog.csdn.ne ...

  5. grep精确匹配搜索某个单词的用法 (附: grep高效用法小结))

    grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...

  6. Ubuntu下,grep的用法

    grep(Global search Regular Expression and Print out the line)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.U ...

  7. grep命令用法

    linux中grep命令的用法 作为linux中最为常用的三大文本(awk,sed,grep)处理工具之一,掌握好其用法是很有必要的. 首先谈一下grep命令的常用格式为:grep  [选项]  ”模 ...

  8. grep的用法,小技巧,模板中含有\t时:grep -P "^\t" file

    linux中grep和find的用法区别 本文章详细的介绍了关于在linux中的grep和find两个命令的用法介绍,以及后面总结了它们两年用法区别哦. 先我们来介绍一下关于grep用法和一些小注意事 ...

  9. <三剑客> 老三:grep命令用法

    grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...

随机推荐

  1. Sumdiv(快速幂+约数和)

    Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16244 Accepted: 4044 Description C ...

  2. Wormholes 分类: POJ 2015-07-14 20:21 21人阅读 评论(0) 收藏

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35235   Accepted: 12861 Descr ...

  3. HDU(3790),最短路二级标准

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    ...

  4. MySQL常用操作总结

    MySQL常用操作 前提条件:已安装MySQL. 学习目标:用一条sql语句写出A和B的剩余数量 AA表 BB表 以上为一道面试题,接下来由这道面试题来回顾一些数据库的基本操作. 登录MySQL su ...

  5. CSU 1114 平方根大搜索 java大数

    1114: 平方根大搜索 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 49  Solved: 23[Submit][Status][Web Board ...

  6. 无边框窗体、用户控件、Timer控件

    一.无边框窗体1 最大化.最小化以及关闭按钮制作实际上就是更换点击前.指向时.点击时的图片 (1)将图片放在该文件夹的Debug中,获取图片的路径Application.StartupPath + & ...

  7. CSS3那些不为人知的高级属性

    尽管现代浏览器已经支持了众多的CSS3属性,但是大部分设计师和开发人员貌似依然在关注于一些很“主流”的属性,如border-radius.box-shadow或者transform等.它们有良好的文档 ...

  8. shell中sed用法

    简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ...

  9. Struts2的标签库(三)——控制标签

    Struts2的标签库(三) --控制标签 1.if/elseif/else标签 用于分支控制,取代JSP中的if语句,根据一个boolean(test属性的值)值判断是否进行下一步运算或者输出等. ...

  10. 如何在Ubuntu/CentOS上安装Linux内核4.0

    大家好,今天我们学习一下如何从Elrepo或者源代码来安装最新的Linux内核4.0.代号为‘Hurr durr I'm a sheep’的Linux内核4.0是目前为止最新的主干内核.它是稳定版3. ...