Bash Stream Editor
sed is a non-interactive stream editor that rewrites text on the fly, most often through the substitution command s/old/new/.
What Is sed?
sed reads its input one line at a time, applies the editing commands you give it, and prints the result to standard output. It never modifies the original file unless you explicitly tell it to with -i, which makes it safe to experiment with. These examples reuse the same team.csv file of employee records used throughout this section.
Example
$ cat team.csv
name,department,age,salary
Alice Johnson,Engineering,29,85000
Bob Smith,Marketing,34,62000
carol white,engineering,41,95000
David Lee,Sales,26,54000
Eve Davis,Marketing,38,71000
Frank Miller,Engineering,45,110000
grace hopper,Sales,31,67000
$ sed 's/Marketing/Retail/' team.csv
name,department,age,salary
Alice Johnson,Engineering,29,85000
Bob Smith,Retail,34,62000
carol white,engineering,41,95000
David Lee,Sales,26,54000
Eve Davis,Retail,38,71000
Frank Miller,Engineering,45,110000
grace hopper,Sales,31,67000Replacing Every Match with the g Flag
By default sed replaces only the first match it finds on each line, then moves on. That was invisible in the last example because each line contained at most one Marketing. Adding g after the closing slash tells sed to replace every match on the line, which matters as soon as a pattern can appear more than once, like the commas in a CSV row.
Example
$ sed 's/,/;/g' team.csv
name;department;age;salary
Alice Johnson;Engineering;29;85000
Bob Smith;Marketing;34;62000
carol white;engineering;41;95000
David Lee;Sales;26;54000
Eve Davis;Marketing;38;71000
Frank Miller;Engineering;45;110000
grace hopper;Sales;31;67000Editing Files in Place with -i
Piping sed's output to a new file works fine, but when you want to update the original file directly, add -i. Giving -i a suffix, such as -i.bak, makes sed save a backup of the original before overwriting it, which is the safest way to edit in place.
Example
$ sed -i.bak 's/Sales/Business Development/g' team.csv
$ cat team.csv
name,department,age,salary
Alice Johnson,Engineering,29,85000
Bob Smith,Marketing,34,62000
carol white,engineering,41,95000
David Lee,Business Development,26,54000
Eve Davis,Marketing,38,71000
Frank Miller,Engineering,45,110000
grace hopper,Business Development,31,67000
$ ls
team.csv team.csv.bak- Delete matching lines entirely with sed '/pattern/d' - for example, sed '/Sales/d' team.csv prints every row except the Sales department, without touching the original file.
- Print a line range without editing anything: sed -n '2,4p' team.csv shows only lines 2 through 4; -n suppresses sed's normal automatic printing so only the lines you explicitly print with p appear.
- Chain multiple edits in one command with -e: sed -e 's/,/;/g' -e 's/;$//' team.csv runs both substitutions in a single pass.
- Use a different delimiter when the pattern itself contains slashes, e.g. sed 's#/usr/local#/opt#' avoids escaping every forward slash.
- Add a number after g, like s/,/;/2, to replace only the second occurrence onward on each line instead of every occurrence.