List

 

AWK Workflow

AWK has several built-in variables. They are set by AWK when the program is run. We have already seen the NR$0, and RSTART variables.

$ awk 'BEGIN { print ARGC, ARGV[0], ARGV[1]}' mywords
2 awk mywords

The program prints the number of arguments of the AWK program and the first two arguments. ARGC is the number of command-line arguments; in our case, there are two arguments including the AWK itself. ARGV is an array of command-line arguments. The array is indexed from 0 to ARGC – 1.

FS is an input field separator, a space by default. NF is the number of fields in the current input record.

For the following program, we use this file:

$wget nishantmunjal.com/dataset/values

$ cat values 
2, 53, 4, 16, 4, 23, 2, 7, 88
4, 5, 16, 42, 3, 7, 8, 39, 21
23, 43, 67, 12, 11, 33, 3, 6

We have three lines of comma-separated values.

 

$wget nishantmunjal.com/dataset/stats.awk
BEGIN {

    FS=","
    max = 0
    min = 10**10
    sum = 0
    avg = 0
} 

{ 
    for (i=1; i<=NF; i++) { 
    
        sum += $i
    
        if (max < $i) {
            max = $i
        }
        
        if (min > $i) {
            min = $i
        }
    
        printf("%d ",  $i) 
    }
}

END {
    
    avg = sum / NF
    printf("\n")
    printf("Min: %d, Max: %d, Sum: %d, Average: %d\n", min, max, sum, avg)
}

The program counts the basic statistics from the provided values.

FS=","

The values in the file are separated by the comma character; therefore, we set the FS variable to comma character.

max = 0
min = 10**10
sum = 0
avg = 0

We define default values for the maximum, minimum, sum, and average. AWK variables are dynamic; their values are either floating-point numbers or strings or both, depending upon how they are used.

{ 
    for (i=1; i<=NF; i++) { 
    
        sum += $i
    
        if (max < $i) {
            max = $i
        }
        
        if (min > $i) {
            min = $i
        }
    
        printf("%d ",  $i) 
    }
}

In the main part of the script, we go through each line and calculate the maximum, minimum, and the sum of the values. The NF is used to determine the number of values per line.

END {
    
    avg = sum / NF
    printf("\n")
    printf("Min: %d, Max: %d, Sum: %d, Average: %d\n", min, max, sum, avg)
}

In the end part of the script, we calculate the average and print the calculations to the console.

$ awk -f stats.awk values
2 53 4 16 4 23 2 7 88 4 5 16 42 3 7 8 39 21 23 43 67 12 11 33 3 6 
Min: 2, Max: 88, Sum: 542, Average: 67

This is the output of the stats.awk program.

One Response to “awk Begin and End”

  1. awk Programming | Nishant Munjal

    […] AWK BEGIN AND END Want to Learn More… […]

Leave a Reply

Your email address will not be published. Required fields are marked *

  Posts

1 2 3
October 20th, 2020

Compute the Compound Interest.py

Write a function with name compound_interest that takes three arguments: principle, rate and years in order. the rate is float […]

October 18th, 2020

String Data Type

A string is a sequence of characters. String Data Type str1=”hello” print(type(str1)) Ans: <class ‘str’> str2=’123′ print(type(str2)) Ans: <class ‘str’> […]

October 18th, 2020

String Library

String Replace str=”Hello Bob” print(str) rstr=str.replace(‘Bob’, ‘James’) print(rstr)   This will replace the Bob with James and store it in […]

October 17th, 2020

‘in’ statement in String

  fruits=’banana’ bana in fruits Ans: True Python Function to confirm he vowel in the given input. def is_vowel(l): return […]

September 24th, 2020

awk Marking keywords

Marking keywords In the following example, we mark Java keywords in a source file. $wget nishantmunjal.com/dataset/mark_keywords.awk # the program adds […]

September 24th, 2020

awk Rock-paper-scissors

Rock-paper-scissors Rock-paper-scissors is a popular hand game in which each player simultaneously forms one of three shapes with an outstretched […]

September 24th, 2020

awk Spell Checking

Spell checking We create an AWK program for spell checking. $wget nishantmunjal.com/dataset/spellcheck.awk BEGIN { count = 0 i = 0 […]

September 24th, 2020

awk Pipes

Pipes AWK can receive input and send output to other commands via the pipe. $ echo -e “1 2 3 […]

September 24th, 2020

awk Passing variables

Passing variables to AWK AWK has the -v option which is used to assign values to variables. For the next program, we […]

September 24th, 2020

awk more

Passing variables to AWK Pipes-awk awk Spell Checking Rock Paper Scissor Marking Keywords