Awk command line parameters

Source: Internet
Author: User

Awk command line parameters

Here is an example.


$ Cat datafile
Northwest NW Joel Craig 3.0. 98 3 4
Western WE Sharon Kelly 5.3. 97 5 23
Southwest SW Chris Foster 2.7. 8 2 18
Southern SO May Chin 5.1. 95 4 15
Southeast SE Derek Johnson 4.0. 7 4 17
Eastern EA Suan Beal 4.4. 84 5 20
Northeast ne tj Nicholas 5.1. 94 3 13
North NO Val Shultz 4.5. 89 5 9
Central CT Sheri Watson 5.7. 94 5 13

$ Cat argvs. SC
# Testing command-line arguments with ARGV and ARGC using a for 1oop.
BEGIN {
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. SC datafile
Argv [O] is nawk
Argv [l] is datafile
The number of arguments, ARGC = 2

Description: The BEGIN block contains a for loop for processing command line parameters. ARGC is the number of parameters, and ARGV is an array containing actual parameters. Nawk does not regard options as parameters. In this example, only the nawk command and the input file are valid parameters.

Example

$ Awk 'in in {name = ARGV [1]}; \
$0 ~ Name {print $3, $4} '"Derek" datafile
Awk: can t open Derek
Source line number 1

$ Awk 'in in {name = ARGV [1]; delete ARGV [1]}; \
$0 ~ Name {print $3, $4} '"Derek" datafile
Derek Johnson

Description
1. In the BEGIN block, the name "Derek" is assigned to the variable name. In the next mode block, awk tries to open "Derek" as the input file, and the result fails.

2. After "Derek" is assigned to the variable name, awk will delete ARGV [1. When you enter the mode block, awk does not try to open "Derek" as the input file, but opens the file datafile.

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.