Linux (10) -- grep command and Regular Expression

Source: Internet
Author: User

Grep (Global Regular Expression version) allows Pattern searches for text files. If the matching mode is found, grep prints all rows containing the mode. Grep supports basic regular expressions and its extension set. Grep has three types of Deformation:
Grep: Standard grep command. This format is mainly discussed in this chapter.
Egrep: Extended grep, which supports basic and extended regular expressions, but does not support the application of the/Q mode range. It is not discussed here.
Fgrep: Quick grep. You can search for strings instead of a single mode. Do not misunderstand the word fast. In fact, it is equivalent to grep speed.

1. grep format

Grep [Option] basic regular expression [file]

1. display the string containing "48" $ grep-c "48" data. f

2. display the number of rows that meet the matching mode $ grep-n "48" data. f

3. Show All rows that do not contain 48 $ grep-V "48" data. f

4. Exact match, containing only 48 rows $ grep "48/>" data. f

5. It is case sensitive. By default, grep is case sensitive. to query case-insensitive strings, you must use the-I switch.

$ Grep-I "Sept" data. f

Ii. grep and Regular Expressions

Use regular expressions to add rules to pattern matching. Therefore, you can add more options to the extraction information. It is best to enclose the Regular Expression in single quotes to preventGrepThe private mode used in is confused with some special methods of shell commands.

1. Pattern range: We suppose we want to extractCodeThe city location is 484 and 483 $ grep '48 [34] 'data. f

2. The beginning of the line does not match: $ grep '^ [^ 48] 'data. f

3. Set case sensitivity: $ grep '[ss] EPT 'data. f

Iii. System grep command

1. to query the directories in the directory list, use the following method: $ LS-L | grep '^ d'

2. If you want to query all files in a directory that does not contain a directory, use the following method: $ LS-L | grep '^ [^ d]'.

3. to query a directory set with executable permissions for other users and other user group members, use the following method: $ LS-L | grep '^ d... x. x. x'

Iv. Summary

I hope you have understood the flexibility of grep. It is a powerful and popular tool that has been used in DOS like many other UNIX tools. If you want to quickly search for strings or modes through files, grep is a good choice. In short, grep is an important tool in shell programming.

 

 

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.