zoukankan      html  css  js  c++  java
  • linux shell 脚本攻略学习18--grep命令详解

    grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是unix/linux中用于文本搜索的大师级的工具。它能够接受正则表达式和通配符

    首先,输入grep --help查看帮助信息:

    amosli@amosli-pc:~$ grep --help
    Usage: grep [OPTION]... PATTERN [FILE]...
    Search for PATTERN in each FILE or standard input.
    PATTERN is, by default, a basic regular expression (BRE).
    Example: grep -i 'hello world' menu.h main.c
    
    Regexp selection and interpretation:
      -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
      -F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
      -G, --basic-regexp        PATTERN is a basic regular expression (BRE)
      -P, --perl-regexp         PATTERN is a Perl regular expression
      -e, --regexp=PATTERN      use PATTERN for matching
      -f, --file=FILE           obtain PATTERN from FILE
      -i, --ignore-case         ignore case distinctions
      -w, --word-regexp         force PATTERN to match only whole words
      -x, --line-regexp         force PATTERN to match only whole lines
      -z, --null-data           a data line ends in 0 byte, not newline
    
    Miscellaneous:
      -s, --no-messages         suppress error messages
      -v, --invert-match        select non-matching lines
      -V, --version             print version information and exit
          --help                display this help and exit
          --mmap                ignored for backwards compatibility
    
    Output control:
      -m, --max-count=NUM       stop after NUM matches
      -b, --byte-offset         print the byte offset with output lines
      -n, --line-number         print line number with output lines
          --line-buffered       flush output on every line
      -H, --with-filename       print the file name for each match
      -h, --no-filename         suppress the file name prefix on output
          --label=LABEL         use LABEL as the standard input file name prefix
      -o, --only-matching       show only the part of a line matching PATTERN
      -q, --quiet, --silent     suppress all normal output
          --binary-files=TYPE   assume that binary files are TYPE;
                                TYPE is `binary', `text', or `without-match'
      -a, --text                equivalent to --binary-files=text
      -I                        equivalent to --binary-files=without-match
      -d, --directories=ACTION  how to handle directories;
                                ACTION is `read', `recurse', or `skip'
      -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                                ACTION is `read' or `skip'
      -R, -r, --recursive       equivalent to --directories=recurse
          --include=FILE_PATTERN  search only files that match FILE_PATTERN
          --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
          --exclude-from=FILE   skip files matching any file pattern from FILE
          --exclude-dir=PATTERN  directories that match PATTERN will be skipped.
      -L, --files-without-match  print only names of FILEs containing no match
      -l, --files-with-matches  print only names of FILEs containing matches
      -c, --count               print only a count of matching lines per FILE
      -T, --initial-tab         make tabs line up (if needed)
      -Z, --null                print 0 byte after FILE name
    
    Context control:
      -B, --before-context=NUM  print NUM lines of leading context
      -A, --after-context=NUM   print NUM lines of trailing context
      -C, --context=NUM         print NUM lines of output context
      -NUM                      same as --context=NUM
          --color[=WHEN],
          --colour[=WHEN]       use markers to highlight the matching strings;
                                WHEN is `always', `never', or `auto'
      -U, --binary              do not strip CR characters at EOL (MSDOS)
      -u, --unix-byte-offsets   report offsets as if CRs were not there (MSDOS)
    
    `egrep' means `grep -E'.  `fgrep' means `grep -F'.
    Direct invocation as either `egrep' or `fgrep' is deprecated.
    With no FILE, or when FILE is -, read standard input.  If less than two FILEs
    are given, assume -h.  Exit status is 0 if any line was selected, 1 otherwise;
    if any error occurs and -q was not given, the exit status is 2.

    语法格式:

         grep [OPTIONS] PATTERN [FILE...]
           grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

    参数-实例:

    test.txt

    amosli@amosli-pc:~/learn/grep$ cat test.txt 
    hi,amos
    this is linux world 
    grep command is powerful!

    1.在文件中搜索一个单词

    iamosli@amosli-pc:~/learn/grep$ grep 'hi' test.txt 
    hi,amos
    this is linux world 

    2.从标准输入中搜索一个单词

    amosli@amosli-pc:~/learn/grep$ echo this is a word  | grep hi
    this is a word

    也可以加上参数-e,表示根据模式进行匹配,下面部分也将会对-e参数进行介绍

    amosli@amosli-pc:~/learn/grep$ echo  this is a word  | grep -e hi
    this is a word

    3.-E参数,使用正则表达式进行搜索关键字

    amosli@amosli-pc:~/learn/grep$ echo  this is a word  | grep -E [a-z]+d
    this is a word

    -E参数英文提示信息:

      -E, --extended-regexp     PATTERN is an extended regular expression (ERE)

    意思是使用扩展(extended)的正则表达式.在文章开始的提示信息中有这么一段"egrep' means `grep -E'. ",所以也可以直接使用egrep命令进行搜索

    amosli@amosli-pc:~/learn/grep$ echo  this is a word  | egrep "[a-z]+d"
    this is a word

    4.-o参数,只输出匹配到的文本部分,没有匹配到的不显示

    amosli@amosli-pc:~/learn/grep$ echo this a line. | grep -o -E "[a-z]+."
    line.
    #或者
    amosli@amosli
    -pc:~/learn/grep$ echo this a line. | egrep -o "[a-z]+." line.

    英文提示信息:

    -o, --only-matching       show only the part of a line matching PATTERN

    5.--color参数,重点标记出匹配到的文本

    amosli@amosli-pc:~/learn/grep$ echo this a line. | egrep -o "[a-z]+." --color=auto
    line.

    除了auto之外,还可以选择never和always,这个是表示什么时候重点标记。

    英文提示信息:

      --colour[=WHEN]       use markers to highlight the matching strings;
                                WHEN is `always', `never', or `auto'

    6.-v参数,打印除了匹配到的文本以外的内容

    常用格式:

    grep -v match_pattern file

    例:

    amosli@amosli-pc:~/learn/grep$ grep -v 'hi,amos' test.txt 
    this is linux world 
    grep command is powerful!

    英文提示信息:

      -v, --invert-match        select non-matching lines

    7.-c参数,统计关键词出现的行数

    amosli@amosli-pc:~/learn/grep$ grep -c hi test.txt 
    2

    英文提示信息:

      -c, --count               print only a count of matching lines per FILE

    这里grep只是统计出现的行数,而不是统计关键词出现的次数

    如:

    amosli@amosli-pc:~/learn/grep$ echo -e  "1 2 3
    helo
     4 5" | grep -c "[0-9]"
    2

    这里有5个数字,但只有3行,其中有两行中出现数字,所以显示的次数为2

    如何统计文件中关键词出现的次数?

    可以使用wc 命令进行二次统计,如下:

    amosli@amosli-pc:~/learn/grep$ echo -e  "1 2 3
    helo
     4 5" | grep "[0-9]" | wc -w
    5

    8.-n参数,打印关键词所在行数

    amosli@amosli-pc:~/learn/grep$ grep  -n 'hi' test.txt 
    1:hi,amos
    2:this is linux world 

    在第1行显示了关键词所在的行,极大的方便了阅读。

    英文提示信息:

      -n, --line-number         print line number with output lines
          --line-buffered       flush output on every line

    9.对多个文件进行搜索

    test2.txt

    amosli@amosli-pc:~/learn/grep$ cat test2.txt 
    hi,amos
    this is linux world 
    Welcome you!

    对test.txt 和test2.txt进行批量查询:

    amosli@amosli-pc:~/learn/grep$ grep -n 'linux' test.txt  test2.txt 
    test.txt:2:this is linux world 
    test2.txt:2:this is linux world 

    10.-l参数,对多个文件进行搜索,但只显示文件名

    amosli@amosli-pc:~/learn/grep$ grep -l 'linux' test.txt test2.txt 
    test.txt
    test2.txt

    英文提示信息: 

      -l, --files-with-matches  print only names of FILEs containing matches

      11.-i 参数,忽略大小写进行搜索

    amosli@amosli-pc:~/learn/grep$ echo "THIS IS test" | grep -i "th"
    THIS IS test

    英文提示信息: 

      -i, --ignore-case         ignore case distinctions

    12.-R参数,递归进行搜索

    amosli@amosli-pc:~/learn/grep$ cd ..
    amosli@amosli-pc:~/learn$ grep 'hi,amos' . -R
    ./grep/test.txt:hi,amos
    ./grep/test2.txt:hi,amos

    英文提示信息: 

      -R, -r, --recursive       equivalent to --directories=recurse
          --include=FILE_PATTERN  search only files that match FILE_PATTERN
          --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
          --exclude-from=FILE   skip files matching any file pattern from FILE
          --exclude-dir=PATTERN  directories that match PATTERN will be skipped.

    13.-e参数,用grep匹配多个样式,即多个条件进行查询

    amosli@amosli-pc:~/learn/grep$ grep -e "hi" -e "linux"  -e "grep" test.txt 
    hi,amos
    this is linux world 
    grep command is powerful!

    英文提示信息:

      -e, --regexp=PATTERN      use PATTERN for matching

    还有一种方法可以进行样式匹配,即把所要匹配的关键词用一个文件保存起来,然后进行检索,请看下一个参数-f

    14.-f参数,用grep匹配多个样式,即多个条件进行查询--方法2

    将关键词保存到f.txt中:

    amosli@amosli-pc:~/learn/grep$ cat  f.txt
    hi
    linux
    grep

    开始进行匹配:

    amosli@amosli-pc:~/learn/grep$ grep -f f.txt test.txt 
    hi,amos
    this is linux world 
    grep command is powerful!

    结果跟-e参数一样,但在大文件多关键词的条件下,-f参数绝对提高了查询的效率。

    英文提示信息:

      -f, --file=FILE           obtain PATTERN from FILE

     15.在grep搜索中包括或排除指定的文件(--include参数和--exclude参数)

    amosli@amosli-pc:~/learn/grep$ cat test.html 
    hi,amos
    this is linux world 
    grep command is powerful!

    开始检索只包括.html的文件:

    amosli@amosli-pc:~/learn/grep$ grep "hi,amos" . -r --include *.html -n
    ./test.html:1:hi,amos

    开始检索不包括.html的文件:

    amosli@amosli-pc:~/learn/grep$ grep "hi,amos" . -r --exclude *.html -n
    ./test.txt:1:hi,amos
    ./test2.txt:1:hi,amos

    英文提示信息:

     -r, --recursive       equivalent to --directories=recurse
          --include=FILE_PATTERN  search only files that match FILE_PATTERN
          --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
          --exclude-from=FILE   skip files matching any file pattern from FILE
          --exclude-dir=PATTERN  directories that match PATTERN will be skipped.

     16.-A,-B,-C ,打印出关键词前、后的行

    amosli@amosli-pc:~/learn/grep$ echo -e "1
    2
    3
    4
    5
    6
    7
    8
    9
    10" | grep '3'
    3

    -A,打印出关键词之后的5行,after

    amosli@amosli-pc:~/learn/grep$ echo -e "1
    2
    3
    4
    5
    6
    7
    8
    9
    10" | grep '3' -A 5
    3
    4
    5
    6
    7
    8

    -B,打印出关键词之前的2行,before

    amosli@amosli-pc:~/learn/grep$ echo -e "1
    2
    3
    4
    5
    6
    7
    8
    9
    10" | grep '3' -B 2
    1
    2
    3

    -C,打印出关键词前后2行

    amosli@amosli-pc:~/learn/grep$ echo -e "1
    2
    3
    4
    5
    6
    7
    8
    9
    10" | grep '3' -C 2
    1
    2
    3
    4
    5

    英文提示信息:

      -B, --before-context=NUM  print NUM lines of leading context
      -A, --after-context=NUM   print NUM lines of trailing context
      -C, --context=NUM         print NUM lines of output context

    17.-q 参数,静默模式输出

    在静默模式中,grep 命令不会向标准输出打印任何输出,它仅是运行命令,然后根据命令执行成功与否返回退出状态,没有找到值则返回状态为1,找到则返回0.

    没有找到关键词:

    amosli@amosli-pc:~/learn/grep$ grep -q 'abc' test.txt 
    amosli@amosli-pc:~/learn/grep$ $?
    1: command not found

    找到关键词:

    amosli@amosli-pc:~/learn/grep$ grep -q 'amos' test.txt 
    amosli@amosli-pc:~/learn/grep$ $?
    0: command not found

    英文提示信息:

      -q, --quiet, --silent     suppress all normal output
          --binary-files=TYPE   assume that binary files are TYPE;
                                TYPE is `binary', `text', or `without-match'

    18、其他参数,可输入man grep查看grep命令手册

  • 相关阅读:
    期待Eclipse3.4
    Winforms中使用系统托盘图标
    Eclipse下的Struts2开发插件
    彩色 夺冠
    网络&系统
    A Famous Music Composer
    Quick Brown Fox
    解密QQ号——队列
    谁先倒
    bootstrap Table从零开始
  • 原文地址:https://www.cnblogs.com/amosli/p/3496988.html
Copyright © 2011-2022 走看看