Awk command on Linux (processes data) [Basic Guide]

awk to process data in Linux

The awk command in Linux is a powerful tool that allows you to process texts and make changes to files. It can interpret small scripts to process a file or text.

There are other AWK variants, such as: BWK, GAWK and MAWK, which are implementations from different developers with some improvements.

The awk can receive commands directly as parameters, or through a file, using the “-f” option followed by the name of the file containing the commands.

Example:

Given a file with text separated by spaces, the “print” command will serve to display something on the screen. The variables $0, $1, $2, $3, and $4 can print the entire content ($0), or each column of the text:

To print the entire contents of the file:

$ awk '{print $0}' file Read Organa General Resistance Luke Skywalker Jedi Resistance Darth Vader Sith Empire Obi-Wan Kenobi Jedi Resistance

To print only the first column:

$awk '{print $1}' file Read Luke Darth Obi-Wan

To print the second column:

$ awk '{print $2}' Organa Skywalker Vader Kenobi file

It is also possible to play with the text:

$ awk '{print $1 "" $2 "is a" $3 "from the “$4}' file Leia Organa is a Resistance General Luke Skywalker is a Resistance Jedi Darth Vader is a Sith from the Obi-Wan Empire Empire Kenobi is a Resistance Jedi

To list only user logins in the /etc/passwd file, you can use the “-F” option, indicating the delimiter:

$ awk -F': '' {print $1} '/etc/passwd root bin daemon adm

The awk can also be used to transform a text or CSV file for SQL commands:

$ awk '{print “insert into user (name, surname, position, department) values (\"” $1 “\”,\ "” $2 “\”,\ "”,\ "” $3 “\”,\ "” $4 “\”);”}' file 
insert into user (name, surname, position, department) values (“Read”, "Organa”, "General”, "Resistance”);
insert into user (first name, last name, position, department) values (“Luke”, "Skywalker”, "Jedi”, "Resistance”);

The only care that should be taken is to reference the quotation marks that make up the SQL command with the “\” backslash, so that they are interpreted as text and not as quotes.

Awk accepts the if, for, and while directives as in the C language, as well as mathematical operators and many other things. In short, it’s a complete tool for transforming files.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need help?