grep command Learning in Linux

Source: Internet
Author: User
Tags character classes dmesg egrep

1. Introduction

grep is a powerful text search tool that uses regular expressions to search for text and print matching lines. The grep family of Unix includes grep, Egrep, and Fgrep. Egrep and Fgrep commands are only a small difference from grep. Egrep is the extension of grep, which supports more re metacharacters, and fgrep is fixed grep or fast grep, which regards all the letters as words, and says that the metacharacters in the regular expression represents the literal meaning back to itself, no longer special. Linux uses the GNU version of grep. It is more powerful and can use the Egrep and FGREP functions with the-G,-e,-f command line options.

2.grep Common usage

grep [-ACINV] [--color=auto] ' search string ' filename
Options and Parameters:
-A: binary files are searched for data in the form of a text file
-C: Calculates the number of ' search string ' found
-I: Ignores case differences, so case is considered the same
-N: Output line number by the way
-V: Reverse selection, which shows the line without the ' search string ' content!
--color=auto: Can be found in the keyword part of the color display Oh!

Remove the/etc/passwd, where Root is present.

# grep root/etc/passwd or # CAT/ETC/PASSWD | grep root

Remove the/etc/passwd, which is the root row, and display the line numbers of the rows in/etc/passwd

# grep-n ROOT/ETC/PASSWD

in terms of the display of keywords, grep can use--color=auto to display the keyword portion using color.
The/etc/passwd will be removed and no root and Nologin rows will appear.

# grep-v ROOT/ETC/PASSWD | Grep-v Nologin

  Use DMESG to list the core information, and then grep to find the line containing the ETH, in the first two lines of the keyword row and the last three lines are also caught out of the display

# DMESG | Grep-n-a3-b2--color=auto ' ETH '

finding a directory recursively based on file content

# grep ' energywise ' *       #在当前目录搜索带 ' energywise ' line file # grep-r ' energywise ' *    #在当前目录及其子目录下搜索 ' energywise ' File # grep-l -R ' energywise ' * #在当前目录及其子目录下搜索 ' energywise ' file, but does not display matching rows, only matching files are displayed
3.grep and regular expressions

  Search for character classes: If I want to search for the two words of test or taste, I can find that they have a common ' t?st ' existence ~ this time, I can search for:

# grep-n ' t[ae]st ' regular_express.txt

In fact [] in a few bytes, he would like to represent a "one" byte, so, the above example shows that the string I need is "tast" or "test" two string only!
Inverse selection of character classes [^]: If you want to search for a line that has OO, but do not want to have g in front of Oo, the following

# grep-n ' [^g]oo ' Regular_express.txt

   Continuous character class: Suppose I don't want to have a lowercase section in front of Oo, so I can write [^abcd....z]oo, but this doesn't seem convenient, because the order of encoding on the ASCII of lowercase bytes is sequential, so We can simplify it to the bottom:

# grep-n ' [^a-z]oo ' Regular_express.txt

That is , when we are in a set of collection bytes, if the byte group is contiguous, such as uppercase English/lowercase english/numeric, etc., you can use [a-z],[a-z],[0-9] and other ways to write, then if our request string is the number and English? Oh! He wrote it all together and became: [A-za-z0-9].
We're going to get the line with numbers, and that's it:

# grep-n ' [0-9] ' regular_express.txt

  Beginning of line with trailing bytes ^ $
Line beginning character: What if I want the only one listed at the beginning of the line? This is the time to use the anchor byte! We can do this:

# grep-n ' ^the ' regular_express.txt

Also, what if I want the line that starts with the lowercase section? Can do this:

# grep-n ' ^[a-z] ' regular_express.txt

If I don't want to start with an English letter, it can be:

# grep-n ' ^[^a-za-z] ' regular_express.txt

^ symbols, which are different from outside of the character class symbols (brackets [])! In [] represents the "reverse selection", outside [] represents the meaning of positioning at the beginning of the line!
  So if I want to find out, the line ending at the end of the row is the decimal point (.):

