The use of grep under Unix

Source: Internet
Author: User
Tags regular expression

grep (Global Regular expression version) allows schema lookup of text files, and grep supports basic regular expressions and its extension sets.

GREP has three kinds of variants:

grep standard grep command

Egrep extends the grep command, supports basic and extended regular expressions, but does not support \q-mode-wide applications

Fgrep Fast grep command, allows you to find strings instead of a pattern, don't misunderstand fast, actually and grep speed is quite

The general format of grep:

grep [options] basic Regular expression [file] here the basic regular expression can be a character

Use double quotes:

String parameters best Use double quotes to expand, one is to avoid being misunderstood as a shell command, two can be used to find a number of words composed of strings, in the invocation of variables also use double quotes, such as grep "$MYVAR", otherwise no results, in the call pattern match is, should use single quotes

grep options:

-C outputs only the count of matching rows, such as grep-c "test" *.txt, which shows 4, which indicates that there are 4 rows containing test

-I is case-insensitive (applies only to given), such as Grep-i "bank" *.c equivalent to Grep-i "bank" *.c

-H do not display file name when querying multiple files

-L Output only file names that contain matching characters when querying multiple files

-N Displays matching rows and line numbers, showing the number of rows before each line showing the content

-S does not display error messages that do not exist or have no matching text

-V Displays all rows that do not contain matching text, grep-v "test" abc.txt and displays the contents of the row that does not contain "test"

-W matches a word as a unit

-e allows extended pattern matching to be used

grep and Regular Expressions:

Using regular expressions is best used in single quotes to avoid arguments being executed as shell commands

^ Anchor Line start like: ' ^grep ' matches all rows beginning with grep.

$ anchor row end like: ' grep$ ' matches all rows with grep ending.

. Match a newline character Furu: ' GR.P ' matches the GR followed by an arbitrary character followed by P.

* Match 0 or more previous characters Furu: ' *grep ' matches all one or more spaces immediately following the grep row ... * To use on behalf of any character.

[] matches a specified range of characters, such as ' [Gg]rep ' matches grep and grep.

[^] matches a character that is not in the specified range, such as: ' [^a-fh-z]rep ' matches do not contain a-f and h-z letters, followed by rep rows.

\(.. \) tag matching characters, such as ' \ (love\) ', Love is marked as 1.

\< anchors the beginning of a word, such as: ' \> anchor the end of a word, such as ' grep\> ' to match a line containing a word that ends with grep.

X\{m\} repeats characters x,m times, such as: ' 0\{5\} ' matches rows containing 5 O.

X\{m,\} repeats the character x, at least m times, such as: ' O\{5,\} ' to match at least 5 o rows.

X\{m,n\} repeats the character x, at least m times, not more than n times, such as: ' O\{5,10\} ' matches a row of 5--10 O.

\w Matching

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/unix/

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.