Use shell commands in the awk Program

Source: Internet
Author: User
The awk program allows calling shell commands and provides pipelines to solve the problem of data transmission between awk and the system. Therefore, awk is easy to use system resources. There are two main methods:

A. Syntax] awk output command | "commands accepted by shell" (for example, Print $1, $2 | "sort-K 1 ")

[B. Syntax] "commands accepted by shell" | awk input command (for example, "ls" | Getline)

Note:

The awk input command only has one Getline command.

The awk output Commands include print and printf.

In the syntax, the data output by awk will be transferred to the shell for processing by the Shell Command. in the preceding example, the data output by print is sorted by the Shell Command "sort-K 1" and then sent to the screen (stdout ).

In the previous awk program, "Print $1, $2" may be executed multiple times, and the output results will be saved in pipe first. When the program ends, "Sort-K 1 ".

Note the following two points:

1. No matter how many times Print $1 and $2 are executed, the execution time of "sort-K 1" is "awk program end time ",

2. "sort-K 1" is executed once ".

 

In the B syntax, awk will first call the shell command. the execution result is sent to the awk program through pipe. In the preceding example, awk first let the shell execute "ls". After the shell executes the command, it stores the result in pipe. The awk command Getline then reads data from pipe.

Note the following when using this syntax: in the preceding example, awk "calls shell to execute" ls "immediately, and the execution frequency is one time.

Getline may be executed multiple times (if pipe contains multiple rows of data ).

Example: sort the process PID

PS-Ef | awk '{print $2 | "sort-n "}'

Store the pid value of the awk result to pipe and send it to "sort-n" at the end of the program for sorting and output it to the standard output, in addition, you can call close ("sort-N") to close the pipe before "sort-N" and immediately submit "sort-N" to shell for execution.

Example: print the number of online users

Awk 'in in {While ("who" | Getline) n ++; print n }'

Print the current number of online users

Note: awk does not have to process files. Begin will be executed once before any input files are enabled. awk regards the string before the "|" pipeline symbol as a shell command, the command is sent to shell, and the execution result is sent to the awk program by pipe. The Getline is the input command provided by awk.

Syntax

Where to read data

After the data is read

Getline var <File

Specified file

VaR (if VaR is omitted, it indicates that it is placed in $0)

Getline VaR

Pipe variable

VaR (if VaR is omitted, it indicates that it is placed in $0)

When pattern is begin or end, Getline reads data from stdin. Otherwise, it reads data from the data file being processed by awk.

Getline reads a row of data at a time. If the read succeeds, return 1. If the read fails, return-1. If the file ends (EOF), return 0 (false)

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.