linux命令系列 grep

grep, egrep, fgrep - print lines matching a pattern

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

linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来,grep全称是Global Regular Expression Print

1. 常用选项:

  -E, --extended-regexp: Interpret PATTERN as an extended regular expression.   # 开启扩展(Extend)的正则表达式

  -i, --ignore-case: Ignore case distinctions in both the PATTERN and the input files.   # 忽略大小写

  -v, --invert-match: Invert the sense of matching, to select non-matching lines.  # 反过来,只打印没有匹配的,而匹配的反而不打印

       -n, --line-number: Prefix each line of output with the 1-based line number within its input file.  # 显示行号

  -w, --word-regexp  # 被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker

    Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word

    constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the
    underscore.

  -c, --count: Suppress normal output; instead print a count of matching lines for each input file.  # 显示总共有多少行被匹配到了,而不是显示被匹配到的内容,注意如果同时使用-cv选项是显示有多少行没有被匹配到。

  -o, --only-matching: Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.  # 只显示被模式匹配到的字符串。

  -A NUM, --after-context=NUM: Print NUM lines of trailing context after matching lines.   # 显示匹配到的字符串所在的行及其后NUM行

  -B NUM, --before-context=NUM: Print NUM lines of leading context before matching lines.  # 显示匹配到的字符串所在的行及其前NUM行

  -C NUM, -NUM, --context=NUM: Print NUM lines of output context.  # 显示匹配到的字符串所在的行及其前后各NUM行

2. 模式部分:

  基本正则表达式

    匹配字符

      .  : 任意一个字符

      [abc] : 表示匹配[abc]中任意一个字符

      [a-zA-Z] : 匹配a-z或A-Z之间任意一个字符

      [^123] : 匹配123之外的任意一个字符

      

猜你喜欢

转载自www.cnblogs.com/z-joshua/p/10043357.html
今日推荐