Linux Common Commands--awk

Source: Internet
Author: User

awk 一种编程语言

Syntax format: awk "option" ' Pattern{action} ' "File" ... awk [option] ' condition {action} ' [file] ...
Note: There must be at least one space between each element in the awk command and in subsequent options and files
Option Description:

parameter Options Explanatory notes
-F Specify character characters delimiter
-V Define or modify a variable inside the awk
$ Fetch, reference
$ First column
$ Entire row
$NF Last line
$ (NF-1) Penultimate line
Rs Delimiter for each record read in (similar to line break \ n)
Nr Line number, number of records
ORS Delimiter for output time

Relational operators

operator meaning Example
< Less than X < y
<= Less than or equal X <= y
== Equals x = = y
!= Not equal to X! = y
>= Less than X < y
< Greater than or equal X >= y
> Greater than X > Y
~ Match Regular expression x~/y/
!~ does not match the regular expression X!~y

Case one:
Introduction to the AWK environment

Case TWO:
awk Execution Process:

    1. awk reads the first line of content
    2. to determine whether the conditions in the pattern are met (for example: nr>=2)
      A. If the match executes the corresponding action (e.g. {print $})
      B. If the condition is not matched, continue reading the next line
    3. li> continue to the next line
    4. Repeat procedure 1-3, know the last line read (Eof:end of file)

      Simple description of the record:

    5. How many steps does an elephant put in a refrigerator? Open the refrigerator door, put the elephant in, close the refrigerator door.
    6. to debug your awk command with NR, NF, $ number.
    7. NR holds the number of each record (line number) when the new line is read automatically +1
    8. RS is the delimiter for the record, and a simple understanding is that you can specify the end flag for each record.
    9. use RS instead of \ n
    10. rs action means the end of a record
    11. FS marks the end of each zone

      AWK basic structure
      awk begin{coms}/pattern/{ COMs} end{coms}
      Start the module,/Who/{to do}, End module

    12. begin and end modules can only have one, begin{}begin{} or end{}end{}
    13. who does the module? can have multiple (for example: Nr==2{print $ nr==5{print)
      Case three:
      parameter Rs,nr example


      Description: When RS is null, print the output record number in the line hand and print out each row The contents of the
      Regular expression Simple example

      Note: $1~ indicates a match to the first column (the first region), o{1,2} indicates that the condition has one or two o,{print nr,$1,nf} for printing line numbers, first column, last column
      Case FOUR:
      Use awk to remove the corresponding port number,

      The number of empty lines in the statistics file

      New file, with a space delimiter, area 3 greater than 15, how many lines



      Use AWK implements a value of 1 and 100

Linux common Commands--awk

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.