What is AWK Command in Linux with Examples?
![Command in Linux](https://www.serverwala.com/blog/wp-content/uploads/2024/05/What-is-AWK-Command-in-Linux-with-Examples.jpg)
Introduction
AWK command was developed in the 1970s to solve the issue of textual data. You might be amused that even simple textual data needs command. This command, which later evolved as a language, can handle extensive textual data and search words in it based on the instruction.
Do you know AWK reads text line-by-line? AWK is a brilliant tool that can read data on the fly and with the utmost accuracy. This tool is also a good scripting language, so compiling the language is unnecessary.
In this article, we will study Linux AWK command, their operations, statements, and patterns. Moreover, we have also included simple examples related to the AWK command in Linux.
AWK Command Syntax
We will understand the basic syntax of the AWK command on Linux with a detailed example. The basic command is something like below.
awk 'pattern { action }' input_file
![AWK Command Syntax](https://www.serverwala.com/blog/wp-content/uploads/2024/05/image-6.png)
The term ‘AWK’ in the above is used to start interpreting any given textual data. Next, we have ‘pattern,’ which is used when you want the tool to search for a similar pattern in each line. Then we have the term ‘action’ that goes by its name, meaning what the tool should do when it identifies the pattern.
The last one, ‘input_file,’ is simply the file you want the AWK to read. An interesting feature of the AWK command in Linux lies in the usage of $1, $2, $3, etc. These special variables help in applying the search to particular fields. Like $0 is used to apply a search filter on the entire line. Similarly, the $1 is used to represent the first field. We hope the basic command syntax is crystal clear to you. Now let’s look at an easy example.
Example: There is a text file named ‘score.txt,’ and it has the following content.
Jia 68
Ramon 95
Sandy 50
Now we want the tool to filter out candidates who have scored below 80. We will use the below command to act.
awk '$2 < 80 { print $1 }' score.txt
![](https://www.serverwala.com/blog/wp-content/uploads/2024/05/image-8.png)
We have used $2 to apply the search filter on the second field, i.e., the score part. The $1 variable is used to instruct the tool to print the names of the candidates whose scores are below 80. The result of the above command will be as follows.
Jia
Sandy
Also Read: Ping Command Examples for Linux Users
How Does the AWK Command Work?
![How Does the AWK Command Work?](https://www.serverwala.com/blog/wp-content/uploads/2024/05/image.jpeg)
The working of the AWK command largely depends on the operations, patterns, and statements. The reason behind the dependency is these three important components are the power to modify the behavior of the AWK command. We will analyze all three components in simple language to clearly understand the working of the AWK command in Linux.
AWK Operations
This is the set of instructions that you give to the AWK command in Linux to perform on the particular line. You can go through the below list of operations to get a better understanding.
- Printing specific parts of the text
- Text filtering
- Mathematical calculations on numerical parts of the text
- Instruct for task repetition
- Instruct for counting the occurrence
There are many more types of operations that you can perform on the textual data. The best part about this tool is that it makes the analyses of textual data easy and fast.
AWK Statements
The statements are simply used to control and modify the flow of the instructions. These statements include while, else, for, if, etc. Have a look at their usage below.
We have a file named ‘numerical.txt’ containing the content below.
10
20
34
11
17
We will use the below statement in the command to verify whether a number is odd or even.
awk '{ if ($1 % 2 == 0) print $1, "is even"; else print $1, "is odd"; }' numbers.txt
![](https://www.serverwala.com/blog/wp-content/uploads/2024/05/image-7.png)
In the above command, we have clearly instructed to analyze each number and print odd or even as per the analysis. The result will be as follows.
10 is even
23 is odd
8 is even
15 is odd
This is how statements modify the Linux AWK command.
AWK Patterns
If you’re curious how the AWK command in Linux determines where to apply the search, the pattern is the answer. Yes, patterns ensure the correct part is targeted. The tool easily matches the pattern in the textual data and takes the desired action. You can take a quick tour of major AWK patterns below.
Regular Expression Patterns
If you have complex patterns at hand, then regular expressions can solve each of your search queries. You can find literal matches that are used for exact phrase matches. On the other hand, you also get quantifiers where you can scrutinize the occurrence of the textual part. Expressions like alternations and wildcards are also handy to narrow the search.
Relational Expression Patterns
Relational expressions are very useful when the tasks involve comparison between numerical identities. Some of the relational expression includes <, <=, ==, !=, >=, etc.
Range Patterns
What will you do if you have certain lines to filter out? Well, the best solution is to use the range patterns. You can easily instruct your AWK tool to search on a specific part. Have a look at the syntax of the range pattern below.
/start_pattern/, /end_pattern/ {
# Actions to be performed for lines within the range}
Special Expression Patterns
If you want something more specific or want to apply patterns at the beginning or end, then you have to try out special expressions. You can use the ‘NR’ expression to particularize your search on the number of a particular line. Also, you can take the help of numbers assigned to each line through the ‘FNR’ expression. The ‘FNR’ expression is used when you are handling multiple files.
Combining Patterns
Just like a precise army search operation through the jungle, a combing pattern is used to filter out even the minutest detail. The usage of the AWK command with the combing pattern enhances the search experience and displays better results while performing actions on the targeted part. The ‘&&’ operator can combine two conditions to narrow the search. Further, you have the ‘!’ operator, which is used to reject any match that is not an exact match with the pattern.
AWK Variables
AWK variables in the AWK programming language are like containers or placeholders that hold values. These variables are used to store and manipulate data during the execution of an AWK program. Here are some key points about AWK variables.
AWK Actions
AWK actions in the AWK programming language are blocks of code that are executed when certain conditions are met. These actions allow you to perform specific operations on your data based on patterns or conditions you define. Here are the key points about AWK actions. They allow you to define conditions and perform specific tasks on your input data, making AWK a powerful tool for text processing and data manipulation.
Also Read: 14 Tar Command in Linux (with Examples)
Conclusion
The provided text appears to summarize different aspects of the AWK command in Linux. However, it seems like some sections are incomplete or missing, and some parts are repetitive. AWK is a versatile and powerful command-line tool in Linux, ideal for text processing and data manipulation tasks. With its flexible syntax, patterns, statements, and variables, AWK offers a quick and efficient way to handle textual data.