Skip to main content

Syntax

sed [OPTIONS] SCRIPT [FILE...]

Description

The sed (stream editor) command performs text transformations on input streams. It’s particularly powerful for search-and-replace operations, line deletion, and pattern-based text manipulation. Nash implements a subset of sed’s functionality, focusing on the most common operations while maintaining full sandbox isolation.

Options

SCRIPT
string
required
The sed script expression to execute. See supported operations below.
-n
flag
Silent mode. Suppress automatic printing of pattern space. Only print lines explicitly selected with the p command.
-e
flag
Specify the script expression explicitly. Useful when the script starts with a dash.

Supported Operations

Substitution: s/old/new/[flags]

Replace text matching a pattern. Flags:
  • g - Global: replace all occurrences in each line (not just the first)
  • p - Print: output lines where substitution occurred (use with -n)

Line deletion: Nd

Delete line number N. Print only line number N.

Delete all: d

Delete all lines (results in empty output).

Examples

Basic substitution

echo "hello world" | sed 's/world/universe/'
hello universe

Global substitution

echo "foo bar foo baz" | sed 's/foo/XXX/g'
XXX bar XXX baz
Without the g flag, only the first occurrence is replaced:
echo "foo bar foo baz" | sed 's/foo/XXX/'
XXX bar foo baz

Delete specific line

echo -e "line 1\nline 2\nline 3" | sed '2d'
line 1
line 3
echo -e "line 1\nline 2\nline 3" | sed -n '2p'
line 2

Silent mode with substitution

echo -e "apple\nbanana\napricot" | sed -n 's/a/A/p'
Apple
bAnana
Apricot
Only lines where substitution occurred are printed.

Using different delimiters

echo "/usr/local/bin" | sed 's|/usr/local|/opt|'
/opt/bin
Useful when the pattern contains slashes.

Pipeline Examples

Transform log levels

cat app.log | sed 's/ERROR/[ERROR]/g' | sed 's/WARN/[WARN]/g'
Wraps log levels in brackets.

Clean up whitespace

echo "hello    world" | sed 's/  */ /g'
hello world

Replace file extensions

ls *.txt | sed 's/\.txt$/.md/'
Shows how filenames would look with .md extension.

Chain transformations

echo "Hello World" | sed 's/Hello/Hi/' | sed 's/World/Universe/'
Hi Universe

Practical Use Cases

Configuration file updates

cat config.ini | sed 's/debug=false/debug=true/' > config-dev.ini

Remove sensitive data

cat users.csv | sed 's/@.*\.com/@REDACTED.com/g'
Redacts email domains.

Format conversion

cat data.csv | sed 's/,/\t/g'
Converts CSV to TSV (comma to tab).

Version string updates

cat package.json | sed 's/"version": "1.0.0"/"version": "1.1.0"/'

Extract and transform

grep "ERROR" app.log | sed 's/.*ERROR: //' | sort | uniq
Extracts error messages, cleans them up, and shows unique errors.

Line numbering cleanup

cat -n file.txt | sed 's/^[ ]*[0-9]*[ ]//'
Removes line numbers added by cat -n.

Advanced Examples

Multiple substitutions in order

echo "abc" | sed 's/a/1/g' | sed 's/b/2/g' | sed 's/c/3/g'
123

Conditional replacement with print

echo -e "keep\nchange\nkeep" | sed -n 's/change/modified/p'
modified

Remove empty lines

cat file.txt | sed '/^$/d'

Extract specific range

echo -e "a\nb\nc\nd\ne" | sed -n '2p' && echo -e "a\nb\nc\nd\ne" | sed -n '4p'

Tips

Substitution is literal: Nash’s sed performs literal string replacement, not regular expressions. s/a.b/xyz/ replaces the literal text “a.b”, not “a” followed by any character and “b”.

Delimiter flexibility

You can use any character as the delimiter:
sed 's/old/new/'    # Forward slash
sed 's|old|new|'    # Pipe
sed 's:old:new:'    # Colon
sed 's#old#new#'    # Hash

Global flag matters

echo "aaa" | sed 's/a/X/'    # Xaa (first only)
echo "aaa" | sed 's/a/X/g'   # XXX (all)

Silent mode usage

Use -n with p flag to see only modified lines:
cat large.txt | sed -n 's/error/ERROR/p'
Only shows lines containing “error”.
  • grep - Search for patterns
  • cut - Extract fields from lines
  • sort - Sort lines of text

Build docs developers (and LLMs) love