Use awk for text processing

Source: Internet
Author: User

 

Awk must be known to anyone who has experience processing too much text. It is a scripting language that is easy to use to process data in a certain format. The following describes common methods of awk.

If you want to learn systematically, see http://doc.lazyhack.net/awk.htm

 

1 awk built-in Variables

The built-in field variables of awk and their meanings are as follows:

Field variable

Description

$0

A string whose content is the data row currently read by awk.

$1

$0 indicates the data of the first field.

$2

Data of the Second Field on $0.

...

Other analogy

Built-in Variables

Description

NF (number of fields)

Is an integer. The number of fields in the value table $0.

NR (number of records)

The value table awk has read the number of data rows.

Filenameawk

The file name of the data file being processed.

For example, awk reads the first data row from the data file EMP. dat.

"A125 Jenny 100 210,ProgramMedium:

The value of $0 will be "a125 Jenny 100 210"

The value of $1 is "a125"

$2: "Jenny"

$3: 100

$4: 210

$ The value of NF is 4.

$ Nr value: 1

$ Filename: "EMP. dat"

 

2 awk Workflow

AwkWorkflow:

When awk is executed, it performs the following four steps repeatedly.

    1. Automatically reads a data row from the specified data file.
    2. Values of built-in variables related to automatic update, such as NF, NR, $0...
    3. Execute all the pattern {actions} commands in the program in sequence.
    4. When all pattern {actions} in the program is executed, if there is still unread data in the data file, perform steps 1 to 4 repeatedly.

Awk will automatically repeat the above four steps, the user does not have to write this loop in the program ).

3. Example

Two Methods: A. $ awk-F awkProgram file name Data File Name$ Awk-F pay1.awk EMP. dat (-F indicates specifying the awk program file)

B.$ Awk '{print $2, $3 * $4}' EMP. dat

For example, if the file EMP. dat exists, the data is as follows:

A125 Jenny 100 210

A341 Dan 110 215

P158 max 130 209

P148 John 125 220

A123 Linda 95 210

Run $ awk '{print $2, $3 * $4}' EMP. dat

Output:

Jenny 21000

Dan 23650

Maxcompute 27170

John 27500

Linda 19950

Run $ awk '{printf ("% 6 s work hours: % 3d pay: % 5d \ n", $2, $3, $3 * $4)}' EMP. dat

Output:

Jenny work hours: 100 pay: 21000

Dan work hours: 110 pay: 23650

Max working hours: 130 pay: 27170

John work hours: 125 pay: 27500

Linda work hours: 95 pay: 19950


3 awk Parameters

-V: Specifies the awk-v a = 1' {XXX} 'variable in the program. You can use the variable.

-F specifies the program's external file awk-F pro. awk data

-F: Specifies the awk-f \ t' {XXX} 'delimiter as \ t. The default Delimiter is space.


4 awk advanced usage-array

First, create a data file,And namedReg. dat.This is a student registration document;The first column is the student name.,The courses subsequently taken by the student.

Mary o.s. Arch. discrete

Steve d.s. algorithm arch.

Wang discrete graphics o.s.

Lisa graphics a. I.

Lily discrete algorithm

YesCount the number of workers in each subject. Execute the following:

Create the following program and name it course. awk:

{For (I = 2; I <= NF; I ++) number [$ I] ++}

End {for (course in number) printf ("% 10 S % d \ n", course, number [course])}

Run the following command:

$ Awk-f course. awk Reg. dat

The execution result is as follows:

Graphics 2

O.s. 2

Discrete 3

A. I. 1

D.s. 1

Arch. 2

Algorithm 2

 


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.