Skip to content

Awk command on Linux (processes data)

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 <br></br>insert into user (name, surname, position, department) values (“Read”, "Organa”, "General”, "Resistance”); <br></br>insert into user (first name, last name, position, department) values (“Luke”, "Skywalker”, "Jedi”, "Resistance”);

The <span style=“background-color: rgba(0, 0, 0, 0.2); font-size: revert; color: initial; font-family: -apple-system, BlinkMacSystemFont, “Segoe UI”, Roboto, Oxygen-Sans, Ubuntu, Cantarell, “Helvetica Neue”, sans-serif;“>only care that should be taken is to reference the quotation marks that make up the SQL command with the “\” backslash<span style=“background-color: rgba(0, 0, 0, 0.2); font-size: revert; color: initial; font-family: -apple-system, BlinkMacSystemFont, “Segoe UI”, Roboto, Oxygen-Sans, Ubuntu, Cantarell, “Helvetica Neue”, sans-serif;”>, 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.