Skip to content
  • About
  • Courses
  • Publications
  • Professional Certificates
  • Books
  • Patents
Download CV
Unix

awk Begin and End

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.

$ 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.

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.

Post navigation

Previous Previous
awk Programming
NextContinue
awk more
Latest

🎓 Why Original Work Matters in Your Final Year Project (And How It Can Shape Your Career)

In engineering colleges across the country, final year projects are often treated as just another academic task. But what many students fail to realize is…

Read More 🎓 Why Original Work Matters in Your Final Year Project (And How It Can Shape Your Career)Continue

Latest

🎓 How to Choose Your Final Year Project: A Practical Guide for BTech Students

Choosing the right final year project is one of the most important decisions of your engineering journey. It’s more than just a submission — it’s…

Read More 🎓 How to Choose Your Final Year Project: A Practical Guide for BTech StudentsContinue

Latest

🧠 MCP Server: Model Context Prototyping with Gemini + MySQL + FastAPI

GitHub: https://github.com/nishantmunjal2003/mcp-server-gemini 📌 Project Overview MCP Server is a lightweight, extendable API server that: ⚙️ Features 📁 Folder Structure bashCopyEditmcp-server/ │ ├── app.py # Main…

Read More 🧠 MCP Server: Model Context Prototyping with Gemini + MySQL + FastAPIContinue

Artificial Intelligence Psychology

Why You Can’t Stop Scrolling — And What AI Has to Do With It

Ever caught yourself reaching for your phone, telling yourself it’s “just for a minute”… and then suddenly it’s midnight?You didn’t mean to spend the last…

Read More Why You Can’t Stop Scrolling — And What AI Has to Do With ItContinue

Latest

Advance AI PPT

Read More Advance AI PPTContinue

Nishant Munjal

Coding Humanity’s Future </>

Facebook Twitter Linkedin YouTube Github Email

Tools

  • SIP Calculator
  • EMI Calculator
  • Creative QR Code Generator Tool
  • Write with AI
  • Image Converter
  • SamplePHP

Resources

  • Blog
  • Contact
  • Refund and Returns

Legal

  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

© 2025 - All Rights Reserved

  • About
  • Courses
  • Publications
  • Professional Certificates
  • Books
  • Patents
Download CV
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.