10 popular AWK command examples in Linux

popular AWK commands

Unlock the full potential of AWK with these popular commands that will streamline your text processing tasks and increase your productivity.

Table of Contents

Introduction

AWK is a powerful text processing tool that is widely used in the Linux environment. The name AWK comes from the initials of its creators: Alfred Aho, Peter Weinberger, and Brian Kernighan. It was first introduced in the 1970s and is still a popular tool for text processing. AWK is a programming language that is designed for processing and manipulating text data in a simple and effective way. It is a standard feature in most Unix-like operating systems, including Linux.

Popular AWK commands:

Features

AWK has many features that make it a popular tool for text processing. Some of its popular features include:

  • Pattern matching: AWK can search for specific patterns in text data and perform actions on them.
  • Field Separation: AWK can separate fields based on a delimiter and then perform actions on them.
  • Built-in functions: AWK has many built-in functions for text processing, including mathematical functions, string manipulation functions, and time functions.
  • Output Formatting: AWK can format output in a variety of ways, including changing the field separator, formatting numbers, and sorting data.

10 Popular AWK Command Examples

Display the number of lines in a file

  • Displays the number of lines in the filename
				
					awk 'END {print NR}' filename
				
			
				
					$ cat sample.txt
This is line one
This is line two
This is line three
This is line four

$ awk 'END {print NR}' sample.txt
4

				
			

Search for a specific pattern in a file

  • Searches for the pattern in the filename and prints any lines that contain the pattern
				
					awk '/pattern/ {print}' filename
				
			
				
					$ awk '/one/ {print}' sample.txt 
This is line one
				
			

Print the first field of each line in a file

  • This AWK command will print the first field of each line in the filename
				
					awk '{print $1}' filename
				
			
				
					$ awk '{print $1}' sample.txt
This
This
This
This

				
			

Print the last field of each line in a file

  • This AWK command will print the last field of each line in the filename
				
					awk '{print $NF}' filename
				
			
				
					$ awk '{print $NF}' sample.txt
one
two
three
four

				
			

Print the sum of values in a column in a file

  • Calculate the sum of the values in the first column of the filename and print the result
				
					awk '{print $NF}' filename
				
			
				
					$ cat numbers.txt
123 456 789 101
112 234 543 444
332 331 564 012
023 099 432 213

$ awk '{sum += $1} END {print sum}' numbers.txt 
590

				
			

Print the average of values in a column in a file

  • Calculate the average of the values in the first column of the filename and print the result
				
					awk '{sum += $1; n++} END {print sum / n}' filename
				
			
				
					$ awk '{sum += $1; n++} END {print sum / n}' numbers.txt 
147.5

				
			

Print the lines between two patterns in a file

  • Print all lines between the start pattern and end pattern in a filename
				
					awk '/start pattern/,/end pattern/ {print}' filename
				
			
				
					$ cat sample.txt
This is line one
This is line two
This is line three
This is line four

$ awk '/one/,/three/ {print}' sample.txt
This is line one
This is line two
This is line three

				
			

Sort a file based on a specific column

  • Print the fourth column followed by the first column of each line in the filename and then sort the output.
				
					awk '{print $4 " " $1}' filename | sort
				
			
				
					$ cat sample2.txt 
My name is Adam
Why name him Josh
Her name is Catherine
Why name her Bobbi

$ awk '{print $4 " " $1}' sample2.txt | sort
Adam My
Bobbi Why
Catherine Her
Josh Why

				
			

Remove duplicate lines from a file

  • Use this command to remove all duplicate lines from a file
				
					awk '!seen[$0]++' filename
				
			
				
					$ cat sample3.txt 
My name is Adam
My name is Adam
My name is Adam
Why name him Josh
Why name him Josh
Why name him Josh
Why name him Josh
Why name him Josh
Her name is Catherine
Why name her Bobbi
Why name her Bobbi
Why name her Bobbi
Why name her Bobbi

$ awk '!seen[$0]++' sample3.txt 
My name is Adam
Why name him Josh
Her name is Catherine
Why name her Bobbi

				
			

Replace a specific string in a file

  • Replace the old string with the new string in the filename and print the result
				
					awk '{gsub(/old string/, "new string")} 1' filename
				
			
				
					$ cat sample2.txt 
My name is Adam
Why name him Josh
Her name is Catherine
Why name her Bobbi

$ awk '{gsub(/Adam/, "Oscar")} 1' sample2.txt 
My name is Oscar
Why name him Josh
Her name is Catherine
Why name her Bobbi

				
			

Conclusion

AWK is a widely used and powerful text processing tool in the Linux environment, thanks to its numerous features such as pattern matching, field separation, built-in functions, and output formatting. The 10 popular AWK examples provided in this article are just a glimpse of the vast possibilities and applications of this tool. We hope this article was helpful to you, and we invite you to leave a comment below and share this article!

Related Posts

Leave a Reply

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