What sed Actually Is
sed is short for 'stream editor'. Unlike a text editor such as vim or nano, sed never opens a file for interactive editing. Instead it reads input one line at a time, applies a set of editing commands you supply in advance, and writes the (possibly transformed) result to standard output. Because it is non-interactive and reads a stream, sed is ideal for automation, scripts, and pipelines where no human is present to press keys.
Cricket analogy: Like a third umpire who reviews each delivery against fixed rules and signals out or not-out, sed judges every line against commands you set beforehand rather than improvising ball by ball.
The Line-by-Line Model
sed's core loop is simple and worth memorising: read one line into a working buffer called the pattern space, run every command in your script against that buffer, then (by default) print the buffer and move on to the next line. This cycle repeats until the input is exhausted. Understanding that sed sees only one line at a time by default explains why some tasks feel awkward and why sed has a second buffer, the hold space, for carrying data between lines.
Cricket analogy: Each over is bowled one ball at a time and the scorer records the outcome before the next ball; sed likewise handles one line, prints the result, then faces the next delivery.
A First Look at the Syntax
# Replace the first 'cat' on each line with 'dog', printing the result
echo 'the cat sat on the cat mat' | sed 's/cat/dog/'
# -> the dog sat on the cat mat
# Read from a file and stream the transformed text to stdout
sed 's/ERROR/WARN/' server.logsed reads from standard input when no filename is given, which is why it slots so naturally into pipelines with a leading pipe (|). Give it one or more filenames and it processes each in turn, concatenating the output.
Where sed Fits Among Its Cousins
sed belongs to the classic Unix text-processing trio alongside grep and awk. grep decides whether a line matches and prints or drops it; sed transforms lines, most famously via substitution; awk is a fuller programming language for field-based records and arithmetic. A good rule of thumb is: reach for grep to filter, sed to do quick find-and-replace or line edits, and awk when you need columns, variables, and computation. They compose beautifully because all three read and write plain text streams.
Cricket analogy: grep is the selector picking who plays, sed is the physio making a quick adjustment to a player's grip, and awk is the head coach running full tactical analysis with statistics.
sed writes to standard output and does not modify your files by default. To edit a file in place you must pass the -i option, which is easy to forget and irreversible without a backup — always test your script on stdout first.
- sed is a non-interactive stream editor that transforms text line by line.
- Its core cycle reads a line into the pattern space, runs your commands, then prints and repeats.
- By default sed prints to standard output and leaves the original file untouched.
- With no filename, sed reads stdin, making it a natural fit inside pipelines.
- sed pairs with grep (filter) and awk (field programming) as the Unix text trio.
- Substitution (s///) is sed's signature and most-used command.
Practice what you learned
1. What does the name 'sed' stand for?
2. By default, what does sed do with the file you give it?
3. Which buffer holds the current line while sed applies its commands?
4. Which tool is best suited purely for filtering lines that match a pattern?
5. What happens when sed is run without any filename argument?
Was this page helpful?
You May Also Like
Running sed Scripts
How to invoke sed from the command line and from script files, including the key options that control input, output, and multi-command execution.
sed Substitution Basics
A practical guide to the s command — sed's find-and-replace engine — covering delimiters, flags, backreferences, and the ampersand.
Your First sed One-Liner
Build a genuinely useful sed one-liner from scratch, combining an address, a substitution, and options into a single practical command.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics