Example of using regular expressions for Linux learning-grep

Source: Internet
Author: User
Tags uppercase character egrep

Using grep with regular expressions on Linux can produce a powerful search effect, because regular expressions contain more special characters, so when combined with grep it is best to enclose the expression in single quotes to avoid errors. First create a file RegExp.txt with the following text content:

--------TEXT BEGIN-------------good morining TEACHERHELLP world is as Scriptgold sunshine looks beautifulgolden time files God Belss mewhat a delicious foodthey teast goodyou fell gladwrong word goooodwrong word g10dwrong word g12dwrong word g13 Dwww.helloworld.com[email protected]upper case 100% means purephp has GD module----------TEXT END-------------


Use "^" to match the beginning of the line:

#搜索以good开头的行grep ' ^good ' RegExp.txt


Match end of line with "$"

#搜索以Good结尾的行grep ' good$ ' RegExp.txt


Use the "^$" combination to match blank lines, and the following command calculates how many empty lines are in the file

#搜索空行的行数grep-C ' ^$ ' RegExp.txt


Use square brackets to match multiple possibilities

#搜索包含Good和good的行grep ' [Gg]ood ' RegExp.txt


Use ^ in square brackets to reverse the selection

#搜索一个包含ood的行, but cannot be good or good# remember that using angle brackets in square brackets means "not" grep ' [^gg]ood ' RegExp.txt


Use the "." No.

#搜索包含一个词, the word begins with G, followed by two arbitrary characters, followed by a D's line grep ' G. d ' RegExp.txt

#搜索包含一个词, the word begins with G or G, followed by two arbitrary characters, followed by a D's line grep ' [Gg]. d ' RegExp.txt

#搜索这样一些行, the line contains a word that satisfies the following criteria: #1. The first character can be either G or g#2. The second character can be either L or o#3. The third character can be any character except the newline. The fourth character must be Dgrep ' [gg][lo].d ' RegExp.txt


Use exact match

#搜索含有gold的行 # found in search results golden also matches out grep ' Gold ' RegExp.txt

#正如上例所示, general search, want to search for a row containing gold, found that golden also matches the # now needs to precisely match the line that contains the word "\<gold\>" RegExp.txt

#用 \b Effect and "\<\>" consistent grep ' \bgold\b ' RegExp.txt


Use the "*" number

#搜索这样一些行, the line contains a word that satisfies the following criteria: #1. Start with G #2.g followed by 0 to infinity o#3.0 to Infinity O back with dgrep go*d RegExp.txt


Use "*." No.

#1. Starting with G #2.g there must be a character. Finally, Dgrep ' G.*d ' RegExp.txt


Use the "-" sign

#文件中有一些拼写错误的单词, found that the O-letter in the Glod is written in the number 0, grep ' g1[0-9]d ' RegExp.txt


Use "\" to do character escapes

#搜索文件中包含域名www. Helloworld.com's line #从搜索的结果来看, here's "." is parsed as any character except for a newline # to use this point only as a character point, you need to use the escape character grep ' www.helloworld.com ' RegExp.txt

#这里将点做转义, the result of the output satisfies the expected grep ' www\.helloworld\.com ' RegExp.txt


Use the "\{\}" number

#文档中有一个单词good被拼写错了, wrote a few more o# search with the letter G beginning with two more O words grep ' go\{2,\} ' RegExp.txt

#搜索以字母g开头, the middle exactly contains 4 o words grep ' go\{4\} ' RegExp.txt


Special POSIX characters

#grep支持一些特殊的POSIX字符, for example #[:alnum:] literal numeric character #[:alpha:] literal character #[:d igit:] Numeric character #[:graph:] non-null character (non-whitespace, control character) #[:lower:]    lowercase characters #[:cntrl;] Control character #[:p rint:] non-null characters (including spaces) #[:p unct:] punctuation #[:space:] All whitespace characters (new lines, spaces, tabs) #[:upper:] Uppercase character #[:xdigit:] hexadecimal digits (0-9,a- F,A-F) #搜索以大写字母开头的行grep ^[[:upper:]] RegExp.txt

#搜索以数字开头的行grep ^[[:d igit:] RegExp.txt


Using the extended regular expression egrep

#搜索g和d之间至少有一个0的行 # "+" stands for matching the preceding characters more than 1 times (including once) Egrep ' Go+d ' RegExp.txt

#搜索g和d之间只有0个或者1个o的行 (0 or 1 times) # "? "Represents a match for the preceding character 1 times above egrep ' Go?d ' RegExp.txt

#搜索glad或gold的行egrep ' Glad|gold ' RegExp.txt

#搜索有glad或者gold的行的另外一种写法egrep ' G (la|ol) d ' RegExp.txt


Example of using regular expressions for Linux learning-grep

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.