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:
To print only the first column:
To print the second column:
It is also possible to play with the text:
To list only user logins in the /etc/passwd file, you can use the “-F” option, indicating the delimiter:
The awk can also be used to transform a text or CSV file for SQL commands:
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.