Operations 9 min read

Mastering Awk: How to Filter Text with Regular Expressions on Linux

This guide explains how to use the awk command together with regular expressions to filter and manipulate text in Unix/Linux, covering basic syntax, meta‑characters, pattern matching, character classes, anchors, escaping, and practical examples for common filtering tasks.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering Awk: How to Filter Text with Regular Expressions on Linux

When using Unix/Linux commands to read or edit text, you often need to filter the output to obtain the parts you are interested in; regular expressions are perfect for this purpose.

What is a regular expression?

A regular expression is a string that represents a set of character sequences, allowing you to filter command or file output, edit parts of text, or process configuration files.

Features of regular expressions

Regular expressions consist of ordinary characters and meta‑characters.

Ordinary characters , such as spaces, underscores, A‑Z, a‑z, 0‑9.

Meta‑characters include: (.) – matches any single character except a newline. (*) – matches zero or more occurrences of the preceding character. [character(s)] – matches any character listed inside the brackets; ranges can be specified with a hyphen, e.g., [a-f] or [1-5]. ^ – matches the beginning of a line. $ – matches the end of a line. \ – escape character.

Using awk as a text‑filtering tool

Awk can be used as a programming language for text filtering. Its general syntax is: # awk 'script' filename In the script, the format is /pattern/ action, where pattern is a regular expression and action is the command executed when the pattern matches a line.

Basic awk examples

Print all lines of /etc/hosts (no pattern specified):

# awk '//{print}' /etc/hosts
awk prints all lines
awk prints all lines

Print lines containing localhost:

# awk '/localhost/{print}' /etc/hosts
awk prints matching lines
awk prints matching lines

Use the wildcard (.) to match any single character (e.g., l.c matches loc, localhost, localnet):

# awk '/l.c/{print}' /etc/hosts
awk wildcard example
awk wildcard example

Use the asterisk (*) to match zero or more occurrences (e.g., l*c matches lc, lcc, llc):

# awk '/l*c/{print}' /etc/hosts
awk asterisk example
awk asterisk example

Use a character set [al1] to match any of the listed characters:

# awk '/[al1]/{print}' /etc/hosts
awk character set example
awk character set example

Match lines starting with K or k followed by T:

# awk '/[Kk]T/{print}' /etc/hosts
awk range example
awk range example

Use ranges to specify character classes, e.g., [0-9] for digits, [a-z] for lowercase letters, [A-Z] for uppercase letters, [a-zA-Z] for any letter, [a-zA-Z0-9] for alphanumeric.

# awk '/[0-9]/{print}' /etc/hosts
awk numeric range example
awk numeric range example

Use the caret ^ to match the start of a line:

# awk '/^fe/{print}' /etc/hosts
awk start‑anchor example
awk start‑anchor example

Use the dollar sign $ to match the end of a line:

# awk '/ab$/{print}' /etc/hosts
awk end‑anchor example
awk end‑anchor example

Escape special characters with \. For example, to match a literal dollar sign:

# awk '/$25.00/{print}' deals.txt
# awk '/\$25.00/{print}' deals.txt
awk escape example
awk escape example

Summary

The examples above cover the basic operations of awk as a filtering tool. More advanced features will be introduced in later sections.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

regextext processingawk
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.