Awk is one of the most powerful commands on Linux, some consider it as the programming language too. The three scientists named Alfred Aho, Peter Weinberger, and Brian Kernighan at the AT & T Bell Laboratories in 1977 write the Awk command. We get the term Awk by combining the first letters of these three names. Awk overcomes the limitations of the sed command and is used for text processing and text scripting.
Awk is not just used for searching and replacing the text, we also use it for sorting, validating, and indexing the Database. We also called it the GNU Awk (gAwk) as it serves as one of the important filters of Linux.
Tasks performed by Awk command
Let's explore some tasks that can be performed using the Awk command:
- By using the Awk command, we scan a full file each line by line, and it can also format the output lines generated when we give a particular input.
- The command could split a single file into multiple fields of small clusters and then compares the input text generating the exact output.
- Not only searching, it also performs several other actions such as sorting, validating, and indexing the Database.
- Just as any other programming language, it could apply arithmetic and string operation, conditions, and loops on input string to generate output.
- So, let’s know about the Awk command on Linux, which is one of the most used commands in scripting. Also, we will know about how the Awk command is used to manipulate data, using this AWK command how we can generate specified reports as per our user requirement on Linux.
Awk Syntax
Let's begin with the syntax the following is the syntax of the Awk Command
Awk ‘BEGIN {start_action} {action} END {stop_action}’ filename
The action
here specifies the actions to be performed. start_action
and stop_action
are the delimiters.
Let us look at the Awk command with simple examples:
-
To print the entire data of the file
cat filename|
Awk '{print}' filename
cat filename|
Awk '{print$0}' filename
the filename specifies the filename of the file that you would like to get details.
Previously, we used to do it with the cat
command; Here is how you can display the data with the Awk command.
Both the Awk '{print}
' and Awk '{print$0}
' are used to print the entire data of the File.
-
To print a particular column
cat filename| Awk '{print$1}'
Awk '{print$1}' filename
Awk '{print$1}
' used to print the very first column
Awk '{print$2}
' used to print the second column
Using this, you can access a particular column without getting the entire file description.
-
To print pattern Matching Technique in Awk
$ free -m | Awk '{/Mem/print}'
Mem
here represents the row names Mem. Here, we are matching Mem using Awk '{/Mem/print}
', the Mem, here is used to match a particular pattern, then returns the row that is matched with the tag name Mem.
-
To Print multiple columns
free -h | Awk '/Mem/{print$1,$2,$4}'
Even when we are using Mem, if we want to extract only some columns with the tag name Mem, we can do this by specifying column numbers you want to display with the Print
statement. Here, it prints 1st,2nd,3rd columns at the time of type Mem.
-
To Print line number using variable NR
free -h | Awk '{print NR,$0}'
If you want the line numbers to be displayed with each line, You can do this by using the NR
command along with Awk.
The above command returns the output along with the line numbers.
-
To print specific lines
If you want to print only specific lines along with the line numbers, You can do this by using NR == <line number>
that you want to display along with the command Awk.
free -h | Awk 'NR == 2,NR == 9{print NR,$0}'
Here, the command print lines between 2 & 9.
-
Filter data manipulation
In the real-time environment, if you want to get the list of all users, you can use the following command
$ cat listofusers.txt | Awk '$4~/M/'
The command gives the list of all users.
Conclusion
The article gives you a brief about one of the important commands on Linux called Awk, how to use this command, and what is the syntax of the command.
Hope you like the article.