bash学习之九:正则表达式(regular expression)和grep

本文不集中介绍正则表达式的语法。正则表达式的语法会在grep的示例中介绍。

Wikipedia对grep的描述如下:

grep is a command line text search utility originally written for Unix. The name is taken from the first letters in global / regular expression / print, a series of instructions for the ed text editor. The grep command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.

由此可见,介绍正则表达式,不能不介绍grep。下面先介绍grep的常用选项(以我个人的看法,顺序由常用到不常用)。

1、grep的常用选项

-n, --line-number
       Prefix each line of output with the line number within its input
       file.

-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.

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

-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 con-
       stituent 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.

-F, --fixed-strings
       Interpret  PATTERN as a list of fixed strings, separated by new-
       lines, any of which is to be matched.

-c, --count
       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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值