Linux FAQ 4: awk

Source: Internet
Author: User
Linux, awk 1. awksed processes files in behavior units. awk is better than sed in that it can not only process files in behavior units but also in columns. The default line delimiter of awk is line feed, and the default column separator is consecutive spaces and tabs, but the line separator and column separator are separated... linux, awk 1. awksed processes files in behavior units. awk is better than sed in that it can not only process files in behavior units but also in columns. The default line delimiter of awk is line feed, and the default column separator is consecutive spaces and tabs, but the line separator and column separator can be customized, for example, each line of the/etc/passwd file has several fields separated by:, you can redefine the column separator of awk as follows: and process the file as a column. Awk is actually a very complex scripting language, and has branch and loop structures like C, but its basic usage is similar to sed. The basic form of the awk command line is: awk option 'script' file1 file2... awk option-f scriptfile file1 file2... like sed, the files processed by awk can be redirected by standard input or passed in as command line parameters. the edit command can be passed in directly as command line parameters, you can also use the-f parameter to specify a script file. the editing command format is:/pattern/{actions} condition {actions}. similar to sed, pattern is a regular expression, actions is a series of operations. The awk program reads the files to be processed in one row. if a row matches pattern or the condition is met, the corresponding actions is executed. If an awk command contains only the actions part, actions acts on each row of the file to be processed. For example, the content of the file testfile indicates the inventory of a store: ProductA 30 ProductB 76 ProductC 55 prints the second column of each row: $ awk '{print $2 ;} the 'testfile307655 automatic variables $1 and $2 indicate the first and second columns respectively. they are similar to the location parameters of the Shell script, while $0 indicates the entire row. For example, if the inventory of a product is less than 75, you need to order at the end of the line: $ awk '$2 <75 {printf "% s \ t % s \ n", $0, "REORDER" ;}$ 2 >=75 {print $0;} 'testfileproducta 30 REORDERProductB 76 ProductC 55 REORDER it can be seen that awk has a very similar printf function with C language. The condition part of the awk command can also be two special condition-BEGIN and END. for each file to be processed, actions after BEGIN will be executed once before processing the entire file, actions after END is executed once after the entire file is processed. The awk command can use variables like the C language (but does not need to define variables), such as counting the number of empty lines in a file $ awk '/^ * $/{x = x + 1 ;} END {print x;} 'testfile is the same as the Shell environment variable. some awk variables have predefined special meanings: the file name of the current input file of the built-in variable FILENAME commonly used by awk, this variable is the row number of the current row of the read-only NR. this variable is read-only, and R represents the number of columns in the current row of recordNF. this variable is read-only, F represents the column delimiter of fieldOFS output format. the default is the column delimiter of the space FS input file. the default is the consecutive space and the row delimiter of the TabORS output format, the default value is the line delimiter of the RS input file. the default value is a line break, for example, printing the user account list in the system $ awk 'In in {FS = ":" }{ print $1 ;} '/etc/passwdawk can also use the if/else, while, and for control structures like the C language
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.