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:
Section titled “gawk Syntax:”gawk [options] [file(s)]Linux gawk Options:
Section titled “Linux gawk Options:”| Option | Description |
|---|---|
| -F | Specify a field separator |
| -f | Provide a file with awk script |
| -v | Assign a value to an awk variable |
| -W | Specify compatibility mode |
| -help | Display a help message |
gawk Parameters:
Section titled “gawk Parameters:”| Parameter | Description |
|---|---|
| file(s) | Input file(s) to be processed by gawk |
How to use gawk command:
Section titled “How to use gawk command:”Print Specific Fields from a File
Section titled “Print Specific Fields from a File”awk '{print $1, $3}' file.txtPrints the first and third fields from the file “file.txt”.
Print if a Field Meets a Condition
Section titled “Print if a Field Meets a Condition”awk '$3 > 50 {print $0}' data.csvPrints the entire line from “data.csv” if the third field is greater than 50.
Calculate Sum of a Column
Section titled “Calculate Sum of a Column”awk '{sum += $1} END {print sum}' numbers.txtCalculates the sum of the first column in “numbers.txt” and prints the total.
Find and Replace Text in a File
Section titled “Find and Replace Text in a File”gawk '{gsub(/old_word/, "new_word")} 1' textfile.txtReplaces all occurrences of “old_word” with “new_word” in “textfile.txt”.
Print Lines Matching a Pattern
Section titled “Print Lines Matching a Pattern”gawk '/keyword/' example.txtPrints lines from “example.txt” containing the keyword “keyword”.
Filter Records Based on a Condition
Section titled “Filter Records Based on a Condition”gawk '$2 == "John" {print $0}' sales.csvPrints records from “sales.csv” where the second field is equal to “John”.
Merge Two Files Based on a Key
Section titled “Merge Two Files Based on a Key”gawk 'NR==FNR{a[$1]=$2;next} ($1 in a) {print $0, a[$1]}' file1.txt file2.txtMerges two files “file1.txt” and “file2.txt” based on a common key in the first column.
Custom Field Separator
Section titled “Custom Field Separator”gawk -F: '{print $1, $3}' /etc/passwdUses ”:” as the field separator to print the first and third fields from “/etc/passwd”.
How do I use gawk in Linux?
Section titled “How do I use gawk in Linux?”To use the gawk command in Linux, execute the following command:
gawk --option <value>How can I print a specific column using gawk?
Section titled “How can I print a specific column using gawk?”To print a specific column using gawk, you can use the following command:
gawk '{print $1}' file.txtHow do I filter lines based on a specific condition with gawk?
Section titled “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:
gawk '$3 > 50 {print $0}' file.txtHow can I use gawk to perform arithmetic operations?
Section titled “How can I use gawk to perform arithmetic operations?”To perform arithmetic operations using gawk, you can use the following command:
gawk '{sum+=$1} END {print sum}' file.txtHow do I specify a custom field separator with gawk?
Section titled “How do I specify a custom field separator with gawk?”To specify a custom field separator using gawk, you can use the following command:
gawk -F':' '{print $1}' file.txtHow can I process multiple input files with gawk?
Section titled “How can I process multiple input files with gawk?”To process multiple input files using gawk, you can use the following command:
gawk '{print $0}' file1.txt file2.txtHow do I use gawk to replace text in a file?
Section titled “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:
gawk '{sub("old", "new", $0); print $0}' file.txtHow can I format output using gawk?
Section titled “How can I format output using gawk?”To format the output using gawk, you can use the following command:
gawk '{printf "Name: %-10s Age: %d\n", $1, $2}' file.txtHow do I execute a script file with gawk?
Section titled “How do I execute a script file with gawk?”To execute a script file using gawk, you can use the following command:
gawk -f script.awk input.txtApplications of the gawk command
Section titled “Applications of the gawk command”- Text processing
- Pattern scanning
- Processing structured data
- Generating reports and data extraction
- Field and record manipulation