Linux Command learning: grep Text Search Tool

Source: Internet
Author: User
Tags control characters

Mark: This series is just a record of the author in the Linux learning process of a bit, novice may refer to, the master will skip it!!!


Linux Learning Summary:grep Text Search Tool

function: According to the user-specified text pattern (search conditions) to the target file line by row search, and the matching line printed out, it's all called " Global Regular expression Print", globally regular expressions, which are open to all users by default.

syntax Format: Grep [option] Pattern file

Option: command options, specifically:

-V:--revert-match Reverse selection, showing all rows that do not contain matching files;

-I:--ignore-case ignores character case;

-C:--count calculates the number of columns that conform to the style

-E:--extended-regexp supports the use of regular expressions;

-A:--after-context in addition to displaying columns that conform to the template style and displaying the contents after that line

-B:--before-context In addition to displaying columns that conform to the template style and displaying the contents before the row

-C:--context In addition to displaying columns that conform to the template style and displaying the contents before the line

-N:--line-number The number of columns in the row before displaying the line that matches the style;

Example One showsall lines in the file "/tmp/testin.sh" that do not contain "Linux"

command: grep–v–n "Linux"/tmp/testin.sh

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/78/A9/wKioL1aA7XzRlaFvAABEjxAlSQg539.png "title=" 1.png " alt= "Wkiol1aa7xzrlafvaabejxalsqg539.png"/>


example Two, query file "/tmp/testin.sh" keyword "Linux" case-insensitive all rows

Command: GREP-V-n "Linux"/tmp/testin.sh

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/78/A7/wKioL1aA5jjAOhK5AAA9NOARD9s711.png "title=" 2.png " alt= "Wkiol1aa5jjaohk5aaa9noard9s711.png"/>


instance three, display contains the case " all lines of the Linux keyword and calculate the total

Command: Grep-v-n-c "Linux"/tmp/testin.sh

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/78/A7/wKioL1aA5oaBR3wCAAAOHJVKJAo806.png "title=" 3.png " alt= "Wkiol1aa5oabr3wcaaaohjvkjao806.png"/>


Regular Expressions: Are patterns written by a class of characters, many of which do not represent their literal meaning, but rather express control or wildcard functions.

The regular expression meta-character, where:

Character Matching:

“.” : matches any single character, such as "GREP.P" matching grep followed by an arbitrary character;

"[]": matches any character within the specified range, such as "[Gg]rep" matches grep and grep;

"[^]": matches any character not in the specified range, such as "[^a-z]grep" matches the beginning of a letter that does not contain "A-Z" , followed by grep;

Location anchoring:

"^": beginning of line locator,"^grep" matches all lines starting with grep;

"$": End -of- line locator,"grep$" matches all lines ending with grep;

"^$": blank line;

"\<": anchors the beginning of a word, such as:"\<grep" matches a line containing a word that begins with "grep";

"\>": anchors the end of a word, such as:"grep\>" matches a line containing words ending with "grep";

Number of matches:

"*": matches 0 or more characters, such as "*grep" matches all one or more spaces followed by the line of grep;

". *": matches any character;

“\?” : 0 or one time, the preceding characters are optional, such as "Gr\?ep", the representative can be grep,EP;

"\{m\}": repeats M times, such as "grep\{2\}", matching rows containing at least 2 "grep" ;

"\{m,\}": repeat characters at least m times, such as "grep\{2,\}" matches at least 2 grep rows;

"\{m,n\}": repeat characters at least m times, up to n times, such as "grep\{2,3\}" matches 2 to 3 the line of grep;

POSIX characters:

[: Alnum:]: literal numeric characters;

[: Alpha:]: literal character;

[:d igit:]: numeric characters;

[: Graph:]: non-null characters (non-whitespace, control characters);

[: Lower:]: small characters;

[:p rint:]: non-null characters, including spaces;

[: Space:]: all whitespace characters (new lines, spaces, tabs);

[: Upper:]: Uppercase characters;

[:p UNCT:]: punctuation;


example Four, displayingthe word"grep", "grep" in the file " /tmp/test.grep"

command:grep–n "[Gg]rep"/tmp/test.grep

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/78/A9/wKiom1aA5zyDryLuAABV82Vn9t0706.png "title=" 4.png " alt= "Wkiom1aa5zydryluaabv82vn9t0706.png"/>


example Five, displaytheline that begins with "grep" in the file "/tmp/test.grep"

command:cat/tmp/test.grep | grep ^grep

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/78/AA/wKiom1aA6sGB4wyBAAAk5pdA23Y817.png "title=" 1.png " alt= "Wkiom1aa6sgb4wybaaak5pda23y817.png"/>


example Six, displaysthe line in the file "/tmp/test.grep" that ends with"grep"

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/78/AA/wKiom1aA60LCl-1KAAAlPWfcNJI219.png "title=" 2.png " alt= "Wkiom1aa60lcl-1kaaalpwfcnji219.png"/>


example Seven, displays the line with at least one whitespace character in/tmp/test.grep;

command:grep-n "[[: space:]]\{1,\}$]/tmp/test.grep

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/78/A8/wKioL1aA6_PyfN_wAABCs9OK8VU104.png "title=" 3.png " alt= "Wkiol1aa6_pyfn_waabcs9ok8vu104.png"/>


Cond!

This article from "near home" blog, declined reprint!

Linux Command learning: grep Text Search Tool

Related Article

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.