The basics of AWK in perl

Source: Internet
Author: User
Tags processing text

Perl Basics of AWK in LanguageI. AWK and perl
 
 
  1. 1. Perl, we are no stranger to it so far. It plays a powerful role in web programming. For every web developer, its importance is self-evident,
  2. Perl uses the features of C, sed, awk, shell scripting, and many other programming languages.
  3. The most important feature is that it integrates the Regular Expression Function and a huge third-party code library CPAN.
  4. In short, Perl is as powerful as C and as convenient as script descriptive languages such as awk and sed. From this perspective, AWK plays an important role in perl.
  5. Here, we will not focus on perl statements, but introduce the basic content of AWK, an important component,
  6. Let's unveil the secrets of awk in perl!
Ii. AWK Introduction
 
 
  1. What is awk? For beginners, this is synonymous with a pattern. In fact, awk is collectively referred to as three creators Aho, Weinberger, and Kernighan,
  2. In short, AWK is a programming language tool for processing text.
  3. What can wk do? It can be used for style loading, flow control, mathematical operators, process control statements, and even built-in variables and functions.
  4. It has almost all the exquisite features of a complete language, so its three creators have formally defined it as "style scanning and processing language ".
  5. It allows you to create short programs that read input files, Sort data, process data, perform calculations on input, and generate reports. There are countless other functions.
3. AWK text processing process:
 
 
  1. When processing a file, awk first scans each row of the file to find the pattern that matches the content given in the command line.
  2. If the Matching content is found, perform the next programming step. If no matching content is found, process the next row.
4. Use of AWK
 
 
  1. Syntax format of AWK:
  2. Awk [options] 'scritpts 'filename
  3. Or awk [options] 'partern [actions] 'filename
  4. [Options]:
  5. -F: Specifies the delimiter used by awk to process text rows.
For example

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1300491920-0.png "alt =" "/>

 
 
  1. Three built-in ofs fs nf variables of common awk
  2. -FS: Field Separator
  3. For example
 
 
  1. Awk  -v FS=: ‘{print $2}’ /etc/passwd  

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/13004ac6-1.png "alt =" "/>

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/13004941D-2.png "alt =" "/>

 
 
  1. -V OFS = specifies the character delimiter used to define the awk output.
  2. For example: Awk-v OFS = # '{print $1, $2}'/etc/passwd

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1300493O9-3.png "alt =" "/>

 
 
  1. Note: In the previous example, $1 and $2, $1, $2 are output in the form of $1 and $2 respectively. $1 $2 is output after merging $1 and $2
  2. For example

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/13004943K-4.png "alt =" "/>

 
 
  1. NF indicates the total number of fields when awk processes a row.
  2. For example, output the last field of a row
  3. Awk '{print $ NF}'/etc/passwd

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1300494A3-5.png "alt =" "/>

 
 
  1. Note: The last and third fields of a row are displayed.

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/13004aG0-6.png "alt =" "/>

 
 
  1. Use of printf
  2. Format: printf format item1, item2 ,......
  3. Note:
  4. 1: The maximum difference from the print command is that printf needs to specify the format
  5. 2: format is used to specify the output format of each item
  6. 3: The printf statement does not automatically print the linefeed \ n
  7. 4: The format indicators start with %, followed by a character
  8. Format:
  9. % C: ASCII code of the characters displayed
  10. % D, % I: displays the decimal INTEGER OF THE character
  11. % E, $ E scientific notation to show values
  12. % F: displays floating-point numbers.
  13. % G, % G: The value is displayed in scientific notation or floating point format.
  14. % S display string
  15. % U show unsigned integer
  16. % Display % itself
  17. In addition, you can add modifiers for format.
  18. N: display width
  19. -: Left alignment
  20. +: Displays numeric symbols.
  21. For example:

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1300491A0-7.png "alt =" "/>

 
 
  1. Use output redirection in Awk
  2. Print items> output-file
  3. Print items> output-file
  4. Print items | command
Awk OPERATOR:
 
 
  1. Arithmetic Operators:
  2. -X: negative value
  3. + X: convert to a value;
  4. X ^ y:
  5. X ** y: Power
  6. X * y: Multiplication
  7. X/y: Division
  8. X + y:
  9. X-y:
  10. X % y:
  11. String OPERATOR:
  12. There is only one, and it does not need to be written to implement string connection;
  13. Value assignment operator:
  14. =
  15. + =
  16. -=
  17. * =
  18. /=
  19. % =
  20. ^ =
  21. ** =
  22. ++
  23. --
  24. It should be noted that, if a mode is =,/=/may cause a syntax error, it should be replaced by/[=;
  25. Boolean Value
  26. In awk, any non-0 value or non-null string is true. Otherwise, it is false.
 
 
  1. Comparison operator:
  2. X <y True if x is less than y.
  3. X <= y True if x is less than or equal to y.
  4. X> y True if x is greater than y.
  5. X> = y True if x is greater than or equal to y.
  6. X = y True if x is equal to y.
  7. X! = Y True if x is not equal to y.
  8. X ~ Y True if the string x matches the regexp denoted by y.
  9. X !~ Y True if the string x does not match the regexp denoted by y.
  10. Subscript in array True if the array has an element with the subscript
  11. Logical Relationship Between Expressions:
  12. &&
  13. |
  14. Conditional expression:
  15. Selector? If-true-exp: if-false-exp
  16. Selector: conditional expression
  17. If-true-exp: statement
  18. If-false-exp: statement
  • Next we will focus on the usage of the awk mode.
 
 
  1. Awk mode:
  2. Awk [options] 'parten ''print .... 'Filename'
  3. Parten:
  4. 1: The regular expression format is/regular expression/
  5. 2: conditions that are met when the expression value is not 0 or a non-null character, for example, $1 ~ /User/or $1 = 'user'
  6. Use operators ~ (Matching) and ~ ! (Mismatch)
  7. For example:
650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1300493Y6-8.png "alt =" "/>

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/13004911N-9.png "alt =" "/>

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/130049D27-10.png "alt =" "/>

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/13004951M-11.png "alt =" "/>

 
 
  1. The format of the matching range is pat1, and pat2 pat indicates the pattern.
  2. Special BEGIN/END format
  3. BEGIN: the processing action before awk executes the action
  4. Specifies the action that occurs before the first input record is processed. You can set global variables here.
  5. END: The action executed after awk executes the action.
  6. Action that occurs after the last input record is read
  7. For example:
  8. Awk-F: '$ NF ~ /Bash/{print "username bash"; printf "%-10 s % s \ n, $1, $ NF"} '/etc/passwd

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/13004ab2-12.png "alt =" "/>

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1300494F0-13.png "alt =" "/>

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1300493228-14.png "alt =" "/>

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1300491621-15.png "alt =" "/>

Null Mode means that all rows cannot be described due to the limited number of characters in length. If there are any errors, I hope you can correct them and discuss them more. Thank you. Next, we will continue to use the awk loop control statement,

This article from the "Good Hope" blog, please be sure to keep this source http://haicang.blog.51cto.com/2590303/949060

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.