Linux Chinese search Tool "grep" command detailed

Source: Internet
Author: User
Tags egrep

there should be two impulses in life, one is a vigorous love, one is to say walk on the trip. I belong to the third, vigorous resignation and then participate in the Marco of the Linux face-to-face class, two weeks of Linux has some preliminary understanding, at the same time, two weeks within eight days of the course of the various definitions, concepts, principles, orders due to the volume of large, trivial, Also really give us these small white a dismount, even if every day to retreat also feel that time is not enough. In view of their own responsibility and Marco of the job requirements, the text Search tool grep to write some user guide, which may have a description of the inaccurate or biased, look in time to correct.

start .....

first, let's talk about what grep is and where it's used.

grep is a Linux Globle search Regular expression and print out the Line windows os word excel< Span style= "font-family: ' The song Body '; > One thing about documents, I believe most people are in excel So,do you understand?

In short, you can put Grep is likened to the "Find" button, and I believe you see this, but given that the Linux system uses command-line operations for most of the time and is different from the graphical interface of the Windows system, we in order to enter a variety of commands through the small black box (very high-end atmosphere of the grade) before entering a text, so that after entering the text is also used to command the page, search, modify and so on. Here the "find" is the grep command, in addition to this there are egrep,fgrep, these will do not repeat.

, understand what grep is, and let 's talk about how grep is used.

since it is a text Search tool under Linux, first there is a Linux, then into the Linux system, and then casually find a trivial text to see it, as to how to get in, their Google go,Google does not go? That's the mother.

The following description of the process of file paths and files please ignore, you can follow this step by step, you can also study to go.

after entering the system to open a file, such as open the Fstab file under the ETC directory , you can use cat/etc/fstab this command,

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5D/0C/wKiom1Uflc_ym8Q7AAGG4IljoaA231.jpg "title=" 1.png " alt= "Wkiom1uflc_ym8q7aagg4iljoaa231.jpg"/>

See the following pile of things, this is the text inside the file,grep is to find the object specified in this, such as the search inside the Word, or "/" this symbol and so on.

Knowing the above, we can use the grep command to find it!

grep command is used format requirements, we can use the man command to see the use of grep, as to how to use the " Men " command, self-degree niang.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5D/08/wKioL1UflyrzplXXAABh2tthl1A874.jpg "title=" 2.png " alt= "Wkiol1uflyrzplxxaabh2tthl1a874.jpg"/>

use format for grep in synopsis

Man . This command will find that there are a number of options, which can be added and not added.

For example , grep--color=[when], where the When is preferable to Auto,always,never,

when using grep–color=auto , the search is automatically displayed in other colors so that users can distinguish between always and auto will in any case all match fields are color-coded, and Auto The color is only added when the output to the terminal,never is never color-added meaning. If the option is not added, the default output is.

Other options:

-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

-O : outputs only matching parts

Here are a few examples to understand.

1 . The dev line appears in the/etc/fstab file

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5D/0C/wKiom1UflgWS7Gj7AADh-Ecy8f0285.jpg "title=" 3.png " alt= "Wkiom1uflgws7gj7aadh-ecy8f0285.jpg"/>

The things that show up here are already marked red, because I've made grep automatically take -color=auto , as shown, and you don't have to worry about it.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5D/0C/wKiom1UflhmyLUoYAACuAuPoq5k362.jpg "title=" 4.png " alt= "Wkiom1uflhmyluoyaacuaupoq5k362.jpg"/>

2 . The dev line appears in/etc/fstab , and the rows are displayed in /etc/fstab the line number

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5D/0C/wKiom1Ufli3QhyGsAADQMSsZuE0477.jpg "title=" 5.png " alt= "Wkiom1ufli3qhygsaadqmsszue0477.jpg"/>

3 . The line that does not appear Dev is displayed in the /etc/fstab

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5D/08/wKioL1Ufl8bRIHfNAACssCRa7xM266.jpg "title=" 6.png " alt= "Wkiol1ufl8brihfnaacsscra7xm266.jpg"/>

4.Calculate The number of dev occurrences in /etc/fstab

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5D/0C/wKiom1UflqHR9BerAAApI9r-m-0922.jpg "title=" 7.png " alt= "Wkiom1uflqhr9beraaapi9r-m-0922.jpg"/>

5 . The dev line appears in the/etc/fstab , ignoring the case

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5D/0D/wKiom1UflrngDCXCAADiPynCa2k015.jpg "title=" 8.png " alt= "Wkiom1uflrngdcxcaadipynca2k015.jpg"/>

6.Show Dev in /etc/fstab

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5D/08/wKioL1UfmAuAM-FNAAA_zzlJmdY904.jpg "title=" 9.png " alt= "Wkiol1ufmauam-fnaaa_zzljmdy904.jpg"/>

These are the basic uses of grep.

Next will be the use of grep and regular expressions, yes! The Table! Up Expression Ah!

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5D/0D/wKiom1Uflt2hx24oAABBnq8ZGl8894.jpg "title=" 10.jpg "alt=" Wkiom1uflt2hx24oaabbnq8zgl8894.jpg "/>

displays lines that begin with # in the/etc/rc.d/rc.sysinit file , followed by at least one white-space character, and then have at least one non-whitespace character

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5D/0D/wKiom1UflvCwwCe6AAFssfELxQE765.jpg "title=" 11.png "alt=" Wkiom1uflvcwwce6aafssfelxqe765.jpg "/>

As for what you see in the quotation marks behind grep ... There is time to explain later .... recommended reading "Regular expression- minute Introductory tutorial"

let's talk about the use of egrep and fgrep.

In fact grep–e and egrep is a meaning, but there are differences, the difference between the two:

1, grep followed by the regular expression inside the escape character "\" and egrep no "\"(except anchor symbol);

Give me a chestnut.

we still show a line in the/etc/rc.d/rc.sysinit file that starts with # , followed by at least one white-space character, and then has at least one non-whitespace character, this time with Egrep

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5D/08/wKioL1UfmIjx7S9aAAI1o2zhpOY885.jpg "title=" 12.png "alt=" Wkiol1ufmijx7s9aaai1o2zhpoy885.jpg "/>

2, egrep can use a|b This or the relationship

And then a chestnut.

Displays the default shell for root, fedora, or user1 users on the current system

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5D/08/wKioL1UfmLXRthhZAABRA33_OBM684.jpg "title=" 13.png "alt=" Wkiol1ufmlxrthhzaabra33_obm684.jpg "/>

This is the use of Egrep, and grep only a cautious difference

The last fgrep,does not support regular expressions, only for quick-check, there is nothing to talk about.

The above is a personal summary, semantic if there is ambiguity or description of the non-standard place, look.


Linux Chinese search Tool "grep" command detailed

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.