zoukankan      html  css  js  c++  java
  • linux grep命令详解

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。

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

    Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的扩展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。linux使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。

    文件字符搜索

    grep能够快速的对文件进行搜索,命令和参数都比较好理解:

    grep [-acinv] [--color=auto] '搜寻字符串' filename
    

    选项与参数:

    • -a :将 binary 文件以 text 文件的方式搜寻数据
    • -c :计算找到 '搜寻字符串' 的次数
    • -i :忽略大小写的不同,所以大小写视为相同
    • -n :顺便输出行号
    • -v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!
    • --color=auto :可以将找到的关键词部分加上颜色的显示喔!

    为了方便使用这个命令,先建立一个用来测试的文件,文件的内容如下:

    The Wrong Man
    1956 film by Alfred Hitchcock
    The Wrong Man is typical of most of his films. In The Wrong Man he appears only in silhouette, just before the credits at the beginning of the film, where he tells a darkened studio that the story is true.
    

    搜索含有Man的句子:

    root@ubuntu:~# grep -n Man  grep_text.txt 
    1:The Wrong Man
    3:The Wrong Man is typical of most of his films. In The Wrong Man he appears only in silhouette, just before the credits at the beginning of the film, where he tells a darkened studio that the story is true.
    

    其中 -n 为显示出有搜索到的句子在第几行。其他的参数同样可以使用如:

    root@ubuntu:~# grep -n -i -v Man  grep_text.txt 
    2:1956 film by Alfred Hitchcock 
    4:
    

    可以对照上面对参数的说明,-n -i -v ,其中-v是显示搜索不到的行数。

    作为命令的管道进行搜索

    在linux的命令中,有些命令显示了大量的内容,不容易查看可以通过grep过滤,得到想要的结果,如命令ps ax 显示了大量的,使用grep过滤相关进程。

    	root@ubuntu:~# ps ax | grep -n python
    149:  5574 pts/0    Tl     0:00 /usr/bin/python /usr/local/bin/scrapy crawl iiSpider
    150:  5589 pts/0    T      0:00 /usr/bin/python /usr/local/bin/scrapy crawl ccSpider
    151:  5714 pts/0    T      0:00 /usr/bin/python /usr/local/bin/scrapy crawl ddSpider
    

    正则匹配

    grep的正则跟其他语言的正则表达式有一些不同,认识到grep里的规则,就能容易写出grep的过滤条件。

    RE(正则表达式)

    • ^ 匹配正则表达式的开始行
    • $ 匹配正则表达式的结束行
    • < 从匹配正则表达式的行开始
    • > 到匹配正则表达式的行结束
    • [ ] 单个字符;如[A] 即A符合要求
    • [ - ] 范围 ;如[A-Z]即A,B,C一直到Z都符合要求
    • . 所有的单个字符
      • 所有字符,长度可以为0
    • 忽略正则表达式中特殊字符的原有含义

    查看带数字的行:

    root@ubuntu:~# grep -n [0-9]  grep_text.txt 
    2:1956 film by Alfred Hitchcock 
    

    查看The开头的行

    root@ubuntu:~# grep -n ^The  grep_text.txt 
    1:The Wrong Man
    3:The Wrong Man is typical of most of his films. In The Wrong Man he appears only in silhouette, just before the credits at the beginning of the film, where he tells a darkened studio that the story is true.
    

    更多教程:阿猫学编程

  • 相关阅读:
    十步完全理解 SQL
    Oracle VM Virtual
    Pycharm 使用
    Open Yale course:Listening to Music
    SQL 必知必会
    安装 SQL server 2008 R2
    Ubuntu安装mysql之后,编译找不到头文件
    core dump文件的生成
    Linux静态库与动态库制作过程
    GEC6818连接Ubuntu,下载程序至开发板
  • 原文地址:https://www.cnblogs.com/bugingcode/p/8287951.html
Copyright © 2011-2022 走看看