| name | sed |
| description | Expert GNU sed one-liner generation and transformation. Use this skill whenever the user asks to transform text, edit files in-place, refactor code via CLI, process logs, do bulk find/replace, extract lines, delete patterns, or perform any stream editing task. Trigger on keywords like "sed", "replace in files", "find and replace", "edit in place", "strip lines", "extract pattern", "log filtering", "batch rename content", "remove lines matching", "transform output", or any request that maps to a sed one-liner or pipeline. When in doubt, use this skill — sed is often the right tool.
|
GNU sed Skill
All output assumes GNU sed (sed --version shows GNU). POSIX compatibility is
not a goal; use -E (extended regex) by default unless basic regex is clearly
sufficient.
Core Principles
- Prefer one-liners. Multi-line sed scripts are a last resort.
- Use
-E for extended regex (avoids backslash noise).
- Use
-i for in-place edits; always show the command with a backup suffix
suggestion (-i.bak) if the operation is destructive.
- Dry-run first. When editing files, show the
sed without -i before the
in-place version so the user can verify.
- Chain with pipes when sed alone isn't the cleanest tool.
- Never use sed to parse structured formats (JSON, YAML, XML). Redirect to
jq, yq, or xmllint and say why.
Flags to Always Know
| Flag | Meaning |
|---|
-E | Extended regex (ERE): +, ?, ` |
-i[SUFFIX] | Edit in place (GNU: -i or -i'' for no backup, -i.bak for backup) |
-n | Suppress automatic print; use with p to print selectively |
-e | Multiple expressions in one invocation |
-f FILE | Read commands from a script file |
--sandbox | Disallow e, r, w commands (safe mode) |
Command Quick Reference
Substitution (s)
sed 's/foo/bar/'
sed 's/foo/bar/g'
sed 's/foo/bar/gI'
sed '/error/s/foo/bar/g'
sed '10,20s/foo/bar/g'
sed 's/[0-9]\+/(&)/g'
sed -E 's/(foo)(bar)/\2\1/'
sed 's|/usr/local|/opt|g'
Delete lines (d)
sed '/^#/d'
sed '/^[[:space:]]*$/d'
sed '5,10d'
sed '/START/,$d'
sed '/keep/!d'
Print / Extract (p, -n)
sed -n '/error/p'
sed -n '/error/='
sed -n '10,20p'
sed -n '/START/,/END/p'
sed -En 's/.*key=([^ ]+).*/\1/p'
Insert / Append / Change (i, a, c)
sed '/pattern/i\NEW LINE'
sed '/pattern/a\NEW LINE'
sed '/pattern/c\REPLACEMENT LINE'
Transform characters (y)
sed 'y/abc/ABC/'
Multi-expression
sed -E -e 's/foo/bar/g' -e 's/baz/qux/g'
Patterns & Addresses
/regex/ — lines matching regex
/re1/,/re2/ — line range from re1 to re2 (inclusive)
N — line number N
N,M — line numbers N through M
N~STEP — every STEP lines starting at N (e.g. 1~2 = odd lines)
$ — last line
/re/! — lines NOT matching
Common Use Cases
Shell scripting: rewrite config values
sed -E 's/^(TIMEOUT=).*/\1120/' config.env
sed -E -i.bak 's/^(TIMEOUT=).*/\1120/' config.env
Code refactoring: rename symbol across files
grep -rl 'OldClass' src/ | xargs sed -E 's/\bOldClass\b/NewClass/g'
grep -rl 'OldClass' src/ | xargs sed -E -i.bak 's/\bOldClass\b/NewClass/g'
Use \b for word boundaries (GNU sed supports \b in ERE).
Log processing: extract fields
sed -En 's/.*\[([^\]]+)\].*/\1/p' access.log
sed -n '/2024-01-01 10:00/,/2024-01-01 11:00/p' app.log
sed -E 's/\x1B\[[0-9;]*[mK]//g'
File batch editing: add/remove lines
find . -name '*.sh' | xargs sed -i '1{/^#!/!i\#!/usr/bin/env bash
}'
sed -E -i 's/[[:space:]]+$//' file.rb
sed -i 's/^FEATURE_X=/#FEATURE_X=/' .env
Delete lines between markers (exclusive)
sed '/START/,/END/{/START/!{/END/!d}}' file.txt
Number lines (print with line numbers)
sed = file.txt | sed 'N;s/\n/\t/'
Combining with Other Tools
grep -rl 'pattern' . | xargs sed -i 's/pattern/replacement/g'
sed -n '/error/p' app.log | awk '{print $1, $NF}'
cat access.log | sed -n '/POST/p' | cut -d' ' -f7 | sort | uniq -c
Output Format
When responding to a sed request:
- Show the one-liner in a code block, ready to copy-paste.
- If editing files in-place, show dry-run first, then the
-i version.
- Add a one-line comment explaining what each part does if non-obvious.
- If the task is better served by
awk, perl, jq, or ripgrep, say so briefly
and optionally provide that alternative instead.
Gotchas
- macOS ships BSD sed, not GNU sed.
-i requires a mandatory suffix (-i ''
is valid on BSD, -i.bak on both). If the user is on macOS, note this.
- Regex in sed is greedy by default; there is no non-greedy quantifier in GNU sed
ERE. Workaround: use negated character classes (
[^x]* instead of .*?).
\w is a GNU sed extension, not portable sed. Use [[:alnum:]_] for portable
word characters, and [0-9] or [[:digit:]] for digits. Use Perl (perl -pe)
when you need PCRE shorthands such as \d.
- Multiline matching across line breaks requires the
N command or a hold-space
trick. If the user needs real multiline regex, recommend perl -0777 -pe.