Discover how mastering the top 10 Linux tools for system administrators, including the powerful Bash and versatile Git, can help you efficiently manage networks, secure
Unlock the full potential of AWK with these popular commands that will streamline your text processing tasks and increase your productivity.
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.
AWK has many features that make it a popular tool for text processing. Some of its popular features include:
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
awk '/pattern/ {print}' filename
$ awk '/one/ {print}' sample.txt
This is line one
awk '{print $1}' filename
$ awk '{print $1}' sample.txt
This
This
This
This
awk '{print $NF}' filename
$ awk '{print $NF}' sample.txt
one
two
three
four
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
awk '{sum += $1; n++} END {print sum / n}' filename
$ awk '{sum += $1; n++} END {print sum / n}' numbers.txt
147.5
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
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
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
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
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
Discover how mastering the top 10 Linux tools for system administrators, including the powerful Bash and versatile Git, can help you efficiently manage networks, secure
Discover the fascinating world of open-source software and Linus Torvalds, the creator of the Linux kernel, through 10 surprising and little-known facts that will broaden
In this article, we will review the top 10 most commonly used Git commands. You can’t call yourself a competent DevOps Engineer or IT professional