AWK Basic Tutorial

  5 mins read  

AWK Basic Tutorial

Introduction

AWK is an interpreted programming language that is generally used to make shell scripts more powerful and feature-rich. It is mostly used to process data in text and file operations.

The name comes from the first letter of the surname of each of the creators: Alfred Aho, Peter Weinberger and Brian Kernighan.

This language is considered by many to be an important milestone in the history of programming, having had a great influence on the creation of other programming languages, such as Perl and Python.

AWK Variants

  • BWK: Also known as AWK or NAWK refers to the version by Brian Kernighan.
  • GAWK: (GNU AWK) is another open source implementation (OPEN SOURCE)
  • MAWK: It’s a much faster AWK implementation version by Mike Brennan (…)

Example 1

awk '{ print "Hello, World" }'
awk -f hello.awk

The - f flag says that the following command is a program to execute.

For an executable program, use /usr/bin/awk.

For example, create a world.awk file with the content

# !/usr/bin/awk -f
# My first awk script
{ print "Hello, World!" }

Making this file executable

chmod +x world.awk

Running

./world.awk

Language Variables

Save the dogs.txt File

rex calm skinny yellow
skinny angry white white
fat white lennon
fat angry white fred

Print entire file

awk '{print $0}' dogs.txt
  • $1 prints the dog’s name,
  • $2 a color,
  • $3 your mood,
  • $4 your fitness.

We can use these variables within the print command. In it, whenever we put a comma, we are spacing

awk '{print "The",$1,"is",$4}' dogs.txt

Output

The rex is yellow
The skinny is white
The fat is 
The fat is fred

Without spaces

awk '{print "The"$1"is"$4}' dogs.txt

Output

Therexisyellow
Theskinnyiswhite
Thefatis
Thefatisfred

Using BEGIN

With BEGIN, AWK doesn’t wait for an entry (like typing ENTER twice), it executes everything from that block.

awk 'BEGIN {print "Everything will be\nprinted\nat once"}'

Output

Everything will be
printed
at once

Tips

  • We use the - F flag to tell AWK that we will use the **CSV (comma separated values) **.
  • As in language C, AWK also has the command printf, which is much more flexible than print.

    The only thing print does that printf doesn’t do is automatically put a new line at the end of the string \n.

awk '{printf "Dog Color% s:% s \n", $1, $2}' dogs.txt
  • sprintf: It acts and has the same syntax as printf. The difference is that its output is stored in a variable rather than displayed on the screen.

User Defined Variables

Cannot start variables by digits, they are case sensitive, must not have the same name as AWK variables, or other commands, and do not need to be initialized or declared.

When using the first variable, it is either an empty string (“”) or “0”, but it is not good to trust your script with these initial values.

AWK is poorly typed, that is, you can assign a number to a variable and then not assign a string, for example.

Ex .: Save as “dogs2.awk

BEGIN { namecolor="%-15s %20s\n"; printf namecolor, "Name", "Color\n"}
 { printf namecolor, $1, $2}

Run: awk -f dogs2.awk dogs.txt

if and comparison operators

These are the control statements that control the flow of program execution in AWK. In AWK, they are similar to C, and have the same looping and interactions as the Shell pattern.

They are: if, while, for, do and the like.

File: gnu.txt

Eric    59 3 2112
Linus    30 5 578
Richard   40 3 2789
Marcos   24 1 134
awk '{ if($4 > 2000) print $1 }' gnu.txt

Output:

Eric
Richard

Directing the exit

awk 'BEGIN {print 1+1 > "result" }' gnu.txt

View: cat result

else and else if

Create a file: vim conditions.awk and insert this content:

#!/usr/bin/awk -f
{
 if ($1 == "Eric")
 printf "The first \n"
 else if ($1 == "Linus")
 printf "the second\n"
 else
 printf "Other person\n"
}

Run: awk -f conditions.awk gnu.txt


This was just an introduction to Awk, in the future we’ll see more tips.

Thanks for reading!

Join our Facebook group