Skip to main content

How to Use the sed Command on Linux for Text Editing

HardReset.InfoJanuary 1, 20269 min19 views
2 connections·4 entities in this video→

Understanding the sed Command

  • πŸ’‘ sed is a powerful stream editor on Linux used for searching, replacing, and modifying text directly from the terminal without opening a full editor.
  • βš™οΈ The basic syntax involves sed [options] 'command' filename, where command specifies the action to perform.

Basic Substitution and Global Replacement

  • 🎯 The s/old/new/ command is used for substituting text, replacing the first occurrence of 'old' with 'new' on each line.
  • 🌍 To replace all occurrences of a pattern on a line, use the global flag: s/old/new/g.
  • ⚠️ By default, sed only shows changes in the output; the original file remains unchanged.

In-Place Editing and Backups

  • πŸ’Ύ The -i option allows for in-place editing, modifying the file directly.
  • πŸ”™ To create a backup of the original file before editing, use -i.bak, which appends .bak to the original filename.

Targeting Specific Lines and Deleting Lines

  • πŸ“ You can apply commands to specific lines by prefixing the command with the line number (e.g., 6s/old/new/).
  • ↔️ A line range can be specified using a comma between line numbers (e.g., 2,3s/old/new/) to apply changes to multiple lines.
  • πŸ—‘οΈ The d command is used to delete specific lines from the file, and -i is needed to make this deletion permanent.
Knowledge graph4 entities Β· 2 connections

How they connect

An interactive map of every person, idea, and reference from this conversation. Hover to trace connections, click to explore.

Hover Β· drag to explore
4 entities
Chapters3 moments

Key Moments

Transcript32 segments

Full Transcript

Topics11 themes

What’s Discussed

sed commandLinuxStream EditorText EditingCommand LineSubstitutionGlobal ReplacementIn-place EditingFile BackupsLine TargetingDelete Lines
Smart Objects4 Β· 2 links
ConceptsΒ· 3
ProductΒ· 1