Stream Editing with sed (search, replace, line deletion, insertion)

sed is a stream editor: it edits text as it flows past, without opening a file in an editor, and its signature move is the substitution s/old/new/, which finds a pattern and replaces it, one command that can transform whole files automatically.

11 min read · 8 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Editing without opening an editor

grep finds lines; often you then want to change them, replace a word, delete some lines, reformat, across a whole file, automatically. Opening the file in nano and editing by hand does not scale to thousands of lines or to running inside a script.

sed, the stream editor, is the answer. It edits text as it flows past it, transforming each line according to your instructions, with no interactive editor involved. Its most important skill is find-and-replace, and this closing lesson of the course shows how it works.

Theory

The substitution command

sed's signature is the substitution: s/old/new/. It finds old (a pattern) and replaces it with new. By default it replaces only the first match on each line.

Add the g flag, s/old/new/g, and it replaces all matches on each line ('g' for global). This one command, applied to a whole file or stream, can rename a term everywhere, fix a typo throughout, or reformat data. The pattern part is a regular expression, so everything you learned about regex applies here too.

Practical

sed substitution and deletion (verified)

# Replace only the FIRST match on the line
$ echo "cat cat cat" | sed 's/cat/dog/'
dog cat cat

# Replace ALL matches with the g flag
$ echo "cat cat cat" | sed 's/cat/dog/g'
dog dog dog

# Delete line 2 with a line address
$ printf "one\ntwo\nthree\n" | sed '2d'
one
three

Formula

The g flag, and line addresses

Two ideas unlock most of sed. The g flag decides scope: s/old/new/ changes the first match per line, s/old/new/g changes every match. Forgetting g when you meant 'all' is the most common sed slip.

Line addresses decide WHERE a command acts: 2d deletes line 2, /ERROR/d deletes every line matching ERROR, and 1,5s/old/new/g substitutes only in lines 1 to 5. Also note: by default sed prints its result to the screen and does NOT change the file; add -i to edit the file in place (carefully, there is no undo).

Quiz

What does echo "cat cat cat" | sed 's/cat/dog/' output (note: no g flag)?

  1. dog dog dog, replacing every cat
  2. dog cat cat, replacing only the first cat on the line
  3. cat cat cat, unchanged
  4. An error, because sed needs a file
Show the answer

dog cat cat, replacing only the first cat on the line

Without the g flag, sed's s/old/new/ replaces only the FIRST match on each line, so 'cat cat cat' becomes 'dog cat cat', just the first cat is changed. Option A would be correct only WITH the g flag (s/cat/dog/g), which replaces all matches to give 'dog dog dog'; that one letter is the difference. Option C is wrong: sed does perform the substitution; the line is not left unchanged. Option D is wrong: sed reads happily from a pipe (standard input), it does not require a file argument. Remember the rule: no g means first match only; add g for all matches on the line.

Think first

Why use sed instead of just editing the file by hand?

For a small change, nano is easy. When and why is sed the better choice? Then tap.

Show the answer

sed wins whenever the edit must be AUTOMATIC, REPEATABLE, or applied at SCALE, because it is non-interactive and fits into pipelines and scripts. Editing by hand in nano is fine for one file and one change, but it does not scale: imagine renaming a term across 500 configuration files, or fixing the same typo in a log every night, or transforming data as it streams through a pipeline, no human is going to open each file and retype. sed does it in one command, and because it takes its instructions on the command line, you can put it in a SCRIPT and run it a thousand times identically, or chain it after grep in a pipeline (grep the interesting lines, then sed to reshape them). It also works on data that never becomes a file at all, text flowing between commands. The trade-off is that hand-editing lets you see and judge each change, while sed applies its rule blindly, so you test the substitution first (without -i, watching the screen output) before committing it in place. In short: for a one-off tweak, an editor is fine; for anything automated, bulk, or inside a pipeline or script, sed is the right, powerful tool. Automate the edit, and it scales to any size.

Theory

BCA501 complete: you can live on the terminal

You started unable to navigate a Linux box and now you can: move around the file system, manage files and permissions, control processes, schedule jobs, write shell scripts with logic and loops, and wield regular expressions with grep and sed to search and transform text. That is the full arc of this course. A powerful next step is combining tools, for example grep "ERROR" log.txt | sed 's/ERROR/PROBLEM/', finding lines then reshaping them. Keep practising on the campus server; fluency comes from daily use.

Summary

Key takeaways

  • sed is a stream editor: it transforms text as it flows past, non-interactively, with no editor to open.
  • Its signature is substitution: sed 's/old/new/' replaces the first match on each line.
  • Adding the g flag, sed 's/old/new/g', replaces ALL matches on each line.
  • Line addresses select where to act: 2d deletes line 2, /pattern/d deletes matching lines, 1,5s/// limits by range.
  • By default sed prints its result and does not change the file; -i edits in place (no undo, so test first).
  • sed shines for automatic, repeatable, large-scale edits and inside pipelines (often after grep).
  • Memory hook: s/old/new/ first match, add g for all; test before -i.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Advanced Text Processing Tools

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Stream Editing with sed (search, replace, line deletion, insertion) · Linux Operating System (LOS) (Minor-04) · Gri-Learn