awk Step by Step: A Simple Guide for Linux Beginners

 

Working with text files is a daily task in Linux.
Log files, user lists, reports, and command output are all plain text.

The awk command is one of the simplest and most useful tools for working with that text.

In this guide, you will learn how to use awk one small step at a time.
We will start with the simplest working examples and build only what you need for real IT work.

No shortcuts.
No advanced theory.
Just clear, practical commands you can use today.


What awk Is Used For

awk reads text line by line and lets you pick out specific parts of each line.

In IT, this is used to:

  • Extract columns from files

  • Filter lines from logs

  • Clean up command output

  • Create simple reports

We will begin with a small file that looks like real system data.


Create a Simple Test File

First, create a file named users.txt.

cat > users.txt

Type the following lines:

alice 1001 admin 
bob 1002 user
carol 1003 user

Press Ctrl+D to save.

This file has three columns:

  • Column 1: name

  • Column 2: ID number

  • Column 3: role

Each space separates one column.

This is similar to a user list or a report file in IT.


Print the Whole File with awk

The simplest awk program prints every line.

awk '{print}' users.txt

Output:

alice 1001 admin 
bob 1002 user
carol 1003 user

Between the braces is the action.
print means print the entire line.

This shows the basic structure of every awk command.


Print Only the First Column

In awk, each column is called a field.

  • $1 is the first field

  • $2 is the second field

  • $3 is the third field

awk '{print $1}' users.txt

Output:

alice 
bob
carol

$1 means the first column of each line.

This is how you extract usernames from files or command output.


Print a Different Column

To print the second column:

awk '{print $2}' users.txt

Output:

1001 
1002
1003

Change the number to select any column.

This is useful for extracting IDs, process numbers, or port numbers.


Print Multiple Columns

You can print more than one field.

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

Output:

alice admin 
bob user
carol user

The comma separates fields and adds a space.

This is common when creating clean reports from messy data.


Show the Line Number

awk keeps track of the current line number.
This is called NR.

awk '{print NR, $1}' users.txt

Output:

1 alice 
2 bob
3 carol

NR means record number, which is the line number.

This helps when debugging files or matching errors to line numbers.


Filter Lines with a Condition

You can tell awk to print only lines that match a condition.

For example, only users with role admin:

awk '$3 == "admin" {print $1, $3}' users.txt

Output:

alice admin

The condition comes first.
Only matching lines are printed.

This is how you filter logs, users, or service states.


Use awk with a Command Pipeline

awk is often used with other commands using a pipe.

ps aux | awk '{print $1}'

Example output:

USER 
root
root
daemon

A pipe sends the output of one command into awk.

This is common in monitoring and troubleshooting processes.


Change the Field Separator

By default, awk splits fields on spaces.

Let’s work with a CSV file.

cat > data.csv

Type:

alice,1001,admin 
bob,1002,user

Press Ctrl+D.

Now run:

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

Output:

alice admin 
bob user

-F, tells awk that fields are separated by commas.

This is how you work with CSV files in IT.


Common Beginner Mistakes

Forgetting quotes around the program

Wrong:

awk {print $1} users.txt

Correct:

awk '{print $1}' users.txt

The braces must be inside quotes.


Confusing field numbers

$1 is the first column, not the second.
Counting always starts at one.


Using the wrong field separator

If your file uses commas or colons, you must set -F correctly.


A Practical Real-World Example

Suppose you have a system file with users and roles, and you only want admin usernames.

awk '$3 == "admin" {print $1}' users.txt

Output:

alice

This gives you a clean list of admin users.

This is useful for audits, scripts, and reporting.


A Small Next Step

You can also format the output.

awk '{print "User:", $1, "Role:", $3}' users.txt

Output:

User: alice Role: admin 
User: bob Role: user
User: carol Role: user

This shows that awk can format text, not just extract it.


Conclusion

You now know how to:

  • Print fields

  • Select specific columns

  • Filter lines with conditions

  • Work with piped commands

  • Handle CSV files

These are the core awk patterns used in real IT work.

Practice these commands a few times.
With repetition, they will become natural.