Thuta Learning
IntermediateDevOps & Toolsbeginner

sed & awk Basics

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Get comfortable with sed & awk basics — no need to fear them
  • Practice running the commands yourself in the terminal
  • See how these commands come in handy on a real server or project

Let's think about it this way for a second

The most common sed (stream editor) pattern is sed 's/old/new/' file — it replaces 'old' text in the file with 'new' (s = substitute). Adding the g flag (sed 's/old/new/g') replaces every match in each line, whereas without g, only the first match on each line gets replaced. awk, on the other hand, is built for column-based data (CSV, log files) — awk '{print $1}' file prints only the first column ($1) of every line, and you can access columns by number with $2, $3, and so on.

Let's connect this to a real-world scenario

If you want to replace localhost with production-server.com in a config file, use sed 's/localhost/production-server.com/g' config.txt (this only shows the output; adding the -i flag actually edits the file) — it's safer to take a backup before running sed -i. ps aux | awk '{print $2, $11}' extracts just the PID (column 2) and command name (column 11) from the process list — awk is commonly paired with a pipe whenever you need to pull out specific columns from a log or CSV file.

Let's try it together in the terminal

bash
echo "hello world" | sed 's/world/linux/'
sed 's/localhost/prod.example.com/g' config.txt
ps aux | awk '{print $2, $11}'
echo "a,b,c" | awk -F',' '{print $2}'
You should see
Running sed on hello world will show it transformed into 'hello linux' in the terminal.

5-Minute Try-It

Create the line 'I like cats' with echo, then pipe it into sed 's/cats/dogs/' and run it.

A Quick Word of Caution

Before running sed -i (in-place edit) on a production config file, the safest move is to cp file file.backup first.

Easy traps

  • Running sed -i directly on a big config file without taking a backup first, then struggling to undo it
  • Assuming awk column numbers are 0-indexed (Python-style) — awk actually starts at $1, not 0

Now Try It Yourself

Create the line 'I like cats' with echo, then pipe it into sed 's/cats/dogs/' and run it.

You'll know it worked when: Running sed on hello world will show it transformed into 'hello linux' in the terminal.

sed & awk Basics | Thuta Learning