18th September 2021

What is Linux AWK? AWK Example Usage?

There is information about the “awk” command in Linux-based operating systems, which is used to search within the file, list it, and print the content appropriately. Since everything is a file in Linux, it may take time to find and list the settings in the file. In this case, you can use the “awk” command, which is widely used to get the desired value in the file and list it. As the basic function, words are parsed according to the spaces in the line and take reference value according to their order. For example; first variable is “$1” , second variable is “$2” and n variable is “$n

“awk” Example Usage

Let’s create a .txt file to show in the example. We will understand the use of the awk command by making examples on this file.

generating sample data
generating sample data

 

As can be seen in the example, we have very simple data. In this way, we can easily use the spaces in the line as a parser. When we evaluate the rows and columns according to the spaces, it will be First = $1, Last Name = $2, Age = $3 and City = $4.

simple data
simple data

 

Example 1:

Our first example would be to display the $1 values in the name column, i.e. contact names.

Example 1
Example 1

 

Example 2:

If we want to print the Name and Surname information together, it will be as follows.

 

Example 2
Example 2

 

When we apply the above code, you will see that the data is displayed without any spaces between First Name and Last Name values. To correct this situation, double quotes (” “) denoting the use of string expressions will be useful.

LEARN MORE  Installing VMware Tools on Ubuntu

double quotes ("")
double quotes (” “)

 

Example 3:

You can use the commands below to view all columns.

Example 3
Example 3

 

In Commands length, NR and NF

We will talk about length, NR and NF that you can use in commands. “length” will give the number of characters in the specified line, “NR” will give the number of lines, and “NF” will give the number of space-separated fields in the line. When we apply the code below, the output we will get will be as follows.

In Commands NF
In Commands NF

 

Example 4:

Our data is under 13 columns (the first number indicates the headings). We know the headers of the columns and we want the data to return to us, except for the headers, when listing the data. Then we do row control (NR) with “if”. We use the (NR!=1) command so that the first line does not appear. In this way, we can ensure that the remaining ones are listed.

LEARN MORE  What is Alias Command? Identifying Alias ​​in Kali with Examples

Example 4
Example 4

 

Example 5:

If we want to display only the rows with all fields filled in the data stack, you can use the following command.

Example 5
Example 5

 

Filtering and Mathematical Operations Using “awk”

Let’s say we have a table consisting of 3 columns, for example, Continent, Country, Population. Let’s take a value in the 2nd column of this table (countries with a Country value of X) and get the Population sum of this value. The name of our file is awkworkbook.csv, ‘;’ in the table where the columns are separated as comma-separated Let’s specify the character we use for parsing with. We can search for the value X in the $2 column and the Country column and return the sum of the values with {sum += $3}.

create awkworkbook.csv file
create awkworkbook.csv file
create awkworkbook.csv file
create awkworkbook.csv file

 

Example 6:

Based on the code and table below, the result of the command will be “345533“.

Filtering and Mathematical Operations Using "awk"
Filtering and Mathematical Operations Using “awk”

 

Special Characters Using “awk”

\n” is a horizontal new line. “\t” Tab character. “\v” Line in vertical. \” Double quotes.
Since double quotes are used as string identifiers, you can prevent the data from being treated as a string by putting \ in front of double-quotes. Apart from these special characters, which we have limited in relation to the examples, there are of course different characters that you can use.

LEARN MORE  What is DVWA and Installing on Kali Linux

Editing Content

Example 7:

The following code will change the column order (by swapping 5 and 4) and transfer the new output to the new.txt file. However, we have seen in the examples above that as a result of this process, the gap between the columns will also disappear. In this case, we can insert spaces with double quotes, commas to sort columns, or assign the existing order to a variable.

Example 8:

After some simple changes to column-by-column sorting, we continue with cross-column adjustments. With the command below, we will both change the order of the columns and change the space between the columns with “—” and save this edited data as a new.txt document.

 

Example 9:

In the code below, the files in the current directory will be listed and the name and extension information of the files with JPG extension will be arranged as name and “*.doc”. That is, JPG files will be saved as DOC with the same name. -F” in the code.” Specifies that the column separator character will be a period (.) instead of a space. The next bash will have the output processed as a command.

 

Example 10:

It’s helpful to use a backslash (\) to write long “awk” commands on a new line.

Example 10
Example 10

 

Example 11:

In the last example, the BEGIN keyword is used to give information at the beginning of the command when processing with the “Awk” command.

Example 11
Example 11

 

Leave a Reply

Your email address will not be published. Required fields are marked *