Awk for Unix text processing tools

Source: Internet
Author: User
For Unix text processing tools, the command input in the awkUnix command line is text, and the output is also text. Therefore, Mastering Unix text processing tools is an important capability. Awk is a common Unix text processing tool. It is named by its inventor (Aho, Weinberger, and Kernighan... for Unix text processing tool awk Unix command lines, the input command is text, and the output is also text. Therefore, Mastering Unix text processing tools is an important capability. Awk is one of the commonly used Unix text processing tools. It is named after the first character of its inventor (Aho, Weinberger, and Kernighan, it is a text data processing tool that checks input based on pattern matching and then processes the expected matching results and outputs them to the screen. 1. awk command format: awk 'mode {operation}' file 1 file 2 ...... The awk command works like this: for each input file, check it line by line. if this row matches the 'mode' part of the awk command parameter, the operation in the '{operation}' section of the command for this row. The following is a simple example: [plain] $ cat awk_test.txt 1 a, B, d, f 2 B alsdjf, apple, kdjf 3 c 163.2.201.1 4 d www.google.com 5 e http://blog.csdn.net/xia7139 $ Awk 'NR = 1 {print} 'awk_test.txt 1 a, B, d, f example, use awkcommand to output the first line of the awk_test.txt file, the NR used in the command mode is the built-in variable of the awk command, representing the row number of the file. In this way, all rows with a row number of 1 can be printed and output. 2. definitions of common built-in Variable variables the FS field of the row being processed by NR is separated, the default value is space or TAB $ n. the nth field of the current processing row $0. all content of the current processing row. here we need to explain the meaning of the field: in the use of awk, the field separator separates a line of the file into each part. each part is called a field and 1st fields are separated from left to right ,......, The nth field. the nth field indicates the entire line. If the field separator is not specified, it is a space or tab by default. 3. several examples and the examples below output are the operations on the awk_test.txt file in the above text. [Plain] operations by row number: (1) print 1-3 rows of the file $ awk 'NR = 1, NR = 3 {print} 'awk_test.txt 1 a, B, d, f 2 B alsdjf, apple, kdjf 3 c 163.2.201.1 (2) print row 1st and row 3rd of a file $ awk 'NR = 1 | NR = 3 {print} 'awk_test.txt or $ awk' (NR = 1) | (NR = 3) {print} 'awk_test.txt 1 a, B, d, f 3 c 163.2.201.1 (3) print only odd rows (even rows) $ awk '(NR % 2) = 1 {print}' awk_test.txt 1 a, B, d, f 3 c 163.2.201.1 5 e http://blog.csdn.net/xia7139 $ Awk '(NR % 2) = 0 {print}' awk_test.txt 2 B alsdjf, apple, kdjf 4 d www.google.com use a regular expression: (1) print the row containing 2 $ awk '/2/{print}' awk_test.txt 2 B alsdjf, apple, kdjf 3 c 163.2.201.1 (2) print the row ending with com $ awk '/com $/{print}' awk_test.txt 4 d www.google.com specified to be separated and output the specified field: (1) print the first and third fields in line 1-3 $ awk 'NR = 2, NR = 3 {print $1, $3} 'awk_test.txt 1 a, B, d, f 2 alsdjf, apple, kdjf 3 163.2.201.1 (2) specify the separator ., output the third field of the second line of csdn and the whole line $ awk-F. '$2 = "csdn" {print $3, $0}' awk_test.txt net/xia7139 5 e http://blog.csdn.net/xia7139

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.