The Shell's awk little memory

Source: Internet
Author: User

Awk is a powerful text analysis tool, with the search for grep and the editing of SED, which is especially powerful when it comes to analyzing data and generating reports. To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.

A complete awk statement is: awk ' [patten]{action} ... ', where pattern defaults to 1,action default to

{print}.

Then awk ' 1 ' is the complete notation of awk ' 1{print} '; In the same vein, awk ' {print} ' is the complete notation of awk ' 1{print} '.


awk '! a[$0]++ ' How to understand?

This is a very classic awk statement that duplicates


awk command

Use

Find the rows in the file that match the pattern, and then perform a specific action on them.

Grammar

awk [-F Ere] [-v assignment] ... {-F Programfile | ' Program '} [[File ...

| Assignment ...] ] ...

There are four types of patterns used in the awk command language syntax:

Regular expressions

Relational expressions

Combination of patterns

BEGIN and END patterns


Length (String): Returns the lengths of the string.

[[email protected] ~]# awk ' BEGIN {print length ("Qiruyi")} '

6

Enter the following line of commands:

awk '/smi/' testfile

All records containing the specific value of the SMI string will be printed to the standard output.

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 ending with the character Ern


| Specifies that if any one of the strings separated by | (vertical line) is in a 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.


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

awk '/t{2,}/' testfile

Print to standard output:

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.


^ 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.


\ (backslash) escape character. The escape character removes the character when it precedes any character that has a special meaning in an extended regular expression

of any special meaning. For example, the command line:

/a\/\//

will be associated with mode A//

Match, because the backslash negates the usual meaning of the slash as a regular expression delimiter. To specify the backslash itself as a character, use the double

Backslash.


Escaped sequence of recognition

The awk command identifies most of the escape sequences used in the C language conventions, as well as several escape sequences that the awk command itself serves as special characters. Turn

The sequence of righteousness is:

The character that the escape sequence represents

\ "\" (double quotation marks)

\//(Slash) character

\DDD a character whose encoding is represented by a 1, 2, or 3-bit octal integer, where d represents an octal digit

\\\ (backslash) character

\a Warning Character

\b Backspace character

\f Page Break characters

\ nthe newline character (see note below)

\ r Enter character

\ t-jump-lattice character

\v Vertical Jump Lattice


The six conditional statements in the language are:

If you need the following syntax:

if (Expression) {Statement} [else Action]

The while requires the following syntax:

while (Expression) {Statement}

For requires the following syntax:

for (Expression; Expression; Expression) {Statement}

Break causes the exit program to loop when the break statement is used for a while or for statement.

Continue when the continue statement is used for a while or for statement, the program loops to the next iteration.


To display all lines between the word start and stop, including "Start" and "Stop", enter:

awk '/start/,/stop/' Chapter1

To run an awk command program Sum2.awk that handles file Chapter1, enter:

Awk-f Sum2.awk Chapter1


To print the first two fields in reverse order, enter:

awk ' {print $, $ ' chapter1


How do I print single double quotes?

awk ' BEGIN {print ' single quote--\047 ";p rint" double quote--\042 "} '


1. To remove duplicates, this does not say much, just give the code:

1. awk '!a[$0]++ ' file (s)

2. awk '! ($ A) {a[$0];p rint} ' file (s)


The Shell's awk little memory

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.