ARGC and ARGV

The ARGC and ARGV variables are used to pass arguments to the AWK script from the command line.

ARGC specifies the total number of arguments passed to the AWK script on the command line. It always has a value of 1 or more, as it counts the program name as the first argument.

The AWK script filename specified using the -f option is not counted as an argument. If we declare any variable on the command line, it is counted as an argument in GAWK:

$ awk 'BEGIN { print "No of arguments =", ARGC }' one two three four

The output of the execution of the preceding command is as follows:

No of arguments = 5 

ARGV is an array that stores all the arguments passed to the AWK command, starting from index 0 through to ARGCARGV[0] always contains AWK.

In the following example, we show how ARGV and ARGC work. Here, ARGV[0] will store the AWK:

$ vi arguments.awk

BEGIN {
print "Total no. of arguments =", ARGC
for ( i=0; i<ARGC; i++ )
printf "ARGV[%d] = %s ", i, ARGV[i]
}


$ awk -f arguments.awk a1 a2 a3 a4

The output of the execution of the preceding code is as follows:

Total no. of arguments = 5
ARGV[0] = awk
ARGV[1] = a1
ARGV[2] = a2
ARGV[3] = a3
ARGV[4] = a4

The variable assignment using the -v option is also not counted in the command-line argument; however, normal variable assignment is counted in the command-line argument, and is accessible through ARGV. For example:

$ awk -v A=10 -f arguments.awk B=20 one two three

The output of the execution of this code is as follows:

Total no. of arguments = 5
ARGV[0] = awk
ARGV[1] = B=20
ARGV[2] = one
ARGV[3] = two
ARGV[4] = three

In the next example, we pass arguments to the script in the format - - argument_name argument_value. The AWK script will accept the employee's first name, empfname, and salary, empsal, as arguments. If we use - - name Amit 4000 as an argument for the AWK script, it will set the salary as 4000 for the employee name Amit as follows:

$ vi argc_argv.awk

BEGIN {
OFS=" "
for ( i=0; i<ARGC; i++ )
{
if ( ARGV[i]=="--name")
{
empfname=ARGV[i+1];
empsal=ARGV[i+2];
delete ARGV[i]
delete ARGV[i+1]
delete ARGV[i+2]
}
}
}
{
if ( $1==empfname)
print $1,$2,$3,$4,$5,$6,empsal
else
print $0;
}

$ awk -f argc_argv.awk --name Amit 4000 emp.dat

The output of the execution of the preceding code is as follows:

Jack    Singh   9857532312  [email protected]      M   hr      2000
Jane Kaur 9837432312 [email protected] F hr 1800
Eva Chabra 8827232115 [email protected] F lgs 2100
Amit Sharma 9911887766 [email protected] M lgs 4000
Julie Kapur 8826234556 [email protected] F Ops 2500
Ana Khanna 9856422312 [email protected] F Ops 2700
Hari Singh 8827255666 [email protected] M Ops 2350
Victor Sharma 8826567898 [email protected] M Ops 2500
John Kapur 9911556789 [email protected] M hr 2200
Billy Chabra 9911664321 [email protected] M lgs 1900
Sam khanna 8856345512 [email protected] F lgs 2300
Ginny Singh 9857123466 [email protected] F hr 2250
Emily Kaur 8826175812 [email protected] F Ops 2100
Amy Sharma 9857536898 [email protected] F Ops 2500
Vina Singh 8811776612 [email protected] F lgs 230
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.5.12