Extended usage of Text Processing command series--grep

Source: Internet
Author: User

Regular expressions are divided into basic regular expressions and extended regular expressions.

Typically, grep uses a basic regular expression, and if you want to use an extended regular expression, you can use the-e option, which is equivalent to Egrep

Some other options for grep:

-A N: Displays the n rows after the matching line, (after).

-B N: Displays the n rows preceding the matching line, (before).

-C N: Displays the front and back n rows. (context)

Example 1:

[[email protected] tmp]# grep--color-a 1 ' ^cpu MHz '/proc/cpuinfocpu mhz:2392.390cache size:3072 kb[[em AIL protected] tmp]#

[[email protected] tmp]# grep --color -a 1 -b 2  ' ^cpu MHz '  /proc/cpuinfomodel name      : intel (R)  Core (TM)   i3-2370m cpu @ 2.40ghzstepping        : 7cpu  mhz         : 2392.390cache size       : 3072 kb[[email protected] tmp]#[[email protected] tmp]#  grep --color -C 2  ' ^cpu mhz '  /proc/cpuinfomodel name       : intel (R)  core (TM)  i3-2370M CPU @ 2.40GHzstepping         : 7cpu MHz          : 2392.390cache size      : 3072 kbfpu : yes [[Email protected] tmp]# 


An extended regular expression:

In the extended regular expression inside the metacharacters and basic regular expressions inside the meta-characters are basically the same, the difference is mainly a few:

+: Matches the previous character appears 1 or more times.

{N,m}: The previous character appears N to M times.

|: Represents or. If a|b, the match is either A or B.

(): Indicates grouping, no backslash is required.


[[email protected] tmp]# grep-e ' (c|c) at ' Aacatcat[[email protected] tmp]#


Matches the number between 1-255:

Grep-e--color ' \b ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \b '


[Email protected] ~]# Ifconfig bond0 | grep ' inet addr ' | Grep-e--color-o ' (\b ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) (\.\b) {3}\b ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \b ' 172.17.100.252172.17.100.255255.255.255.0[[email protected] ~]#



This article from "Love Open source, happy to share!" "Blog, be sure to keep this provenance http://hezhanglinux.blog.51cto.com/10861477/1711027

Extended usage of Text Processing command series--grep

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.