CloudsArk
Commands Linux

What Is the awk Command in Linux?

Learn what the awk command does in Linux, how its syntax works, and when to use it.

What Is the awk Command in Linux?

Introduction

The awk command processes structured text by records and fields. It is useful for beginners, Linux administrators, DevOps engineers, and RHCSA students because it solves practical terminal tasks.

What the Command Does

Use awk to work with the specific Linux object it manages. Before changing anything, identify the target and run a read-only check when possible.

Basic Syntax

awk 'PATTERN { ACTION }' FILE

The syntax includes the command, any options, and the target object.

Common Options

  • -F: set the field separator.
  • NR: current record number.
  • print: print selected fields or values.

Practical Examples

awk '{ print $1 }' /etc/passwd
awk -F: '{ print $1, $7 }' /etc/passwd
awk '$5 > 1000 { print $1, $5 }' data.txt
df -h | awk 'NR>1 { print $1, $5 }'

Verification command:

awk --version

Example output:

root
bin
daemon

When to Use This Command

Use awk when text is arranged in columns or delimited fields and you need to print, filter, or calculate values. It is useful with /etc/passwd, CSV-like files, and command output.

Common Mistakes

  • Forgetting that awk splits on whitespace by default unless -F is set.
  • Using double quotes around the awk program and letting the shell expand $1.
  • Trying to parse complex formats when a dedicated parser would be safer.

Quick Reference

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

Summary

The awk command is safest when you understand the target, choose the right option, and verify the result with a separate command.