teluzia.Cloud Engineer · Day 4
CHAPTER 01 · LINUX FUNDAMENTALS AND GIT

Grep, sed and awk

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

Text tools make logs and configuration inspectable without opening every file by hand. grep selects matching lines, sed transforms a stream, and awk understands records and fields. You will use a three-row synthetic access log rather than real customer data. Each tool will answer a different question: which request failed, how a label changes, and how many requests had a chosen status. Keeping the original fixture unchanged makes the outputs easy to verify. Your deliberate fault uses the wrong field separator, so you can see how a syntactically valid command can still compute the wrong result.

Think of it this way

grep is a highlighter, sed is a copy editor, and awk is a clerk who reads columns and calculates totals.

TOPIC 01 / 03Step by step

grep line selection

grep searches input for a pattern and emits matching lines. Plain patterns use regular-expression rules, so punctuation such as a dot can match more than its literal character. Use grep -F when searching for exact text, and grep -n when line numbers help investigation. A status of zero means a match was found, one means no match, and a higher status indicates an error. This difference matters in scripts: an empty result is not necessarily a broken tool. Prefer anchored or field-aware patterns when a loose match could select an unrelated status or identifier.

WORKED EXAMPLE
grep -n " 500 " access.log selects the failure line with its line number.
Why this matters

Precise selection narrows large evidence sets and avoids mistaking a coincidental substring for the status, host or identifier you actually intended to inspect.

A common trap

Using grep 500 can match a byte count or path containing 500; anchor the relevant structure or use awk for field-aware selection.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

grep line selection

grep searches input for a pattern and emits matching lines. Plain patterns use regular-expression rules, so punctuation such as a dot can match more than its literal character. Use grep -F when searching for exact text, and grep -n when line numbers help investigation. A status of zero means a match was found, one means no match, and a higher status indicates an error. This difference matters in scripts: an empty result is not necessarily a broken tool. Prefer anchored or field-aware patterns when a loose match could select an unrelated status or identifier.

WORKED EXAMPLE
grep -n " 500 " access.log selects the failure line with its line number.

Why this matters

Precise selection narrows large evidence sets and avoids mistaking a coincidental substring for the status, host or identifier you actually intended to inspect.

A common trap

Using grep 500 can match a byte count or path containing 500; anchor the relevant structure or use awk for field-aware selection.

sed stream editing

sed applies editing commands to each input line and writes the resulting stream. A substitution such as s/ERROR/FAIL/ changes the first matching occurrence on each line; a trailing g changes every match on that line. By default, sed does not edit the source file. That is a useful safety property: redirect to a new filename and compare before replacing anything. The delimiter can be changed when patterns contain slashes. Shell quoting protects sed expressions from unintended expansion. In-place editing is convenient later, but it should follow a verified transformation and a backup plan.

WORKED EXAMPLE
sed "s/GET/FETCH/" access.log > transformed.log; the source still contains GET.

Why this matters

Previewing transformed output makes bulk edits reviewable and prevents accidental corruption when a replacement expression matches more text than you initially expected today.

A common trap

Redirecting sed output into its own input filename truncates that file before sed reads it; write to a distinct output path first instead.

awk records and fields

awk reads records, usually lines, and divides each record into fields. Its default separator treats runs of whitespace as separators; -F supplies a different separator. Field $1 is the first field, while $0 is the complete record. Patterns select records, actions perform work, and END runs after all input is processed. A counter or sum can therefore summarize a log in one pass. A valid expression can return zero because the wrong separator or column was chosen. Before aggregating, print a few fields and confirm their meanings against the fixture.

WORKED EXAMPLE
awk '$3 == 500 {n++} END {print n+0}' access.log prints 1 for the fixture.

Why this matters

Field-aware processing supports reliable summaries and encourages checking schema assumptions before trusting totals derived from logs or exported operational data in practice sessions.

A common trap

Setting a comma separator for a whitespace log produces one field per row; inspect field counts instead of assuming a zero total means success.

KEEP THESE WITH YOU

Today’s takeaways

Choose tools according to selection, transformation or aggregation.

Inspect field structure before trusting counts.

Write transformations to a separate file first.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed