[笔记]三大命令应用之-----grep

作者:Easiny

 

今天错过了上午的嵌入式课,可惜可惜了。不过随后去图书馆补了下Unix的命令,

UNIX Shell 实例精讲(第二版)

关于grep sed awk的三个家伙讲的很详细呢。把Man 翻译下,顺便做作笔记。

三个命令好比三个火枪手,个个都很厉害呢。呵呵。

 

前提:正则表达式 && 元字符&& 对shell 的运行机制有较好的了解

 

此笔记仅是在Man的帮助下加上笔记,辅助理解grep的应用。以后忘记的时候可以直接man,联想记忆喽

 

GREP(1)                                                                         User Commands                                                                        GREP(1)

NAME
       grep, egrep, fgrep, rgrep - print lines matching a pattern
     //grep 家族,书上倒是没有提到 rgrep .
SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

DESCRIPTION
       grep  searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to
       the given PATTERN.  By default, grep prints the matching lines.

       In addition, three variant programs egrep, fgrep and rgrep are available.  egrep is the same as grep -E.  fgrep is the  same  as  grep -F.   rgrep  is  the  same  as
       grep -r.

  Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications

that rely on them to run unmodified.

OPTIONS
   Generic[一般的] Program Information
       --help Print a usage message briefly summarizing these command-line options and the bug-reporting address, then exit.

       -V, --version
              Print the version number of grep to the standard output stream.  This version number should be included in all bug reports (see below).

   Matcher Selection   //大概是说对PATTERN的不同选项有不同的解释(目前没用过)
       -E, --extended-regexp
              Interpret PATTERN as an extended regular expression (ERE, see below).  (-E is specified by POSIX.)

       -F, --fixed-strings
              Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.  (-F is specified by POSIX.)

       -G, --basic-regexp
              Interpret PATTERN as a basic regular expression (BRE, see below).  This is the default.

       -P, --perl-regexp
              Interpret PATTERN as a Perl regular expression.  This is highly experimental and grep -P may warn of unimplemented features.

   Matching Control
       -e PATTERN, --regexp=PATTERN
              Use PATTERN as the pattern.  This is useful to protect patterns beginning with hyphen-minus (-).  (-e is specified by POSIX.)

       -f FILE, --file=FILE
              Obtain patterns from FILE, one per line.  //从文件中获得pattern ,每行一个

              The empty file contains zero patterns, and therefore matches nothing.  (-f is specified by POSIX.)

       -i, --ignore-case //忽略大小写
              Ignore case distinctions in both the PATTERN and the input files.  (-i is specified by POSIX.)
        -v, --invert-match//相反匹配,匹配和pattern 相反的内容
              Invert the sense of matching, to select non-matching lines.  (-v is specified by POSIX.)

       -w, --word-regexp //全字匹配,把表达式当作字来匹配,字或者在开头或者在结尾
              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.

       -x, --line-regexp//全行匹配(代定)
              Select only those matches that exactly match the whole line.  (-x is specified by POSIX.)

       -y     Obsolete synonym[同义的] for -i. //忽略大小写

 

 General Output Control
       -c, --count  //只打印匹配的行数,和-v结合使用打印不匹配的行数
              Suppress [禁止] normal  output;  instead  print  a  count of matching lines for each input file.  With the -v, --invert-match option (see below), count non-matching
              lines.  (-c is specified by POSIX.)

       --color[=WHEN], --colour[=WHEN]
              Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators  (for  fields  and  groups  of
              context  lines)  with  escape  sequences  to  display  them  in  color  on the terminal.  The colors are defined by the environment variable GREP_COLORS.  The
              deprecated environment variable GREP_COLOR is still supported, but its setting does not have priority.  WHEN is never, always, or auto.

       -L, --files-without-match
              Suppress normal output; instead print the name of each input file from which no output would normally have been printed.  The scanning will stop on the  first
              match.

       -l, --files-with-matches
              Suppress  normal  output;  instead  print the name of each input file from which output would normally have been printed.  The scanning will stop on the first
              match.  (-l is specified by POSIX.)

       -m NUM, --max-count=NUM
              Stop reading a file after NUM matching lines.  If the input is standard input from a regular file, and NUM matching lines are output, grep  ensures  that  the
              standard  input  is  positioned  to  just  after  the last matching line before exiting, regardless of the presence of trailing context lines.  This enables a
              calling process to resume a search.  When grep stops after NUM matching lines, it outputs any trailing context lines.  When the -c or --count option  is  also
              used, grep does not output a count greater than NUM.  When the -v or --invert-match option is also used, grep stops after outputting NUM non-matching lines.

       -o, --only-matching
              Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

       -q, --quiet, --silent
              Quiet;  do not write anything to standard output.  Exit immediately with zero status if any match is found, even if an error was detected.  Also see the -s or
              --no-messages option.  (-q is specified by POSIX.)

       -s, --no-messages
              Suppress error messages about nonexistent or unreadable files.  Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX, because  it
              lacked -q and its -s option behaved like GNU grep's -q option.  USG-style grep also lacked -q but its -s option behaved like GNU grep.  Portable shell scripts
              should avoid both -q and -s and should redirect standard and error output to /dev/null instead.  (-s is specified by POSIX.)

Output Line Prefix Control //输出行的前端信息控制
       -b, --byte-offset
              Print the 0-based byte offset within the input file before each line of output.  If -o (--only-matching) is specified, print the offset of the  matching  part
              itself.

       -H, --with-filename //输出文件名(在多文件下为默认设置)
              Print the file name for each match.  This is the default when there is more than one file to search.

       -h, --no-filename //禁止输出文件名
              Suppress the prefixing of file names on output.  This is the default when there is only one file (or only standard input) to search.

       --label=LABEL
              Display  input  actually  coming from standard input as input coming from file LABEL.  This is especially useful for tools like zgrep, e.g., gzip -cd foo.gz |
              grep --label=foo something

       -n, --line-number //输出文件内部的行号
              Prefix each line of output with the 1-based line number within its input file.  (-n is specified by POSIX.)

       -T, --initial-tab
              Make sure that the first character of actual line content lies on a tab stop, so that the alignment of tabs looks normal.  This is useful  with  options  that
              prefix  their  output  to  the  actual  content:  -H,-n, and -b.  In order to improve the probability that lines from a single file will all start at the same
              column, this also causes the line number and byte offset (if present) to be printed in a minimum size field width.

       -u, --unix-byte-offsets
              Report Unix-style byte offsets.  This switch causes grep to report byte offsets as if the file were a Unix-style text file, i.e., with CR characters  stripped
              off.   This  will  produce  results identical to running grep on a Unix machine.  This option has no effect unless -b option is also used; it has no effect on
              platforms other than MS-DOS and MS-Windows.

       -Z, --null
              Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name.  For example, grep -lZ outputs a zero byte after each
              file  name  instead  of  the  usual  newline.  This option makes the output unambiguous, even in the presence of file names containing unusual characters like
              newlines.  This option can be used with commands like find -print0, perl -0, sort -z, and xargs -0 to process arbitrary file names, even  those  that  contain
              newline characters.

 

=========================

正则表达式的一些规则

 A regular expression may be followed by one of several repetition operators:
       ?      The preceding item is optional and matched at most once.
       *      The preceding item will be matched zero or more times.
       +      The preceding item will be matched one or more times.
       {n}    The preceding item is matched exactly n times.
       {n,}   The preceding item is matched n or more times.
       {,m}   The preceding item is matched at most m times.
       {n,m}  The preceding item is matched at least n times, but not more than m times.
还有一些扩展的规则可以使用。

 

时间匆忙,等有时间再整理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值