A detailed description of the search tool and basic regular expressions for grep file content in Linux

Source: Internet
Author: User

A detailed description of the search tool and basic regular expressions for grep file content in Linux

grep command : Search for text based on patterns (matching criteria for text characters and meta-character combinations of basic Regular Expressions )

and displays the line of text that conforms to the pattern.

Format: grep [option] Match condition text name

Option:-I: Ignore case

-V: Show rows that are not matched by the pattern

-O: Displays only strings that are matched by the pattern

--color: Search out files High brightness display

-A: Matches the next few lines of the specified line

-B: Matches the last few rows of the specified row

-C: matches the upper and lower lines of the specified row

-e: Extended regular expressions are equivalent to Egrep

Regular Expressions : Regular expression, abbreviated RegExp

Note: \ is a caret that prevents conflicts with bash and takes off its original meaning

Meta characters :

.: Matches any single character

[]: matches any single character within the specified range

[^]: matches any single character outside the specified range

For example: special character set: [:d igit:], [: Lower:], [: Upper:], [:p UNCT:], [: Space:], [: Alpha:], [: Alnum:]

number of matches :

*: Matches any of its preceding characters any time

. *: Any character of any length

\?: Match its preceding character 1 or 0 times

\{a,d\}: Matches the preceding characters at least a time, up to D times

For example: \{1,\} indicates that the preceding character matches at least 1 times and is not limited to

\{0,6\} means: The preceding character matches at least 0 times, up to 6 times

Example: Display lines that start with one or more whitespace characters in a/boot/grub/grub.conf file

grep ' ^[[:space:]\{1,\}] '/boot/grub/grub.conf

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/E4/wKioL1cMZlLSoomsAAEbKVUgbo8919.jpg "title=" Grep11.jpg "alt=" Wkiol1cmzllsoomsaaebkvugbo8919.jpg "/>

The position is locked or called anchored :

^: Locks the beginning of the line, any content after this character must appear at the beginning of the line

Example: Displaying lines beginning with R in the/etc/passwd file

grep ' ^r '/etc/passwd

$: Locks the end of the line, any content that precedes this character must appear at the end of the row

Example: Show the line in the/etc/passwd file that the bash ends

grep ' bash$ '/etc/passwd

^$: Indicates a blank line

\< or \b: Anchor word, any character following it must appear as the first word

\> or \b: anchors the ending, any character preceding it must appear as the tail of the word

Example: Displays a line in the/etc/passwd file that begins with Root and ends with the root word

grep ' ^root\> '/etc/passwd

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/E4/wKioL1cMZ6aARz1JAACg2ippFjs572.jpg "title=" Grep2.jpg "alt=" Wkiol1cmz6aarz1jaacg2ippfjs572.jpg "/>

group : \ (\)

For example: \ (mn\) * means matching any MN combination character

Back to reference

\1: Refers to the first opening parenthesis and all the contents of the corresponding closing parenthesis

\2: Refers to the second opening parenthesis and all the contents of the corresponding closing parenthesis

\ n: references the nth opening parenthesis and all the contents of the corresponding closing parenthesis

Example: Displays a line in a/etc/inittab file that starts with a number and ends with the same number at the beginning

grep ' ^\ ([0-9]\). *\1$ '/etc/inittab

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/E4/wKioL1cMZSfTaWFLAAGt40dQEFU507.jpg "title=" Grep1.jpg "alt=" Wkiol1cmzsftawflaagt40dqefu507.jpg "/>

This article is from the "Xavier Willow" blog, please be sure to keep this source http://willow.blog.51cto.com/6574604/1762878

The grep file Content search tool and basic regular expression 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.