AWK

AWK is a powerful text-processing language that is primarily used for pattern scanning and processing. It provides a compact and expressive syntax for manipulating structured data, such as columns in a CSV file or fields in a log file. AWK operates on a line-by-line basis, where it reads input lines, applies patterns, and executes actions based on those patterns.

Here are a few examples that demonstrate the usage of AWK in different scenarios:

  1. Print Specific Columns: Suppose we have a CSV file (data.csv) with the following content:

Name,Age,City
John,35,New York
Jane,28,Los Angeles
Alice,42,San Francisco
Bob,31,Chicago

We can use AWK to extract specific columns from the CSV file. For example, to print only the "Name" and "City" columns, we can use the following AWK command:

awk -F',' '{print $1, $3}' data.csv

The -F',' option sets the field separator to a comma (since it's a CSV file). The command will output:

Name City
John New York
Jane Los Angeles
Alice San Francisco
Bob Chicago
  1. Filtering Rows with Conditions: Suppose we have a log file (access.log) containing web access logs, and we want to extract only the lines with a specific HTTP response code (e.g., 200). We can use AWK to filter the rows based on a condition. For example:

awk '$9 == 200' access.log

This AWK command checks if the 9th field ($9) of each line is equal to 200. It will print only the lines that satisfy this condition.

  1. Calculating Sum or Average: Suppose we have a file (numbers.txt) with multiple numbers, one per line, and we want to calculate the sum and average of those numbers using AWK. We can use the following commands:

To calculate the sum:

awk '{ sum += $1 } END { print "Sum:", sum }' numbers.txt

To calculate the average:

awk '{ sum += $1 } END { avg = sum / NR; print "Average:", avg }' numbers.txt

These AWK commands accumulate the values of the first field ($1) and use the END block to perform calculations. The NR variable represents the total number of records (lines) in the file.

These examples provide a glimpse into the usage of AWK for text processing, column extraction, filtering, and basic calculations. AWK is a versatile tool that can be used in various data manipulation and analysis tasks. Its capabilities extend beyond these simple examples, allowing you to handle more complex scenarios involving patterns, regular expressions, user-defined functions, and more.

Last updated