Categories: Shell Script

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

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.

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.

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.