Mastering AWK in Linux Scripting

What is AWK?

 

AWK is a domain-specific language designed for text processing and data extraction. It derives its name from the initials of its creators: Alfred Aho, Peter Weinberger, and Brian Kernighan. AWK operates on a per-line basis, making it highly suitable for tasks that involve pattern matching, text extraction, and reporting.

 

 

Basic Syntax

 

The basic structure of an AWK command is as follows:

awk ‘pattern { action }’ file

 

Pattern:  This specifies the condition that must be met for the associated action to be executed.

 

Action:  This defines what should be done when the pattern is matched.

 

File:  The input file on which AWK should operate. If not specified, AWK reads from standard input.

 

 

AWK  Examples

 

1. Print Lines Matching a Pattern:

Let’s say you have a log file named `access.log` and you want to print all lines containing the word “error.”

awk ‘/error/ { print }’ access.log

 

2. Print Specific Fields:

Suppose you have a CSV file named `data.csv` with records like `name,age,city` and you want to print only the names.

awk -F ‘,’ ‘{ print $1 }’ data.csv

 

3. Calculate and Print Average:

Consider a file named `grades.txt` containing student names and their grades. You want to calculate and print the average grade.

awk ‘{ total += $2 } END { print “Average grade:”, total/NR }’ grades.txt

 

4. Conditional Formatting:

Assume you have a file named `sales.txt` with sales data. You want to highlight sales above a certain threshold.

awk ‘$2 > 1000 { printf “%-20s: %d (High)\n”, $1, $2 }’ sales.txt

 
 
 
Automating Tasks with AWK

Here’s a simple example of using AWK in a script to automate a task. Let’s create a script called `analyze_log.sh` that analyzes an Apache access log to count the occurrences of different HTTP status codes.

#!/bin/bash

log_file=”access.log”

awk ‘{ status[$9]++ } END { for (code in status) { print code, status[code] ” occurrences” } }’ $log_file

 

Save the script, make it executable (`chmod +x analyze_log.sh`), and run it. It will read the `access.log` file and output the counts of different HTTP status codes.

 

 

You may also like:

https://hackedyou.org/advanced-linux-shell-scripting/

1 thought on “Mastering AWK in Linux Scripting”

Leave a Comment

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


Scroll to Top