# grep-n ' \.$ ' regular_express.txt

It is important to note that because the decimal point has other meanings, you must use the escape character (\) to remove its special meaning!
  Find the blank line:

# grep-n ' ^$ ' regular_express.txt

Because only the beginning and the end of the line (^$), so that you can find the blank line!
  Any one of the bytes. With repeating bytes *
The meanings of these two symbols in regular expressions are as follows:

    • . (decimal point): Represents "must have an arbitrary byte" meaning;
    • * (asterisk): represents "repeating the previous character, 0 to infinity" meaning, for the combined form

Suppose I need to find out g?? A string of D, that is, a total of four bytes, beginning with G and ending with D, I can do this:

# Grep-n ' G.. d ' Regular_express.txt

  What if I want to list data that has Oo, OOO, oooo, and so on, which means at least two (including) O? because * represents the meaning of "repeating 0 or more of the preceding RE characters", "o*" stands for "having empty bytes or an O or more bytes", so "Grep-n ' o* ' Regular_express.txt" will have all the data Print it out on the terminal! When we need a "minimum of two o ' strings", we need ooo*, i.e.:

# grep-n ' ooo* ' regular_express.txt

If I want the string to start with the end is G, but two g can only exist between at least one o, that is, Gog, Goog, Gooog .... Wait, what's that supposed to be?

# grep-n ' Goo*g ' regular_express.txt

If I want to find the line where G begins and ends with G, the characters are optional

# grep-n ' G.*g ' regular_express.txt

  What if I want to find the line of "any number"? Because there are only numbers, it becomes:

# grep-n ' [0-9][0-9]* ' Regular_express.txt

  limit the continuous RE character range {}. we can use. With the RE character and * To configure 0 to infinitely multiple repeating bytes, so what if I want to limit the number of repeating bytes in a range of intervals? I want to find out two to five o continuous string, how to do? This is the time to use the qualified character {}. But since the symbol {and} is of special significance in the shell, we have to use the character \ To make him lose special meaning. The syntax for {} Is this, assuming I want to find two o strings, which can be:

# grep-n ' o\{2\} ' regular_express.txt

   Let's say we're going to find out that G is followed by 2 to 5 O, then a string of G, and he will be like this:

# grep-n ' Go\{2,5\}g ' regular_express.txt

   What if I want 2 o ' goooo....g? In addition to being gooo*g, it can also be:

# grep-n ' Go\{2,\}g ' regular_express.txt
4. Expand grep (GREP-E or egrep):

  The main benefit of using the extended grep is the addition of additional regular expression meta-character sets.
Prints all rows that contain NW or EA. If you are not using Egrep, but grep, there will be no results detected.

# egrep ' nw| EA ' Testfile     

   for standard grep, if \,grep is preceded by an extension metacharacters, the extended option-E is automatically enabled.

#grep ' nw\| EA ' Testfile

searches for all rows that contain one or more 3.

# egrep ' testfile# grep-e ' testfile# grep ' 3\+ ' testfile        

  Searches for all rows that contain 0 or 1 decimal points characters.

# egrep ' 2\.? [0-9] ' testfile# grep-e ' 2\.? [0-9] ' testfile# grep ' 2\.\? [0-9] ' testfile# first contains 2 characters, followed by 0 or 1 points, followed by a number between 0 and 9.

A row that searches for one or more contiguous no.

# egrep ' (NO) + ' testfile# grep-e ' (no) + ' testfile# grep ' \ (no\) \+ ' testfile   #3个命令返回相同结果,

5. Do not use regular expressions

the Fgrep query is faster than the grep command, but not flexible enough: it can only find fixed text, not regular expressions. If you want to find a line that contains an asterisk character in a file or output

#fgrep '  * '/etc/profile#grep-f ' * '/etc/profile

grep command Learning in Linux

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.