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 '{ 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
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
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
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
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 operatorsThese 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!
awk gawk linux command programming