Skip to main content
Enhance your text-processing workflow by using sed to search, print, or delete specific patterns directly within files. While similar to grep, sed lets you combine pattern matching with editing commands in one step. To follow along, we’ll use an employees.txt file with records formatted as ID|First|Last|Department|Role|Email|Salary.
The image shows a text file named "employees.txt" containing a list of employees with details such as name, department, job title, email, and salary.

1. Basic Search Syntax

sed requires both a pattern and an action. The minimal form is:
Without an explicit action, sed may default to printing every line or throw an error:
To print only matching lines, use -n (quiet mode) with the p command:
  • -n : suppress automatic printing
  • /Manager/ : search for “Manager”
  • p : print matching lines
By default, sed performs case-sensitive matches. Searching for manager (lowercase) yields no results:

2. Searching Substrings

Match partial strings by specifying only a fragment:
This matches “Marketing”, “Manager”, “Mark”, and the last name “Ma”.

3. Exact Word Matches with Word Boundaries

Use \< and \> to match whole words only:
Command breakdown:
  • sed : invoke stream editor
  • -n : suppress default output
  • /\<Ma\>/ : match exact word “Ma”
  • p : print matching line

4. Combining Multiple Search Patterns

Chain multiple scripts with -e to search for more than one pattern:
Lines matching either pattern appear once per script.

5. Deleting Matches

Swap p for d to remove matching lines: Remove Enrique’s record:
Delete all lines containing “Ma” as a whole word:

6. Editing Files In-Place with -i

Apply deletions or substitutions directly using -i:
Using -i overwrites your source file. Always keep backups or use version control.

7. Quick Reference: sed Flags


The image is a checklist titled "sed search," highlighting topics such as using the search function with print and delete commands, expressing the dash e flag for multiple scripts, and revisiting the use of flags -n and -i.

Watch Video