Skip to content
  • About
  • CoursesExpand
    • Problem Solving using C Language
    • Mastering Database Management
    • Linux System Administration
    • Linux and Shell Programming
  • Publications
  • Professional Certificates
  • BooksExpand
    • Books Authored
  • Patents
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

Advance AI PPT

Read More Advance AI PPTContinue

Latest

Prompts for Image Descriptions

Describe the scene using three vivid sensory details — one for sight, one for sound, and one for touch. Summarize the mood of the image…

Read More Prompts for Image DescriptionsContinue

Latest

Dimensionality Reduction

Dimensionality reduction is the process of reducing the number of features (variables) in a dataset while preserving important information. It helps in: ✅ Reducing computational…

Read More Dimensionality ReductionContinue

Artificial Intelligence

Tanh Function in Neural Network

The tanh function, short for hyperbolic tangent function, is another commonly used activation function in neural networks. It maps any real-valued number into a value…

Read More Tanh Function in Neural NetworkContinue

Latest

Why Initialize Weights in Neural Network

Initializing weights and biases is a crucial step in building a neural network. Proper initialization helps ensure that the network converges to a good solution…

Read More Why Initialize Weights in Neural NetworkContinue

Nishant Munjal

Coding Humanity’s Future </>

Facebook Twitter Linkedin YouTube Github Email

Tools

  • SIP Calculator
  • Write with AI
  • SamplePHP
  • Image Converter

Resources

  • Blog
  • Contact
  • Refund and Returns

Legal

  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

© 2025 - All Rights Reserved

  • About
  • Courses
    • Problem Solving using C Language
    • Mastering Database Management
    • Linux System Administration
    • Linux and Shell Programming
  • Publications
  • Professional Certificates
  • Books
    • Books Authored
  • 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.Ok