The awk command in Linux is a scripting language. It is helpful for text processing, data manipulation and report generation while working in the command line. The awk allows the user to use variables, numerical functions, string functions and logical operators without further compilation. The awk enables a programmer to develop a precise but impactful script that can do a specific task for the matched line of every record inside a file.
A. Description
The awk command is suitable for pattern search and processing a file. The script runs to search one or more files to identify matching patterns and if the said patterns perform specific tasks.
B. Syntax
The awk command in Linux is a built-in command that takes options and FILE as its argument. The general syntax for using awk commands is
awk '{action}' [file_name.txt]
C. Options
One or more options can be added to the syntax of the awk command to modify the command. I have listed some useful options below. If you do not find your desired option here, you can look for it on the man (manual) page. To go to the man page, type the following command and press ENTER.
man awk
Useful Options
- -F value (sets the field separator, FS, to value.)
- -f file (Program text is read from the file instead of from the command line. Multiple -f options are allowed.)
- -v var=value (assigns value to program variable var.)
D. Variables
Built-in variables of the awk command in Linux include the field variables—$0, $1, $2, $3, $4, $5 and so on (here, $0 is the entire line) — that divide a line of text into separate words or parts called fields. Here, I have listed some of the useful variables of the awk command.
- NR: Usually, the awk command executes the pattern/action statements for each record in a file. With the NR built-in variables, it tracks a current count of the number of input records(lines).
- NF: The NF command tracks a count of the fields within the current input data lines/record.
- FS: The FS command contains the field separator character used to divide fields on the input line. The default is “white space,” meaning space and tab characters. FS can be reassigned to another character (typically in BEGIN) to change the field separator.
- RS: The RS command preserves the current record separator character. As an input line is the input record by default, the default record separator character is a new line.
- OFS: The OFS command preserves the output field separator. It separates the fields while printing them.
E. Special Expression Patterns
The awk command includes the following special patterns:
- BEGIN – Used to perform actions before records are processed.
- END – Used to perform actions after records are processed.
Remember that, the BEGIN pattern is generally used to set variables, and the END pattern processes data from the records.
Practical Examples of the “awk” Command in Linux
The awk command in Linux is a convenient tool for text manipulation. Going through these examples, you will learn how to use the awk command on Linux in this article. The awk command in Linux has many practical applications, and a few of them are outlined below.
Example 1: Print Every Line of Data From the Specified File Using the “awk” Command in Linux
You can print every line of data from the specified file using the awk command in Linux. Here, I have a text file named hitech.txt which contains the name designation, department and salary of the employees. Now, I will print every line of data from the specified file using the awk command in Linux. To do so, follow the below procedures.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
awk '{print}' hitech.txt
➌ Now, press the ENTER button.
Output >
The following image shows that the awk command prints every data line from the specified file named hitech.txt.
Similar Readings
- The “killall” Command in Linux [6+ Practical Examples]
- The “kill” Command in Linux [4+ Practical Examples]
- The “ps” Command in Linux [9+ Practical Examples]
Example 2: Print the Lines Which Match the Given Pattern Using the “awk” Command in Linux
The awk command in Linux has an amazing feature of printing the lines which match the given pattern. Here, I will print the data line of employees who are programmers on the command prompt. To accomplish this, follow the steps below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Enter the following command in the command line:
awk '/Programmer/ {print}' hitech.txt
➌ Now, hit the ENTER button.
Output >
The following image shows that the data line from the hitech.txt text file, who are programmers, is printed on the command prompt.
Example 3: Splitting Lines Into Fields Using the “awk” Command in Linux
The awk command in Linux helps us to split a line into fields/words. The awk command splits the record delimited by space and preserves them individually in the $n variable. If a line has 5 fields/words, these words are stored in $1, $2, $3, $4 and $5, respectively and $0 means the whole line. I will print the employee name and designation(1st and 2nd word/field, respectively) on the command prompt. To proceed, follow the instructions below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the command as shown below on the command line
awk '{print $1,$2}' hitech.txt
➌ Now, push the ENTER button.
Output >
The following image demonstrates that name and designation of all employee is printed on the terminal with the help of the awk command in Linux.
Example 4: Display Line Numbers Using NR built-in Variables With the “awk” Command in Linux
After going through the previous examples, now if you desire to print the line number along with the data line, it is possible with the help of the awk command and the built-in NR variable in Linux. Here, I will print all the line numbers along with the data. To do so, follow the outlined procedures below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the given command into the command line
awk '{print NR,$0}' hitech.txt
➌ Now, click the ENTER button.
Output >
The awk command prints the line number and the data on the command prompt, as depicted in the following image.
Example 5: Display Lines of Specific Range Using the NR Built-in Variables
You can easily display a bunch of lines from your desired file using the NR built-in variable of the awk command in Linux. Here, I will print lines of the hitech.txt text file from 2 to 5 on the command prompt. To fulfill this task, follow the guidelines below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt window.
awk 'NR==2, NR==5 {print NR,$0}' hitech.txt
➌ Now, hit the ENTER button.
Output >
The attached image illustrates the printing of lines of the hitech.txt text file from 2 to 5 through the use of the awk command in Linux.
Example 6: Printing the First Item Along with the Row Number Separated with Delimiter
You can easily print line numbers with any field separated by a specific delimiter (in this example, I will use a dash “–”) by using the NR built-in variable of the awk command in Linux. Here, I will print the names of all employees from the hitech.txt file and the line number separated by a dash (–). To do so, stick to the steps outlined below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Enter the command below into the command line.
awk '{print NR "- " $1 }' hitech.txt
➌ Now, use the ENTER button.
Output >
The following image displays the implementation of the printing line number and the first field of every record separated with a dash(-) using the NR built-in variable of the awk command in Linux.
Example 7: Use of NF Built-in Variables (Displaying the Last Field)
The awk command in Linux has a handy built-in variable called NF, representing the last field of the data line. Here I will print the name and salary of every employee, which are the 1st($1) and last($NF) fields, respectively. To accomplish this goal, follow the steps described below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Input the command shown below into the command terminal
awk '{print $1,$NF}' hitech.txt
➌ Now, press the ENTER button.
Output >
The following image illustrates my execution of printing 1st and last field of every data line from the hitech.txt file with the assistance of the awk command and $NF built-in variable.
Example 8: Find the Length of the Longest Line of a File Using the “awk” Command in Linux
You can easily find the length of the biggest line in a file using the awk command in Linux. Here, I will print the length of the biggest line in the hitech.txt file using the awk command in Linux. To carry out this task, follow the steps given below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command into the command prompt window.
awk '{ if (length($0) > max) max = length($0) } END { print max }' hitech.txt
Explanation: Here, all lines of the hitech.txt file will be read sequentially. length($0) command will find the length of the whole line and compare it with the value of variable max. If the previous value of max is smaller than the length of the current line, then the length of the current line will be set as the value of the max variable. This process will be done for every line. After executing the same command for all lines, it will print the final value of max.
➌ Now, tap the ENTER key.
Output >
The following image illustrates that the awk command is used to find the length of the biggest line in the hitech.txt text file.
Example 9: Count the Lines in a File Using the “awk” Command in Linux
The awk command in Linux can count the total line number in a text file. Here I will count the total line numbers in the hitech.txt text file. To move forward, adhere to these steps.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the given command into the terminal.
awk 'END { print NR }' hitech.txt
➌ Now, click on the ENTER button.
Output >
The image displays my implementation of counting line numbers in the hitech.txt file using the awk command in Linux.
Example 10: Using Special Expression Pattern to Perform Specific Mathematical Operation
You can serially print squares of some numbers using the awk command in Linux. Here I will print the squares of the numbers from 10 to 15 on the command prompt. To accomplish this, follow these instructions.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Enter the command provided into the terminal window.
awk 'BEGIN { for(i=10;i<=15;i++) print "square of", i, "is",i*i; }'
Explanation: In this command BEGIN command will execute the statement before taking further input. Here for loop is used to iteratively use the value of i from 10 to 15, and print the value of i and “square of” i (multiplying “i” by ”i”).
➌ Now, push the ENTER button.
Output >
The following image shows that the awk command is utilized to print the square of the numbers from 10 to 15.
Example 11: Printing Lines with More or less than a Number of Characters
The awk command in Linux can print lines with more or less than a number of characters in a text file.
Case A: Print Lines With More than a Number of Characters
Here, I will print lines with more than 30 characters using the awk command in Linux. To proceed with this task, follow these guidelines.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the given command into the command line.
awk 'length($0) > 30' hitech.txt
➌ Now, press the ENTER button.
Output >
The following image illustrates that the awk command enables me to print lines from the hitech.txt text file with more than 30 characters.
Case B: Print Lines With Less than a Number of Characters
Here, I will print lines with less than 30 characters using the awk command in Linux. To fulfill this, follow these steps.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Enter the command provided into the terminal window.
awk 'length($0) < 30' hitech.txt
➌ Now, press the ENTER button.
Output >
The following image depicts that lines from the hitech.txt text file with less than 30 characters are printed into the terminal.
Conclusion:
In this article, I have shown some implementation and usefulness of the awk command in Linux. After going through this article, I hope you’ll be skillful enough to explore more things with the help of the illustrated practical examples.
Similar Readings
- The “jobs” Command in Linux [6 Practical Examples]
- The “exec” Command in Linux [8 Practical Examples]
- The “make” Command in Linux [5 Practical Examples]
- The “sleep” Command in Linux [10 Practical Examples]
- The “xargs” Command in Linux [5 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners