jean0t blog


Awk Is Amazing

awk is a great tool for data manipulation from the terminal in linux systems

Tags: programming, operational systems, talk


Awk was created as a way to manipulate data easily because if you wanted to make some manipulation before, you had to create entire programs in C. As we know C isn’t the most complete language, it is really dry in the default library, which means that it would take a lot of time even for something simple, from that inconvenience that amazing tool was born.

Not Just a Tool But a Programming Language By Itself

You can have files named something.awk and it would be taken by the awk program and used to parse the input files, not to mention the possibility to create functions, having for loops and a great regex system.
It considers the data from columns, you have the possibility to modify the separators and pretty much everything. Example of a function in awk:

awk '
    # this code lists every user with /bin/bash
    # as the default shell

    function endsWith(text, suffix) {
        return text ~ prefix"$"
    }
    BEGIN { FS = ":" }
    endsWith($7, "bash") { print $0 }
' /etc/passwd

It has comments with # and a structure pretty well defined, you have 2 basic statements, BEGIN and END, one runs before everything, which you use to configure separators and all variables, the other runs when everything finishes, which you can use to make some substitution or anything else you find useful to run in the end.

Why Would You Use It?

You can use one liners from perl, python or any programming language that is interpreted and can run in command line, but trust me, it wont be as directly and nice as awk. Not to mention that with bash, awk is the only second program you can be 100% sure of existing in any distribution, even alpine has it.
If you want to know a posix tool that will help you create scripts that are sure to run in any environment, it can be a great investment of time to upgrade your skills as system administrator or simply to satisfy a curiosity like me.