When working in the command line environment, it is essential to have a strong understanding of the various commands available to effectively manage files, directories, and other data. One such command is the ‘awk’ command. awk is a powerful utility used to process and manipulate text files in the Unix/Linux environment. This article will explain what the ‘awk’ command is and ways to use it effectively.
What is the ‘awk’ Command?
The ‘awk’ command is a powerful tool for manipulating and processing text files in Unix/Linux environments. It can be used to perform tasks such as pattern matching, filtering, sorting, and manipulating data. awk is mainly used to process and manipulate data in a structured manner.
How to Use awk Command
awk is a command-line tool that can be used in a variety of ways. It can be invoked directly from the command line, or it can be used in conjunction with a shell script. Here are some examples of how to use awk:
Example 1: Counting the Number of Lines in a File
To count the number of lines in a file, you can use the following awk syntax:
Here, “NR” is a built-in variable that contains the number of records (lines) processed by awk. The “END” keyword tells awk to execute this command after all lines in the file have been processed. Here I have created a file text file for illustration purposes and then used the above syntax in a shell script that is:
awk 'END{print NR}' testfile.txt
The text file I created has two lines and when the awk command is used the output displayed 2, you can see the text file I created in the image below:
Example 2: Filtering Data
The awk can be used to filter data based on specific criteria and here is the syntax that one should use for such purpose:
For instance, you can use the command below to filter out all lines in a file that contain the word “Hello.”
awk '!/Hello/' testfile.txt
In this example, the “!” symbol negates the regular expression search, so all lines that do not contain the word “Hello” will be printed. I have used the same text file as in the previous example so here is the output of the above given script:
Example 3: Extracting Specific Fields
awk can also be used to extract specific fields from a file. For example, if you have a file containing a list of names and addresses, and you want to extract only the names, you can use the following command:
Here for illustration, I have printed the first field of the same text file and “$1” represents the first field in each line of the file. The “print” command tells awk to print only that field.
awk '{print $1}' testfile.txt
In the text file the first entry of first line is “This” and the first entry of the second line is “Hello” so here is the output of the given code:
Conclusion
The awk command is a powerful tool that is used to manipulate and process text files. It allows you to perform various operations on text files, such as printing specific columns, searching for patterns, and calculating sums. By mastering the basics of awk, you can streamline your workflow and become a more efficient and effective Linux or Unix user.