Cart 0
No products in the cart.

How to use gawk command in Linux ?

How to use gawk command in Linux ?

Gawk is a powerful Linux command-line tool for manipulating text and data. It is used for extracting data from files, computing statistics, and other text processing tasks. Gawk stands for GNU AWK, which is a version of AWK (an interpreted programming language) developed by the GNU Project. 

Here at Fixwebnode, we will explore the basics of Gawk, its options and how to use Gawk in Linux.

 

What is Gawk ?

Gawk is a programming language and a powerful command-line tool for manipulating text and data. It is a version of AWK (an interpreted programming language) developed by the GNU Project. Gawk is used for extracting data from files, computing statistics, and other text processing tasks. It is very helpful for automating tasks that involve text manipulation.

 

 

How to Use Gawk in Different Linux Distributions ?

Gawk is available on all Linux distributions and is used in the same way. The command syntax is the same and the only difference is the installation process of Gawk in different Linux distributions. 

 

On Ubuntu/Debian

Gawk is available in the Ubuntu/Debian repository and can be installed using the apt command:

$ sudo apt-get install gawk

 

On CentOS/Fedora/RHEL

Gawk is available in the CentOS/Fedora/RHEL repository and can be installed using the yum command:

$ sudo yum install gawk

 

Arch Linux

Gawk is available in the Arch Linux repository and can be installed using the pacman command:

$ sudo pacman -S gawk

 

Gawk syntax

Gawk is a command-line tool. To use it, you need to open a terminal window and type the gawk command followed by the options and arguments. The basic syntax for Gawk is:

$ gawk [options] [script] [files]

 

Gawk command Options

Options are used to enable or disable certain features of Gawk. 

  • Script is a text file containing Gawk commands. 
  • Files are the input files that will be processed by the Gawk commands.

 

How to use Gawk Options ?

Gawk has several options that you can use to customize its behavior. 

Here are some of the most common options:

  • -v var=value: Sets the variable ‘var’ to the given value.
  • -F fs: Sets the input field separator to the given character.
  • -f scriptfile: Reads the commands from the given file.
  • -W warning: Sets the warning level.
  • -o outfile: Writes the output to the given file.
  • -e script: Reads the commands from the given string.

 

Examples of using Gawk ?

1. Counting Lines in a File

Let's say you want to count the number of lines in a text file. You can use Gawk for this task. The following command will print the number of lines in the file:

$ gawk 'END {print NR}' filename

This command will read the file and print the number of lines at the end.

 

2. Extracting Columns from a File

You can use Gawk to extract columns from a file. The following command will print the first and third column from the file:

$ gawk '{print $1,$3}' filename

This command will print the first and third column from each line in the file.

 

3. Summing Numbers in a File

Gawk can also be used to compute the sum of numbers in a file. The following command will print the sum of numbers in the first column of the file:

$ gawk '{sum += $1} END {print sum}' filename

This command will read each line in the file and add the number in the first column to the sum. At the end, it will print the sum of all the numbers.

 

4. Reversing the Order of Columns

You can also use Gawk to reverse the order of columns in a file. The following command will reverse the order of columns in the file:

$ gawk '{for (i=NF; i>0; i--) printf("%s ",$i); printf(" ")}' filename

This command will print each line in the file in reverse order.

 

5. Printing Unique Lines

If you want to print only the unique lines in a file, you can use Gawk for this task. The following command will print only the unique lines in the file:

$ gawk '!seen[$0]++' filename

This command will read each line in the file and print only the lines that have not been seen before.

 

6. Translating Characters

Gawk can also be used to translate characters in a file. The following command will translate all lowercase letters to uppercase:

$ gawk '{print toupper($0)}' filename

This command will read each line in the file and print it with all the letters in uppercase.

 

[Need help fixing Linux system issues ? We can help you. ]

 



CONCLUSION

This article covers the basics of Gawk and its optionsas well as a few practical examples of how to use Gawk in Linux. In fact, Gawk is a powerful command-line tool that can be used for a variety of text processing tasks. It is a great tool for automating tasks that involve text manipulation.


icon

Sign up to Newsletter

...and receive a free support ticket.