api: YAML JSON TOON Database
version: 1.0.0
format: yaml
dataset:
  id: 2478
  slug: sed-reference
  title: Sed Reference
  description: Common sed commands, substitutions, and flags.
  summary: "sed stream-editor patterns: substitution, in-place edits, deleting and printing lines, inserting and appending, regex groups, multiple commands and address ranges, extended with line deletion by pattern, nth-occurrence replacement, case-insensitive matching, trailing-whitespace removal and alternate delimiters."
  use_when:
    - You need a one-line text substitution or line filter.
    - You need the idiom for a common sed task.
  caveats:
    - sed -i behaves differently on GNU and BSD/macOS (macOS needs -i '' ); use a suffix such as -i.bak for portability and safety.
    - The I flag for case-insensitive matching is a GNU extension.
  related:
    - awk-reference
    - unix-text-processing-commands
    - file-operations
    - shell-scripting-guide
  entry_count: 16
  fields:
    - section: data
      field: pattern
      type: string
      example: Substitute
    - section: data
      field: command
      type: string
      example: sed 's/foo/bar/g' file
    - section: data
      field: description
      type: string
      example: Replace all occurrences of foo with bar globall…
  category: Unix Commands
  category_slug: unix-commands
  tags: unix,sed,text-processing,stream-editor
  view_count: 19
  created_at: 1792832553
  updated_at: 1792832553
data:
  - pattern: Substitute
    command: sed 's/foo/bar/g' file
    description: Replace all occurrences of foo with bar globally.
  - pattern: In-place edit
    command: sed -i 's/foo/bar/g' file
    description: Edit file in place; use -i.bak for backup.
  - pattern: Delete lines
    command: sed '3d' file
    description: Delete line 3. Use /pattern/d for pattern match.
  - pattern: Print lines
    command: sed -n '5,10p' file
    description: Print lines 5 through 10 only.
  - pattern: Insert/append
    command: "sed '3i\\nNew line' file"
    description: Insert text before line 3.
  - pattern: Regex group
    command: "sed 's/(\\d+)/[\\1]/g'"
    description: Capture digits in parentheses and wrap in brackets.
  - pattern: Multiple commands
    command: sed -e 's/a/A/g' -e 's/e/E/g'
    description: Apply multiple substitution commands.
  - pattern: Address range
    command: sed '1,5s/old/new/' file
    description: Substitute only in lines 1 through 5.
  - pattern: Delete blank lines
    command: sed '/^$/d' file
    description: Remove all empty lines.
  - pattern: Print a specific line
    command: sed -n '5p' file
    description: Print only line 5; -n suppresses default output.
  - pattern: Print the last line
    command: sed -n '$p' file
    description: The $ address means the last line.
  - pattern: Replace the Nth occurrence
    command: sed 's/foo/bar/2' file
    description: Replace only the second occurrence on each line.
  - pattern: Case-insensitive substitute
    command: sed 's/foo/bar/Ig' file
    description: "GNU sed: the I flag ignores case."
  - pattern: Substitute within matching lines
    command: sed '/error/s/foo/bar/g' file
    description: Apply the substitution only on lines containing 'error'.
  - pattern: Strip trailing whitespace
    command: "sed 's/[ \\t]*$//' file"
    description: Remove spaces and tabs at the end of each line.
  - pattern: Use another delimiter
    command: sed 's|/usr/local|/opt|g' file
    description: Choose a delimiter other than / when the pattern contains slashes.
