Skip to content

gawk command in Linux

gawk is a versatile command-line tool used for processing and analyzing text files in Linux. It excels at working with structured data, allowing users to manipulate fields, filter records, and generate reports. With its powerful features like pattern scanning and text manipulation, gawk is a valuable tool for data processing tasks, such as parsing logs, extracting specific information, and transforming data formats. Mastering gawk can greatly enhance your efficiency in handling text files within the Unix environment.

gawk Syntax:

Terminal window
gawk [options] [file(s)]

Linux gawk Options:

OptionDescription
-FSpecify a field separator
-fProvide a file with awk script
-vAssign a value to an awk variable
-WSpecify compatibility mode
-helpDisplay a help message

gawk Parameters:

ParameterDescription
file(s)Input file(s) to be processed by gawk

How to use gawk command:

Terminal window
awk '{print $1, $3}' file.txt

Prints the first and third fields from the file “file.txt”.

Terminal window
awk '$3 > 50 {print $0}' data.csv

Prints the entire line from “data.csv” if the third field is greater than 50.

Calculate Sum of a Column

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

Calculates the sum of the first column in “numbers.txt” and prints the total.

Find and Replace Text in a File

Terminal window
gawk '{gsub(/old_word/, "new_word")} 1' textfile.txt

Replaces all occurrences of “old_word” with “new_word” in “textfile.txt”.

Terminal window
gawk '/keyword/' example.txt

Prints lines from “example.txt” containing the keyword “keyword”.

Filter Records Based on a Condition

Terminal window
gawk '$2 == "John" {print $0}' sales.csv

Prints records from “sales.csv” where the second field is equal to “John”.

Merge Two Files Based on a Key

Terminal window
gawk 'NR==FNR{a[$1]=$2;next} ($1 in a) {print $0, a[$1]}' file1.txt file2.txt

Merges two files “file1.txt” and “file2.txt” based on a common key in the first column.

Custom Field Separator

Terminal window
gawk -F: '{print $1, $3}' /etc/passwd

Uses ”:” as the field separator to print the first and third fields from “/etc/passwd”.

How do I use gawk in Linux?

To use the gawk command in Linux, execute the following command:

Terminal window
gawk --option <value>

How can I print a specific column using gawk?

To print a specific column using gawk, you can use the following command:

Terminal window
gawk '{print $1}' file.txt

How do I filter lines based on a specific condition with gawk?

To filter lines based on a specific condition using gawk, you can use the following command:

Terminal window
gawk '$3 > 50 {print $0}' file.txt

How can I use gawk to perform arithmetic operations?

To perform arithmetic operations using gawk, you can use the following command:

Terminal window
gawk '{sum+=$1} END {print sum}' file.txt

How do I specify a custom field separator with gawk?

To specify a custom field separator using gawk, you can use the following command:

Terminal window
gawk -F':' '{print $1}' file.txt

How can I process multiple input files with gawk?

To process multiple input files using gawk, you can use the following command:

Terminal window
gawk '{print $0}' file1.txt file2.txt

How do I use gawk to replace text in a file?

To replace text in a file using gawk, you can use the following command:

Terminal window
gawk '{sub("old", "new", $0); print $0}' file.txt

How can I format output using gawk?

To format the output using gawk, you can use the following command:

Terminal window
gawk '{printf "Name: %-10s Age: %d\n", $1, $2}' file.txt

How do I execute a script file with gawk?

To execute a script file using gawk, you can use the following command:

Terminal window
gawk -f script.awk input.txt

Applications of the gawk command

  • Text processing
  • Pattern scanning
  • Processing structured data
  • Generating reports and data extraction
  • Field and record manipulation