Sed Reference

Common sed commands, substitutions, and flags.

  • JSON4,978 B
  • YAML3,475 B · −30%
  • TOON2,698 B · −46%

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 it when

  • You need a one-line text substitution or line filter.
  • You need the idiom for a common sed task.

Watch out for

  • 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.

The data

PatternCommandDescription
Substitutesed 's/foo/bar/g' fileReplace all occurrences of foo with bar globally.
In-place editsed -i 's/foo/bar/g' fileEdit file in place; use -i.bak for backup.
Delete linessed '3d' fileDelete line 3. Use /pattern/d for pattern match.
Print linessed -n '5,10p' filePrint lines 5 through 10 only.
Insert/appendsed '3i\nNew line' fileInsert text before line 3.
Regex groupsed 's/(\d+)/[\1]/g'Capture digits in parentheses and wrap in brackets.
Multiple commandssed -e 's/a/A/g' -e 's/e/E/g'Apply multiple substitution commands.
Address rangesed '1,5s/old/new/' fileSubstitute only in lines 1 through 5.
Delete blank linessed '/^$/d' fileRemove all empty lines.
Print a specific linesed -n '5p' filePrint only line 5; -n suppresses default output.
Print the last linesed -n '$p' fileThe $ address means the last line.
Replace the Nth occurrencesed 's/foo/bar/2' fileReplace only the second occurrence on each line.
Case-insensitive substitutesed 's/foo/bar/Ig' fileGNU sed: the I flag ignores case.
Substitute within matching linessed '/error/s/foo/bar/g' fileApply the substitution only on lines containing 'error'.
Strip trailing whitespacesed 's/[ \t]*$//' fileRemove spaces and tabs at the end of each line.
Use another delimitersed 's|/usr/local|/opt|g' fileChoose a delimiter other than / when the pattern contains slashes.

Fields

The shape of this dataset, read from the data itself: every column, its type and a real value. Agents can use this to plan a query without fetching the whole document.

SectionFieldTypeExample
datapatternstringSubstitute
datacommandstringsed 's/foo/bar/g' file
datadescriptionstringReplace all occurrences of foo with bar globall…

Fetch the same bytes

The static files contain the same reference rows as the API, with no API rate limit. Live metadata, such as view counts, may differ. Use the API for content negotiation; use the static files for a cacheable snapshot.

curl "https://yjtoon.com/api/dataset/sed-reference?format=toon"
const res = await fetch(
  "https://yjtoon.com/static-data/dataset/sed-reference.toon"
);
const toon = await res.text();

Rate limit: 120 requests per minute per IP, no key and no signup. API reference →

Topics

  • unix
  • sed
  • text-processing
  • stream-editor

Search the library

Search by a topic, title, or keyword.

Start with a collection

Browse all 205 datasets