grep and regular expressions

Source: Internet
Author: User
Tags file copy

A. grep concept

grep, the search for regular expressions, which means that grep is simply used to search for matching characters.


Second, the regular expression:

(1) Several repetitive actions followed by regular expressions

- ? Match at most once

-* Match 0 or more times

-+ matches more than once

-{n} matches n times

-{n,} matches at least n times

-{, m} matches up to M times

-{n,m} matches n to M times

(2) commonly used regular expression chestnuts:

^ Beginning of the line

$ End of line

[] Match any one that appears

^user$ only contains user

[tty]$ line ending with TTY

^ [^ l] Exclude directory list of associated directories

[. * 0] 0 before or after adding any character

[^ $] blank line

[^. * $] matches any string in a row

^ . . . . . . $ includes 6-character lines

^ Q start line with ^ q

^ . $ only one character line

^\. [0-9] [0-9] lines starting with a period and two digits

[0-9] \ {2 \}-[0-9] \ {2 \}-[0-9] \ {4 \} date format d d-m m-y y y y

[0-9] \ {3 \} \. [0-9] \ {3 \} \. [0-9] \ {3 \} \. [0-9] \ {3 \} I p address format

[a-za-z]* matches all words


Iii. Common grep options

(1)-C outputs only the count of matching rows.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m02/83/6a/wkiol1dzlirycykfaaa5r3untnc328.png "title="-c.png "alt=" Wkiol1dzlirycykfaaa5r3untnc328.png "/>

(2)-I is case-insensitive (only for single-character).

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m00/83/6b/wkiom1dzllorhrzoaabfiirv-ac706.png "title="-i.png "alt=" Wkiom1dzllorhrzoaabfiirv-ac706.png "/>

(3)-n displays matching lines and line numbers.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m00/83/6a/wkiol1dzlnfraoviaabdphet474746.png "title="-n.png "alt=" Wkiol1dzlnfraoviaabdphet474746.png "/>

(4)-S does not display an error message that does not exist or has no matching text.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/83/6B/wKiom1dzLs2g636-AAA_mpGRuQg755.png "title="-s.png "alt=" Wkiom1dzls2g636-aaa_mpgruqg755.png "/>

(5)-V displays all lines that do not contain matching text.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m02/83/6a/wkiol1dzlvtdi5elaabklt80xlc001.png "title="-v.png "alt=" Wkiol1dzlvtdi5elaabklt80xlc001.png "/>

File1 is a file copy, that is, the CP file File1

(6)-L only output file names that contain matching characters when querying multiple files.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m00/83/6a/wkiol1dzlx-a_jypaaa5tysivre569.png "title="-l.png "alt=" wKioL1dzLx-A_ Jypaaa5tysivre569.png "/>

(7)-H does not display the file name when querying multiple files.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/6A/wKioL1dzLzuB69HaAAArJw_GtUs384.png "title="-h.png "alt=" Wkiol1dzlzub69haaaarjw_gtus384.png "/>

(8) Multiple filtration:

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m01/83/6a/wkiol1dzl3fz13k5aaa8-5ochlk791.png "title=" exact match. png "alt=" Wkiol1dzl3fz13k5aaa8-5ochlk791.png "/>

(9) Use grep to match "and" or "or" mode

The grep command plus-e parameter indicates that extended pattern matching is allowed.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/83/6b/wkiol1dzmd2zl3poaabcwyhu34k755.png "title=" or. png "alt=" Wkiol1dzmd2zl3poaabcwyhu34k755.png "/>

-W searches the entire word, not the partial string in the word.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/83/6b/wkiol1dzmhqb14zjaaa1p5dntnc070.png "title="-w.png "alt=" Wkiol1dzmhqb14zjaaa1p5dntnc070.png "/>

(one)-A displays the n rows after the matching row

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m01/83/6c/wkiom1dzmkrbulzbaaavlr3vfwu577.png "title="-a.png "alt=" Wkiom1dzmkrbulzbaaavlr3vfwu577.png "/>

-B Displays the n rows before matching rows

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m00/83/6b/wkiol1dzmmgcjes7aaaukeyv5gm404.png "title="-b.png "alt=" Wkiol1dzmmgcjes7aaaukeyv5gm404.png "/>

