grep命令

0x00 前言

       前几天微博推送关于grep原作者Mike Haertel的采访视频,突然觉得grep是一个很强大的工具,以前在使用Ubuntu的工程中,一直使用find,但是我最近在使用grep中,发现借助–help选项并不能很好的驾驭这个工具,我一直可以通过原命令提供Usage来学习某一个命令总是一项必要的能力,尤其在linux系统下编程,因为我已经懒的不想花费打开浏览器的时间。

0x01 grep –help

       习惯–help使用某一个工具,会使使用linux系统更加习惯,更加轻松。下来截取来自我的ubuntu系统上的grep使用说明:

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
//-i 就是上面的[OPTION]
//'hello world'就是PATTERN,也可以是使用"",其表达相同
//menu.h main.c就是上面的[file]
Regexp selection and interpretation:
  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
  -F, --fixed-strings       PATTERN is a set of newline-separated 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             display version information and exit
      --help                display this help text and exit

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, --recursive           like --directories=recurse
  -R, --dereference-recursive  likewise, but follow all symlinks
      --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/Windows)
  -u, --unix-byte-offsets   report offsets as if CRs were not there
                            (MSDOS/Windows)

'egrep' means 'grep -E'.  'fgrep' means 'grep -F'.
Direct invocation as either 'egrep' or 'fgrep' is deprecated.
When FILE is -, read standard input.  With no FILE, read . if a command-line
-r is given, - otherwise.  If fewer than two FILEs are given, assume -h.
Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.

       首先,需要说明的是上面的[option]在输入中的位置没有严格规定,可以放在紧挨着grep的后边,或者放到输入一长串命令的最后,其实现的功能基本相同。接下来挑选出几个加以说明:

  • -v, –invert-match select non-matching lines
           在Miscellaneous中其他两个选项比较好理解,比较正常。这个选项就是使用grep打印出剩下不匹配的行,大致就是除去有匹配字符行以后剩下的。

  • -e, –regexp=PATTERN use PATTERN for matching
    其实观看Usage: grep [OPTION]… PATTERN [FILE]…太模糊了,[OPTION]后边又加了三个…实在让人捉摸不透,如果我没有理解错的话PATTERN就是我想要寻找的字符,但是不能连续出现:grep [option] “xxx” “yyy”这种形式,按照上面的Usage,grep将会把第二个”yyy”当做[FILE]来使用,那么肯定会出错。首先来看看-e

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

       -e的用法与其他的可能有所不同,需要多次出现,其他的选项的出现的次数基本都是一次,简而言之,就是一个-e后边跟一个PATTERN,例如,可以写成一下形式:

grep -e "hello" -e "world" hello.c hello.h
  • -n, –line-number print line number with output lines
    打印出匹配的行在原原文件中的行数。

  • -r, –recursive like –directories=recurse
    递归遍历[FILE],当然前提是FILE是一个文件夹的名字(包括可以使用..或者.),其位置可在前面也可在最后。

  • –color[=WHEN]
    可放在命令行的最后,用于高亮显示搜索的PATTERN在输出的文本中,可以使用如下:

grep -r "hello" . --color=auto (递归遍历当前文件下所有文件中的hello,并打印出匹配的行,并高亮显示hello)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值