Grep/egrep Command collection in Linux

Source: Internet
Author: User
Tags egrep

Syntax: grep [-cinvabc] ' word ' filename

-C: Print the number of lines that meet the requirements

-I: Ignore case

-N: Output with the same number of lines as required

-V: Print rows that do not meet the requirements

-A: followed by a number (with or without spaces), for example, –A2 to print the line that meets the requirements and the following two lines

-B: followed by a number, such as –B2, to print the line that meets the requirements and the above two lines

-C: followed by a number, such as –C2, to print the line that meets the requirements and two rows above and below


Examples:

Lines containing ' halt ' and two lines below this line are printed:

Grep-a2 ' Halt '/etc/passwd

The lines containing ' halt ' and the two lines above it are printed:

Grep-b2 ' Halt '/etc/passwd

The lines containing ' halt ' and the lines above and below the line are printed out:

Grep-c2 ' Halt '/etc/passwd

Filter out a line with a keyword and lose a travel number:

Grep-n ' Root '/etc/passwd

Filter lines that do not have a keyword, and enter the travel number:

Grep-nv ' Nologin '/etc/passwd

Filter out all rows that contain numbers:

grep ' [0-9] '/etc/inittab

Filter out all rows that do not contain numbers:

Grep-v ' [0-9] '/etc/inittab

Remove all lines that begin with ' # ':

Grep-v ' ^# '/etc/inittab

Remove all empty lines and lines that begin with ' # ':

Grep-v ' ^# '/etc/crontab |grep-v ' ^$ '

How to print a line that does not start with the English letter

grep ' ^[^a-za-z] ' test.txt
grep ' [^a-za-z] ' test.txt

Filter any character with repeating characters:

grep ' R.. O '/etc/passwd. Represents any one character
grep ' ooo* '/etc/passwd * represents 0 or more preceding characters
grep '. * '/etc/passwd |wc-l '. * ' denotes 0 or more arbitrary characters, and a blank line is included

Specify the number of occurrences of the character to filter:

grep ' o\{2\} '/etc/passwd


Filter one or more of the preceding characters:

Egrep ' o+ ' test.txt

Filter 0 or one of the preceding characters:

Egrep ' O? ' test.txt

Filter string 1 or String 2:

Egrep ' aaa|111|ooo ' test.txt

Applications in Egrep ():

Egrep ' R (oo) |                                                               (at) o ' test.txt () denotes a whole, for example (OO) + means 1 ' oo ' Or more ' oo '









Grep/egrep Command collection in Linux

Related Article

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.