Linux commands: grep talk

Source: Internet
Author: User
Tags aliases

grep Introduction to Commands:

This command searches for text based on the pattern, and displays the lines of text that conform to the pattern. contains matches,

is not an exact match. This means that as long as a line is partially matched, it is displayed.

* Extended reading: mode (pattern) refers to the combination of meta-characters of the text wildcards regular expression to match the criteria

1 . Command format:

grep [OPTIONS] PATTERN [FILE ...]

Grep-i ' Root '/etc/passwd

2 . Command function:

Searches the text to display the line of text that conforms to the pattern.

3 . Command parameters:

-I ignore case when finding

Keywords in--color mode are highlighted in color.

-V shows options that are not matched by the pattern

-O displays only strings that are pattern-matched

Regular expressions are supported:

.: Matches any single character

*: Match any of its preceding characters (including 0 times)

. *: Matches any character of any length

[]: matches any single character within the specified range

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

[:d Igit:]  Digital [: Lower:] lowercase letter [: Upper:] Uppercase [:p UNCT:] Character

[: Space:] space [: Alpha:] All Letters [: Alnum:] All letters and numbers   

\? : matches the preceding character 0 or 1 \ means escape

\ (m,n\): matches the preceding character at least m times, up to N times

\ (m,\): matches the preceding character at least m times

\ (0,n): matches the preceding character, up to n times

Location anchoring:

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

$: Anchor Line end, any content preceding this character must appear at the end of the row

^$: Blank Line

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

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

Group:

\ (\) put parentheses in front of a whole

\ (ab\) * Put AB in front of a whole

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

\3: refers to the third 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

4 . Command instance:

1. Find all lines that contain some characters

grep "Root"/etc/passwd

[[email protected] ~]# grep "root"/etc/passwd

Root:x:0:0:root:/root:/bin/bash

Operator:x:11:0:operator:/root:/sbin/nologin

2. grep--color to highlight the found characters

aliases can be written to the configuration document VIM ~/.BASHRC1

#. BASHRC

# User specific aliases and functions

Alias rm= ' Rm-i '

Alias cp= ' Cp-i '

Alias mv= ' Mv-i '

Alias grep= ' grep--color '

3, grep ' \ ([0-9]\). *\1$ '/test/test1.txt find the line in the Test1.txt document that must end with the first number in front

[[email protected] test]# grep ' \ ([0-9]\). *\1$ '/test/test1.txt

2who is you 2

4 YOUARERINGT4

5,he is 2tom2

7 eg is over 11

8 Thanks you 1and1

[email protected] test]# cat Test1.txt

1,this is helloword! There is no number ending here

2who is you 2 match here to

3,no 4 is not 2. The end of this is with the. End so does not match to

4 YOUARERINGT4. The end of this is with the. End so does not match to

4 YOUARERINGT4 is matched here, ending with the number 4 that appears earlier

5,he is 2tom2 here, which ends with the number 2 that appears earlier

5,he is 2tom2. The end of this is with the. End so does not match to

6 Tom is Cat4,yes is 4. The end of this is with the. End so does not match to

7 eg is over 11. The end of this is with the. End so does not match to

7 eg is over 11 matches here, is ending with the number 1 that appears before

8 thanks you 1and1 here to match, is ending with the number 1 that appears before



This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1862832

Linux commands: grep talk

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.