Skip to content
  • About
  • Courses
  • ResearchExpand
    • Research Publications
    • Books
    • Patents
  • Workshop/Conferences
  • ToolsExpand
    • Creative Image Converter
    • Creative QRCode Generator
    • Creative QR Code Generator Tool
    • EMI Calculator
    • SIP Calculator
  • Blog
  • Resume
Download CV
Unix

awk Programming

The name awk comes from the initials of its designers: Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan. The original version of awk was written in 1977 at AT&T Bell Laboratories. In 1985 a new version made the programming language more powerful, introducing user-defined functions, multiple input streams, and computed regular expressions. This new version became generally available with Unix System V Release 3.1.

.awk is a programming language designed to search for match patterns and perform actions on files.

AWK Commands

$awk 'length($1) > 5 {print}' mywords
$awk 'length($1) > 5' mywords
$awk '$1 ~ /^[b,c]/ {print $1}' mywords

The above script print all the words that begin with b or c character. The regular expression is placed between two slash characters.

$awk 'NR % 2 == 0 {print}' mywords 

NR is a built-in variable that refers to the current line being processed. The above program prints each second record of the mywords file. Modulo dividing the NR variable we get an even line. $awk ‘{print NR, $0}’ mywords Here NR variable will print the line number and the $0 variable refers to the whole record.

$awk '{print substr($0, 4)}' code.c

substr() function. It prints a substring from the given string. We apply the function on each line, skipping the first three characters. In other words, we print each record from the fourth character till its end.

The Match Function

The match() is a built-in string manipulation function. It tests if the given string contains a regular expression pattern. The first parameter is the string, the second is the regex pattern.

$awk 'match($0, /^[c,b]/)' mywords

The program prints those lines that begin with c or b. The regular expression is placed between two slash characters.

$awk 'match($0, /i/) {print $0 " has i character at " RSTART}' mywords

The match() function sets the RSTART variable; it is the index of the start of the matching pattern. This prints those words that contain the ‘i’ character. In addition, it prints the first occurrence of the character.

$ awk -F: '{print $1, $7}' /etc/passwd | head -7
$ echo "Jane 17#Tom 23#Mark 34" | awk 'BEGIN {RS="#"} {print $1, "is", $2, "years old"}' 

Jane is 17 years old
Tom is 23 years old
Mark is 34 years old
The RS is the input record separator, by default a newline. In the example, we have relevant data separated by the # character. The RS is used to strip them. AWK can receive input from other commands like echo.

Post navigation

Previous Previous
Unix Variable $* and $# Difference
NextContinue
awk Begin and End
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
  • Image Converter

Resources

  • Blog
  • Contact
  • Refund and Returns

Legal

  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

© 2025 - All Rights Reserved

  • About
  • Courses
  • Research
    • Research Publications
    • Books
    • Patents
  • Workshop/Conferences
  • Tools
    • Creative Image Converter
    • Creative QRCode Generator
    • Creative QR Code Generator Tool
    • EMI Calculator
    • SIP Calculator
  • Blog
  • Resume
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.