-c display n rows before and after matching rows

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m01/83/6b/wkiol1dzmnns9jl0aaavuqz3r2i560.png "title="--c.png "alt=" Wkiol1dzmnns9jl0aaavuqz3r2i560.png "/>

-R recursively searches all files

usage:$ grep-r "file" *

-O displays only matched strings (useful when using regular expressions)

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/83/6b/wkiol1dzmt7jbjguaaa140vrkmy306.png "title="-o.png "alt=" Wkiol1dzmt7jbjguaaa140vrkmy306.png "/>

-B Displays the matching location

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m00/83/6c/wkiom1dzmverdn-baabkgzqbr2w617.png "title="--b.png "alt=" Wkiom1dzmverdn-baabkgzqbr2w617.png "/>

Note: The above output shows not the position within the line, but the byte position in the entire file

--color the searched characters are color-recognizable and easy to identify

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m02/83/6b/wkiol1dzmxksn0a2aaak3dwm-va616.png "title="--color.png "alt=" Wkiol1dzmxksn0a2aaak3dwm-va616.png "/>

-Q silent mode, not output, often used in shell scripts

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/83/6b/wkiol1dzmywtg7kcaaa7rceckse981.png "title="-q.png "alt=" Wkiol1dzmywtg7kcaaa7rceckse981.png "/>


Iv. using regular Expressions in grep

(1). Match any single character

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m01/83/6b/wkiol1dzmkigv_siaaa1irp-ie4721.png "title=". PNG "alt=" Wkiol1dzmkigv_siaaa1irp-ie4721.png "/>

(2) * matches the previous character any time (can be 0 times)

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m01/83/6c/wkiom1dzmpmq45d-aaa0quyecf4949.png "title=" asterisk. png "alt=" Wkiom1dzmpmq45d-aaa0quyecf4949.png "/>

(3). * matches any character of any length (greedy mode)

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m00/83/6b/wkiol1dzmsszovocaaa589kjhqm607.png "title=". Asterisk. png "alt=" Wkiol1dzmsszovocaaa589kjhqm607.png "/>

(4) [] any single character within the specified range

[^] characters outside the specified range

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/6B/wKioL1dzM-CzXUBKAABadN9iquA820.png "title=" ". png "alt=" Wkiol1dzm-czxubkaabadn9iqua820.png "/>

(5) X\{m,n\} X appears greater than m times, less than n times

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m00/83/6c/wkiom1dzm7vi27hdaaa4fhzbizc460.png "title=" curly brackets. png "alt=" Wkiom1dzm7vi27hdaaa4fhzbizc460.png "/>

(6) X\{m,\} X appears greater than M, no limit

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m00/83/6b/wkiol1dzm6ic5-yaaaa66owtckg626.png "title=" Flower bracket 2,.png "alt=" Wkiol1dzm6ic5-yaaaa66owtckg626.png "/>

(7)? Matches the preceding character 0 times or once (-E, using an extended expression)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/83/6C/wKiom1dzM5KgheKTAABMN561o7A409.png "title=" question mark. png "alt=" Wkiom1dzm5kghektaabmn561o7a409.png "/>

(8) Anchor ^ anchor start (line match)

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m00/83/6c/wkiom1dzmyeyu8kuaaaz-catl0w387.png "title=" on the angle brackets. png "alt=" Wkiom1dzmyeyu8kuaaaz-catl0w387.png "/>

(9) $ Anchor Line end (line end matching)

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/6C/wKiom1dzM0awT-AGAAA3r4jVvM8775.png "title=" to. png "alt=" Wkiom1dzm0awt-agaaa3r4jvvm8775.png "/>

(10) ^$ blank line

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/83/6c/wkiom1dzm2urkxfpaaa_obhamzs820.png "title=" empty line. png "alt=" wKiom1dzM2uRkxFpAAA_ Obhamzs820.png "/>


This article is from "GREEN" blog, please make sure to keep this source http://green906.blog.51cto.com/10697569/1793887

grep and regular expressions

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.