Awk processes command line parameters

Source: Internet
Author: User

Awk processes command line parameters

ARGV awk can obtain command line parameters from the built-in array ARGV, including the command awk. However, none of the options passed to the awk are included. The subscript of the argv array starts from 0.
ARGC is a built-in variable that contains the number of command line parameters.

Example

$ cat argvs# Scriptname:argvsBEGIN{for(i=0; i printf("argv[%d] is %s\n",i,ARGV[i])}printf("The number of arguments, ARGC=%d\n",ARGC)}$ awk -f argvs datafileargv[0] is awkargv[1] is datafileThe number of arguments, ARGC=2

Description
The for loop first sets I to 0, then tests whether it is smaller than the number of command line parameters (ARGC), and then displays each parameter in sequence using the printf function. After processing all the parameters, the final printf statement is used to output the number of parameters ARGC. This example shows that awk does not regard the command line option as a parameter.

Example

$ awk -f argvs datafile "Peter Pan" 12argv[0] is awkargv[1] is datafileargv[2] is Peter Planargv[3] is 12The number of arguments,ARGV=4

Note: Like the previous example, all parameters are printed. The nawk command is treated as the first parameter, while the-f option and script file name (argvs) are excluded.

Example

$ cat datafile5Tom Jones:123:03/14/56Peter Pan:456:06/22/58Joe Blow:145:12/12/78Santa Ana:234:02/03/66Ariel Jones:987:11/12/66$ cat arging.sc# Scriptname:arging.scBEGIN{FS=":";name=ARGV[2]print "ARGV[2] is "ARGV[2]}$1 ~ name {print $0}$ awk -f arging.sc datafile5 "Peter Pan"ARGV[2] is Peter PanPeter Pan:456:06/22/58nawk: can't open Peter Paninput record number 5, file Peter Pansource 1ine number 2

Note:
1. In the BEGIN block, the value of ARGV [2], that is, Peter Pan, is assigned to the variable name.
2. Peter Pan is printed out. However, after processing the datafile and disabling it, awk tries to open Peter Pan as an input file. Awk uses all parameters as input files.

Example

$ cat arging2.scBEGIN{FS=";";name=ARGV[2]print "ARGV[2] is "ARGV[2]delete ARGV[2]}$1 ~ name {print $0}$ awk -f arging2.sc datafile "Peter Pan"ARGV[2] is Peter PanPeter Pan:456:06/22/58

Note: awk uses the ARGV array element as the input file. When the awk runs out of a parameter, it moves it left, and then processes the next one until the ARGV array becomes empty. If a parameter is deleted immediately after it is used, it will not be processed as the next input file.

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.