Bash regular expression, text file find grep egrep

Source: Internet
Author: User
Tags egrep

1. Syntax + options

syntax: grep [-CINVABC] ' word ' filename
-C: Print the number of lines that meet the requirements
- 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), e.g. –A2
prints the line that meets the requirements and the following two lines
-B: followed by a number, such as –B2, indicates that the print meets the requirements
Line and the above two lines
-C: followed by a number, such as –C2, indicates that printing meets the requirements
rows and two rows above and below each other .
-R: All files under the directory are traversed
grep-r ' root '/etc/all files in the directory are checked all the time

[0-9] take any 1 of the brackets


2. Example Introduction
filter out a line with a keyword and enter the travel number grep-n ' root ' 1.txt
filter out lines without a keyword and lose travel number grep-n-V ' root ' 1.txt
filter out all rows that contain numbers grep ' [0-9] ' 1.txt
filter out all lines that do not contain numbers grep-v ' [0-9] ' 1.txt
Remove all lines beginning with ' # ' grep-v ' ^# ' 1.txt
Remove all empty lines and lines beginning with ' # ' grep-v ' ^$ ' 1.txt|grep-v ' ^# '
filter out lines that begin with the English alphabet grep ' ^[a-za-z] ' 1.txt
filter out lines that start with a non-digit grep ' ^[^0-9] ' 1.txt
filter any one or more characters grep ' r.o ' 1.txt;
grep ' r*t ' 1.txt; grep ' r.*t ' 1.txt
. Represents any one character; * represents 0 or more preceding characters;
. * Denotes 0 or more arbitrary characters, and a blank line is included
Specify the number of filter characters grep ' o\{2\} ' 1.txt




3. Egrep
The Egrep tool is an extension of the grep tool
egrep ' o+ ' 1.txt means 1 or more than 1 preceding characters
egrep ' O? ' 1.txt represents 0 or 1 preceding characters
egrep ' roo|body ' 1.txt match roo or match body
egrep ' r (oo) | (at) o ' 1.txt brackets denote a whole
egrep ' (oo) + ' 1.txt denotes 1 or more ' oo '


4.. * + ? Summarize
. Represents any one character (including special characters)
* Represents 0 or more * preceding characters
. * denotes any arbitrary character (contains blank lines)
+ represents 1 or more + preceding characters
? Represents 0 or 1 characters in front of each other
where, +? GREP is not supported, EGREP is supported.


^ The character following the beginning of the anchor line, which must appear at the beginning of the line

$ anchor Line end, character following this character, must appear at the end of the row

"^" in [^] denotes non-meaning

^$ Empty Line

Any character after \< or \b must appear as the hand of the word.

Any character in front of the \> or \b must appear as the tail of the word

\<root\> for the whole word

\ (\) Group: \ (ab\) * Indicates the presence of any AB

Extended groupings do not need to be added to the meaning;


A|b means A or B.


This article is from the "you and I Walk" blog, please be sure to keep this source http://ondali.blog.51cto.com/6650368/1615962

Bash regular expression, text file find grep egrep

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.