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.
The sudo command on Linux executes a given command as if it were another user.…
The ss command on Linux is extremely useful for investigating sockets, providing various information about…
Free Linux command shows the amount of total memory in use and available, as well…
The shell has structures for testing conditions and executing certain program sequences several times (loop),…
The /etc/fstab file stores the configuration of which devices should be mounted and what is…
The Netcat Command in Linux or nc is a utility used to do “almost anything”…
This website uses cookies.