Skip to content
Nishant Munjal
  • About
  • NMRIL Labs
  • Courses
  • ResearchExpand
    • Research Publications
    • Books
    • Patents
    • Ph.D. Supervised
  • Workshop/Conferences
  • ToolsExpand
    • Creative Image Converter
    • Creative QRCode Generator
    • Creative QR Code Generator Tool
    • EMI Calculator
    • SIP Calculator
  • Blog
  • Resume
One Page CV
Nishant Munjal
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

    What is CSP Bypass?

    What is CSP Bypass? CSP (Content Security Policy) is a browser security feature that tries to stop attacks like XSS (Cross-Site Scripting) by controlling what…

    Read More What is CSP Bypass?Continue

  • Cybersecurity

    Cybersecurity Tools

    Cybersecurity Tools 1️⃣ Digital Forensics Tools Tool Name Type Used For Who Uses Autopsy Computer forensics Analyze hard disk, recover deleted files Investigators FTK Imager…

    Read More Cybersecurity ToolsContinue

  • Cybersecurity

    Command Injection Attack

    Command Injection Attack A Command Injection attack happens when a web application takes user input and passes it to the system shell (Linux/Windows command line)…

    Read More Command Injection AttackContinue

  • Cybersecurity

    CSRF in DVWA (for learning/demo)

    CSRF in DVWA (for learning/demo) CSRF: An attack where a logged-in user is tricked into sending unwanted requests to a web application, causing actions to…

    Read More CSRF in DVWA (for learning/demo)Continue

  • Cybersecurity

    XSS = Cross-Site Scripting using DVWA

    XSS = Cross-Site Scripting using DVWA It allows an attacker to inject JavaScript into a web page so that it runs in another user’s browser….

    Read More XSS = Cross-Site Scripting using DVWAContinue

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

© 2026 Nishant Munjal - All Rights Reserved

  • About
  • NMRIL Labs
  • Courses
  • Research
    • Research Publications
    • Books
    • Patents
    • Ph.D. Supervised
  • 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.