DEV Community

Adnan al-emran ontor
Adnan al-emran ontor

Posted on

AWK Cheat Sheet

What is AWK?

awk is a powerful text-processing tool in Linux used to manipulate and analyze text files by processing patterns and performing actions.


Basic Syntax


awk 'pattern {action}' file

Enter fullscreen mode Exit fullscreen mode
  • pattern: The condition to match (optional).
  • action: Commands to execute on matching lines (optional).
  • file: The file to process.

Common Examples

1. Print All Lines


awk '{print}' file.txt

Enter fullscreen mode Exit fullscreen mode
  • Outputs all lines in file.txt.

2. Print Specific Columns

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

Enter fullscreen mode Exit fullscreen mode
  • Prints the 1st and 3rd columns.

3. Match a Pattern


awk '/error/ {print}' log.txt

Enter fullscreen mode Exit fullscreen mode
  • Prints lines containing the word error.

4. Conditional Filtering


awk '$3 > 100 {print $1, $3}' data.txt

Enter fullscreen mode Exit fullscreen mode
  • Prints rows where the 3rd column is greater than 100.

5. Add or Modify Columns


awk '{print $1, $2, $2 + $3}' data.txt

Enter fullscreen mode Exit fullscreen mode
  • Adds a column with the sum of the 2nd and 3rd columns.

6. Count Matching Lines


awk '/pattern/ {count++} END {print count}' file.txt

Enter fullscreen mode Exit fullscreen mode
  • Counts and prints lines matching pattern.

7. Format Output


awk '{printf "Name: %s, Age: %d\n", $1, $2}' names.txt

Enter fullscreen mode Exit fullscreen mode
  • Formats and prints fields with a custom output.

8. Use Field Separator


awk -F ':' '{print $1, $NF}' /etc/passwd

Enter fullscreen mode Exit fullscreen mode
  • Uses : as a separator and prints the 1st and last columns.

Key Concepts

Symbol Meaning
$1, $2 Represents the 1st, 2nd columns (fields).
NR Line number (e.g., NR == 5 matches line 5).
NF Total number of fields in a line.
FS Field Separator (-F or FS=",").
BEGIN Executes before processing lines.
END Executes after processing all lines.

Advanced Examples

Print Line Numbers


awk '{print NR, $0}' file.txt

Enter fullscreen mode Exit fullscreen mode
  • Prints line numbers alongside the content.

Find Maximum in a Column


awk '$3 > max {max = $3} END {print max}' data.txt

Enter fullscreen mode Exit fullscreen mode
  • Finds and prints the maximum value in the 3rd column.

Sum a Column


awk '{sum += $3} END {print sum}' data.txt

Enter fullscreen mode Exit fullscreen mode
  • Sums up all values in the 3rd column.

Filter by Field Count


awk 'NF == 3 {print}' file.txt

Enter fullscreen mode Exit fullscreen mode
  • Prints lines with exactly 3 fields.

Practice Data

Create a sample.txt to test commands:


John 25 500
Mary 30 600
Alex 28 450

Enter fullscreen mode Exit fullscreen mode

Tips

  • Test commands with awk on small files before applying to large ones.
  • Combine with other commands like grep or sed for complex workflows.

Top comments (0)