typestar

sed in Bash

The stream editor: substitute, delete, select.

text='the quick brown fox
jumps over the lazy dog'

# s///: substitute the first match per line, /g for all
echo "$text" | sed 's/the/THE/'
echo "$text" | sed 's/the/THE/g'

# address ranges pick which lines to touch; d deletes
echo "$text" | sed '2s/lazy/alert/'
echo "$text" | sed '1d'

# -n plus p prints only what you ask for
echo "$text" | sed -n '/fox/p'

How it works

  1. s/old/new/ replaces the first match per line; /g all of them.
  2. Addresses like 2s/... and 1d pick their lines.
  3. -n with p prints only the lines you ask for.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
360
Tokens
43
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 34 seconds.

Type this snippet

Step 2 of 3 in Text tools, step 20 of 26 in Language basics.

← Previous Next →