awk regular expression, regular operator detailed description

Source: Internet
Author: User

Preface: Using awk as a text-processing tool, regular expressions are indispensable. To master the use of regular expressions for this tool. In fact, we don't have to learn its regular expressions alone. Regular expressions, like a programming language, have their own grammatical rules already expressed. For different tools, in fact most of the meaning of the same. Regular expressions are used in many Linux text-processing tools (Awk,sed,grep,perl). There are actually only 3 types. Detailed reference:Linux shell Regular expression (bres,eres,pres) difference comparison . As long as some tools are part of some kind of regular expression. Then its grammatical rules are basically the same. Through that article, we know that the regular expression of awk belongs to: an extended regular expression (Extended Regular expression is also called Extended RegEx abbreviation EREs).

First, the awk Extended Regular expression (ERES) Basic Expressions Symbol Introduction

character function
+ Specifies that if one or more characters or the specific value of an extended regular expression ( before the + (plus) sign) is in the string, the string matches. Command line:

awk '/smith+ern/' testfile

Prints to standard output any record that contains the character Smit, followed by one or more H characters, and a string that ends with the character Ern . The output in this example is:

Smithern, Harry Smithhern, Anne

? Specifies that if 0 or one character or the specific value of the extended regular expression (before the? Question mark) is in the string, the string matches. Command line:

awk '/smith?/' testfile

Prints all records containing the character Smit, followed by an instance of 0 or one H character, to the standard output. The output in this example is:

Smith, Alan Smithern, Harry Smithhern, Anne Smitters, Alexis

| Specifies that if any one of the strings separated by | (vertical line) is in the string, the string matches. Command line:

awk '/allen | Alan/' Testfile

Prints all records containing the string Allen or Alan to standard output. The output in this example is:

Smiley, Allen Smith, Alan.

( ) Group strings together in regular expressions. Command line:

awk '/A (LL)? (NN)? e/' Testfile

Prints all records with the string ae or alle or Anne or Allnne to standard output. The output in this example is:

Smiley, Allen Smithhern, Anne

{m} Specifies that the string matches if the exact value of the M pattern is in a string. Command line:

awk '/l{2}/' testfile

Print to standard output

Smiley, Allen

{m,} Specifies that if at least the concrete value of the M pattern is in a string, the string matches. Command line:

awk '/t{2,}/' testfile

Print to standard output:

Smitters, Alexis

{m, N} m  and  n  (contains M and n) the specific values of the patterns in the string (where m<= n), the string matches. Command line:

awk '/er{1, 2}/' testfile

Print to standard output:

smithern, Harry Smithern, Anne Smitters, Alexis

[String] Specifies that the regular expression matches any character specified by the String variable in square brackets. Command line:

awk '/sm[a-h]/' testfile

Prints all records with SM followed by any character in alphabetical order from a to H to standard output. The output of this example is:

Smawley, Andy.

[^ String] The [] (square brackets) and the ^ (caret) at the beginning of the specified string indicate that the regular expression does not match any of the characters in the square brackets . So, the command line:

awk '/sm[^a-h]/' testfile

Print to standard output:

Smiley, Allen Smith, Alan Smithern, Harry Smithhern, Anne Smitters, Alexis

~,!~ Represents a conditional statement that specifies that a variable matches a regular expression (tilde) or does not match (tilde, exclamation point). Command line:

awk ' $ ~/n/' testfile

Prints all records with the first field containing the character N to the standard output. The output in this example is:

Smithern, Harry Smithhern, Anne

^ Specifies the beginning of a field or record. Command line:

awk ' $ ~/^h/' testfile

Prints all records of character H as the first character of the second field to standard output. The output in this example is:

Smithern, Harry.

$ Specifies the end of a field or record. Command line:

awk ' $ ~/y$/' testfile

Prints all records of character y as the last character of the second field to standard output. The output in this example is:

Smawley, Andy Smithern, Harry.

. Period Represents any one character other than the terminal wrap character at the end of the white space. Command line:

awk '/A. e/' Testfile

Prints all records with characters a and e separated by two characters to standard output. The output in this example is:

Smawley, Andy Smiley, Allen Smithhern, Anne

* (asterisk) Represents 0 or more arbitrary characters. Command line:

awk '/a.*e/' testfile

Prints all records that have characters a and E separated by 0 or more characters to standard output. The output in this example is:

Smawley, Andy Smiley, Allen Smithhern, Anne Smitters, Alexis

\ (back slash) The escape character. The escape character removes any special meaning of the character when it precedes any character that has a special meaning in an extended regular expression. For example, the command line:

/a\/\//

Will match the pattern A//, because the backslash negates the slash as the usual meaning of the regular expression delimiter. To specify the backslash itself as a character, use a double backslash. For more information about backslashes and their use, see the following about escape sequences.

Compared to Pers, there are some combinations of type notation: "\d,\d,\s,\s,\t,\v,\n,\f,\r" other functions are basically the same. Our common software: Javascript,.net,java supports regular expressions, basically: EPRs types.

Second, awk common call Regular Expression method

    • In the awk statement:

awk '/reg/{action} '

The/reg/is a regular expression that can be fed into the.

    • AWK regular operation statement (~,~! equals!~)

[[email protected] ~]$ awk ' begin{info= "This is a test"; if (info ~/test/) {print "OK"}} '
Ok

    • Awk built-in using regular expression functions

Gsub (Ere, REPL, [in])

Sub (Ere, REPL, [in])

Match (String, Ere)

Split (String, A, [Ere])

Detailed function use, can be referenced:Linux awk built-in function details (instance)

By the above, I wonder if you have a clearer idea of the awk regular expressions. What is the problem can communicate with me!

awk regular expression, regular operator detail (GO)

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.