Linux Scripting Introduction to learning Notes 14__linux

Source: Internet
Author: User
using awk for advanced text processingAwk is a tool designed for data flow. It's quite popular because you can manipulate columns and rows. AWK has many built-in functions, such as arrays, functions, and so on, which is the same as the C language.
(1) Preliminary knowledgeThe structure of the awk script is basically as follows:
$ awk ' begin{print ' start} pattern {comands} end {print ' End '} ' file
The awk command can also be read from stdin. The awk script is usually composed of 3 parts. Common statement blocks with begin,end and pattern matching options. These 3 parts are optional, and you can omit any part of the script.
(2) Actual combat exercisesThe following awk scripts are included between single or double quotes:
$ Awk ' BEGIN {statement}{statements} end {End statements} '
You can also use:
$awk ' BEGIN {statements} {statements} end {end statments} '

For example:
$ awk  ' BEGIN {i=0} {i++} end{print i} ' filename
Or
$ awk "BEGIN {i=0}{i++} end {print i}" filename
(3) Working principleThe way the awk command works is as follows. (1) Execute the statement in the begin{commands} statement block. (2) Read a line from text or stdin, and then execute Pattern{commands}. Repeat this process until all the files have been read. (3) When reading to the end of the input stream, execute the end{commands} statement block. The BEGIN statement block is executed before awk begins reading rows from the input stream. This is an optional block of statements, such as variable initialization, and table first-class statements in the PrintOut table are usually written to the BEGIN statement block. The end statement block is similar to the BEGIN statement block. The end statement block is executed immediately after awk reads all the rows from the input stream. Summary information, such as printing analysis results for all rows, is a common task implemented in end statement blocks (for example, to print the maximum number after comparing all rows). It is also an optional block of statements. The final part is the generic command in the pattern statement block. This statement block is also optional, and if you do not supply the statement block, the default execution {print}, which is to print each row read. Awk executes this block of statements for each row, which is like a while loop for reading rows, providing the corresponding statements in the loop body. Without reading a row, awk checks whether the row and the supplied styles match. The style itself can be a regular expression, a conditional statement, and an aerial matching range. If the current row matches the style, the statement in {} is executed. The style is optional. If no style is provided, then awk thinks that all rows are matched and executes the statements in {}. See examples:
(4) Supplementary content The awk command is rich in features. To gain insight into the subtleties of awk programming, you should first familiarize yourself with the important options and features of awk, and let's look at some of the important features of awk. (a): The following are some special variables for awk. NR: Represents the number of records that correspond to the current line number during execution. NF: Represents the number of fields, and the number of fields in the current row during execution. $: This variable contains the text content of the current line during execution. $: This variable contains the textual content of the first field. $: This variable contains the text content of the second field. For example:
We could print the last field in a row with the print $NF, print the penultimate field with $ (NF-1), and the other fields by analogy.
The syntax of Awk's printf () function is the same as that of the C language, and we can use this function instead of print. Take a look at some basic usages of awk, and print the 2nd and 3rd fields of each line:
$awk ' {print $2,$3} '  file

To count the number of rows in a file, use the following command:
$awk ' end{print NR} ' file
Only the end statement block is used here, and awk updates the NR to the corresponding line number for each read line. When the last row is reached, the value in NR is the line number of the last line.
Awk usually reads all the rows of a file by default, and if you just want to read the contents of a particular row, you can use the Getline function, and sometimes we need to read the first row from the BEGIN statement block. Syntax: getline var. Variable var contains the contents of a particular row. If you call Getline with no arguments, we can access the contents of the text file with $0,$1,$2. For example:


In addition, we can filter the rows that awk handles by using the filter mode for example $ Awk ' nr<5 ' # line number less than 5 $ awk ' nr==1,nr==4 ' # line number between 1 and 5 $ awk '/linux/' #包含样式的行 (you can use regular expressions to Specified) $ awk '!/linux/' # does not contain a Linux line containing a pattern
(b) Set the field boundary character the default field delimiter is a space. I can also use the-F "delimiter" to explicitly specify a delimiter:
$ awk-f: ' {print $NF} '/etc/passwd
Or
$ Awk ' BEGIN {fs= ': '} {print $NF} '/etc/passwd

In the BEGIN statement, you can set the delimiter of the output field with fs= "delimiter". (c) In the following code, ECHO generates a blank line. The variable cmdout contains the output of the command grep root/etc/passwd, which prints out the rows that contain root. The syntax for reading the output of a command into a variable output is as follows:
For example:
$ echo | awk ' {' grep root/etc/passwd | Getline cmdout; Print Cmdout "}"
By using Getline, we read the output of the external shell command into the input variable cmdout.
AWK supports an associative array with text as its index.
(d) Use loops in awk you can use a for loop in awk in the following format: for (i=0;i<10;i++) {print $i;} or for (the I in array) {print Array[i]}










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.