Linux Commands: Text Processing tool awk detailed

Source: Internet
Author: User
Tags posix line editor

awk Introduction to Commands:

awk is a powerful text analysis tool , and Typically,awk is used for each line of the file as a processing unit. awk processes the text by executing the corresponding command for each line that receives the file.

1 . Command format:

awk ' pattern {action} ' {filenames}

where pattern represents what AWK looks for in the data, and the action is looking for To match the content, the

executes a series of commands. Curly braces ({}) do not need to always appear in the program, but they are used in accordance with specific

mode to group a series of instructions. pattern is the regular expression to be represented, surrounded by slashes.

There are several ways to invoke awk:

command line mode:

awk [-ffield-separator] ' commands ' input-file (s)

where commands is the processing command for AWK, the-F indicates that the area delimiter is optional. Input-file (s)

is a text file that needs to be processed. In awk , each line of a file, separated by a zone separator, is called

An area. In general, if you do not specify the- f Zone delimiter, the default Zone delimiter is a space.

Shell script mode:

Insert all the awk commands into a file and use the awk program to execute, and then awk command interpreter as

The first line of the script to invoke by typing the script name. Equivalent to the first line of the Shell script:#!/bin/bash can

Replace with:#!/bin/awk3. Insert all the awk commands into a separate file, and then call :

awk-fawk-script-file Input-file (s) wherein,-f option: Loads the awk script in the awk-script-file ,

Input-file (s): is a text file that needs to be processed .

2. Command function:

awk is a powerful text analysis tool, the line editor, compared to grep 's lookup,sed 's editing, andawk is particularly powerful in its analysis of data and generating reports. To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.

3 . Command parameters:

· - f FS or--field-separator FS
Specifies the input file delimiter,FS is a string, or is a regular expression, such as- F:.

· - v var=value or--asign var=value
Assigns a user-defined variable.

·         -f Scripfile or--file scriptfile
reads awk commands from the script file.

·         - MF nnn AND-MR nnn
to nnn value set intrinsic limit, -MF option limits the maximum number of blocks assigned to -MR option limits the maximum number of records. These two functions are bell Lab Edition awk extended functionality, in standard .

· -W compact or--compat,-w traditional or--traditional
Run awkin compatibility mode. So gawk 's behavior is exactly the same as the standard awk , and all awk extensions are ignored.

· -W copyleft or--copyleft,-w copyright or--copyright
Print a brief copyright message.

· -W Help or--help,-w usage or--usage
Print all awk options and a short description of each option.

· - w lint or--lint
Print warnings for structures that cannot be ported to traditional Unix platforms.

·         -W Lint-old or--lint-old
prints a warning about a structure that cannot be unix platform porting.

·         -W POSIX
turns on compatibility mode. However, the following limitations are not recognized: /x , function keyword, func , the Exchange code sequence and when is a space, the new row is used as a domain delimiter, and the operator **= cannot replace ^ and ^= ; fflush The is not valid.

·         -W Re-interval or--re-inerval
allows the use of interval regular expressions, refer to (grep posix character class ) , such as the bracket expression [[:alpha:]] .

· - w source Program-text or--source Program-text
Use Program-text as the source code, which can be mixed with the- F command.

· - w version or--version
Print the version of the Bug report information.

This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1945750

Linux Commands: Text Processing tool awk detailed

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.