In Linux, there are many different tools that make the user and administrator's job easier by performing routine or complex tasks. Such solutions allow to automate and simplify the user's work. The awk utility is no exception!
Working with the Linux family of operating systems often requires changing configurations, datasets, documents and other types of data, which the OS is full of, requiring processing according to a given template. This utility is not a classic editor, but a streaming solution that immediately processes data according to the condition. Let's consider it in detail in this instruction!
What is awk and how to use it?
Awk is a streaming text editor, filtering and processing data that comes in as input. Then, which are passed to the standard output stream stdout. It can be redirected to a file or other utility for further work with them.
Awk is not only a utility, but a full-fledged data processing language that reads the information line by line and applies the appropriate commands to change it. For clarity, let's take test data as an example and consider basic commands on its example:
echo '1:2:3:4:5:6:7:8' | awk -F ":" '{print $1}'
This command searches for a value in the first position, taking into account that the separator ":" and after the value is found apply the command "print" to it. This allows you to print the content on the screen. This construction at first glance seems monstrous and not logical, but it will become clearer once you understand the syntax!
awk [options] '[condition {command}]' file
The -F option allows you to override the delimiter between blocks of data, you don't have to use quotes to work. It is acceptable to use immediately after the option, for example -F ":" is equivalent to -F:, which allows to simplify the work. Also among the options you can find the following:
- -f, --file - read the data stream to the input not through the standard stream, but through the specified file;
- -e, --source - execute the specified code in awk;
- -o, --pretty-print - write the result of the utility's work to a file.
The next block condition and command, the utility reads the input data and applies the command to the values matching the specified condition. The format/[value]/ is used to mark the boundaries of the template. Most logical expressions can be implemented into these bounds. Also, when processing a stream of text, the utility defines variables that can be useful for processing and also used in the condition. For example, the $1 variable defines the first element in the strings. Let's assume that the task is to output all user logins. To do this, let's write the command below:
cat /etc/passwd | awk -F: '{print $1}'
And if it is necessary to find among all values of the first column the user with the login 'root', then use the following command:
cat /etc/passwd | awk -F: '/root/ {print $1}'
To search for /root it is necessary to pre-screen the / sign, this means to put a backslash \ in front of it, so the utility will realise that it is not the beginning and end sign of the pattern, but a part of it!
cat /etc/passwd | awk -F: '//root/ {print $1}'
If you want to perform a more structured search, you can use variable access and compare the result:
cat /etc/passwd | awk -F: '$1 == "root" || $4 == "bin" {print}'
For a multitasking template with output to a text file, we will use the command:
cat /etc/passwd | awk -F: '/root/{print} /docker/ {print}' > /tmp/result1.txt
All steps in the tutorial can be performed on powerful cloud servers. Serverspace provides isolated VPS / VDS servers for common and virtualize usage.
It will take some time to deploy server capacity. After that you can connect in any of the convenient ways!
If you need to calculate the number of existing accounts, you can also use the prepared variable NR for this purpose:
cat /etc/passwd | awk -F: '/root/{print NR}'
For a more detailed list of possible options and elements, invoke the help command:
man awk
This tutorial covers the basics of the filtering and pattern search utility, making it easy to get started using this solution. We have covered several types of conditions, while searching, their corresponding commands in the context of work cases.