Lesson 11 / 29

sed, awk & Piping

Transform text and chain commands together.

sed — substitute text

sed 's/old/new/' replaces the first match per line; add g to replace all matches.

echo "cat sat on mat" | sed 's/at/og/'

Output:

cog sat on mat

awk — pull out a field

awk splits each line into fields by whitespace ($1, $2, ...) and lets you print or process them.

ps aux | awk '{print $2, $11}'

Output:

1 /sbin/init
842 /usr/bin/bash

Piping commands together

| sends one command's output as the next command's input, letting small tools combine into powerful pipelines.

cat access.log | grep "404" | wc -l

Output:

37

Quick check: What does `sed 's/foo/bar/g'` do?

  • Replaces only the first 'foo' per line
  • Replaces every 'foo' with 'bar' on each line
  • Deletes lines containing 'foo'
Answer

Replaces every 'foo' with 'bar' on each line — The `g` flag makes the substitution global — all matches per line, not just the first.