GARY IS COMING FOR YOU

You shouldn't have done that.

⚙ MALEVELON CREEK
CLICK TO SCAN
SPACE-BAR SERVICE DESK

Bartender

Here to help you explore

Questions and relevant page context are sent to TypeSafe. Questions Bartender cannot answer are saved to improve the site, without your account or IP. Other conversation stays in this tab.

sed Tool Reference


sed (stream editor) is a powerful text processing tool for filtering and transforming text. Edit files non-interactively, perform find-and-replace operations, and manipulate text streams efficiently.


Basic Usage

  • sed 's/old/new/' file - Substitute first occurrence per line
  • sed 's/old/new/g' file - Substitute all occurrences
  • sed 's/old/new/2' file - Substitute 2nd occurrence
  • sed -i 's/old/new/g' file - Edit file in-place
  • sed -i.bak 's/old/new/g' file - In-place with backup

Substitution

  • sed 's/pattern/replacement/' - Basic substitution
  • sed 's/pattern/replacement/g' - Global substitution
  • sed 's/pattern/replacement/i' - Case-insensitive
  • sed 's/pattern/replacement/gi' - Global, case-insensitive
  • sed 's|old|new|g' - Use different delimiter
  • sed 's/old/new/; s/old2/new2/' - Multiple substitutions

Line Selection

  • sed -n '5p' file - Print line 5
  • sed -n '5,10p' file - Print lines 5-10
  • sed -n '5,$p' file - Print from line 5 to end
  • sed -n '/pattern/p' file - Print matching lines
  • sed -n '/start/,/end/p' file - Print range between patterns
  • sed '5d' file - Delete line 5
  • sed '/pattern/d' file - Delete matching lines

Insertion & Appending

  • sed '5i\new line' file - Insert before line 5
  • sed '5a\new line' file - Append after line 5
  • sed '/pattern/i\new line' file - Insert before match
  • sed '/pattern/a\new line' file - Append after match
  • sed '$a\new line' file - Append to end

Advanced Patterns

  • sed 's/^old/new/' - Match start of line
  • sed 's/old$/new/' - Match end of line
  • sed 's/old.*/new/' - Match rest of line
  • sed 's/\(.*\)old\(.*\)/\1new\2/' - Capture groups
  • sed 's/old/new/; t; s/old2/new2/' - Conditional
  • sed -n 's/old/new/p' - Print only modified lines

Multiple Commands

  • sed -e 's/old/new/' -e 's/old2/new2/' file - Multiple expressions
  • sed 's/old/new/; s/old2/new2/' file - Semicolon separator
  • sed -f script.sed file - Read from script file

Common Examples

Replace Text

sed 's/old/new/g' file.txt

Replace all occurrences.

In-Place Edit

sed -i 's/old/new/g' file.txt

Edit file directly.

Delete Lines

sed '/pattern/d' file.txt

Remove matching lines.

Print Range

sed -n '10,20p' file.txt

Print lines 10-20.

Add Line

sed '5a\new content' file.txt

Insert after line 5.

Remove Empty Lines

sed '/^$/d' file.txt

Delete blank lines.

Remove Leading Spaces

sed 's/^[ \t]*//' file.txt

Strip whitespace from start.

Remove Trailing Spaces

sed 's/[ \t]*$//' file.txt

Strip whitespace from end.

Number Lines

sed = file.txt | sed 'N;s/\n/\t/'

Add line numbers.

Extract Field

sed 's/.*:\(.*\):.*/\1/' file.txt

Extract between colons.


Tips

  • Use -i for in-place editing (be careful!)
  • Use -i.bak to create backup
  • Use -n with p to print only selected lines
  • Use g flag for global substitution
  • Use different delimiters (|, #) for complex patterns
  • Combine with other tools in pipelines
  • Test without -i first to verify changes
  • Essential for batch text processing