info > sed(1)

๐Ÿ“– NAME

GNU sed โ€“ a stream editor, version 4.8. This document is the GNU sed manual.

๐Ÿš€ Quick Reference

Use CaseCommandDescription
Basic substitutionsed 's/old/new/' fileโœ… Replace first occurrence of 'old' with 'new' on each line
Global substitutionsed 's/old/new/g' file๐Ÿ”„ Replace all occurrences
In-place editsed -i 's/old/new/' fileโœ๏ธ Edit file directly (creates backup with -i.bak)
Print specific linesed -n '45p' file๐Ÿ“„ Print only line 45
Delete linessed '3d' file๐Ÿ—‘๏ธ Delete line 3
Delete rangesed '30,35d' file๐Ÿ—‘๏ธ Delete lines 30-35
Case-insensitive matchsed '/pattern/Id' file๐Ÿ” Delete lines matching 'pattern' case-insensitively
Extended regexsed -E 's/[0-9]+/N/g' file๐Ÿ”ง Use extended regular expressions (fewer backslashes)
Print line numberssed -n '=' file๐Ÿ”ข Print line numbers only
Quit after matchsed '/pattern/q' file๐Ÿšช Print lines until pattern, then exit
Read file into streamsed '2r /path/to/file' input๐Ÿ“‚ Insert file content after line 2
Write matched lines to filesed -n '/pattern/w output' input๐Ÿ’พ Write lines matching pattern to output file
Multiple commandssed -e 's/foo/bar/' -e 's/baz/qux/' file๐Ÿ”— Chain multiple operations
Use sed script filesed -f script.sed input๐Ÿ“œ Execute commands from a file

๐Ÿ“ 1 Introduction

sed is a stream editor. It performs basic text transformations on an input stream (file or pipeline). Unlike editors like ed, sed makes only one pass over the input, making it efficient. Its ability to filter text in a pipeline distinguishes it from other editors.

โš™๏ธ 2 Running sed

This chapter covers how to run sed.

2.1 Overview

Normally invoked as:

sed SCRIPT INPUTFILE...

Example: replace all 'hello' with 'world' in input.txt:

sed 's/hello/world/' input.txt > output.txt

If no input file specified or - is given, reads from stdin. Use -i for in-place editing. Use -n to suppress automatic printing; use p to print specific lines. Multiple input files are treated as one long stream (use -s to treat separately).

2.2 Command-Line Options

Full format: sed OPTIONS... [SCRIPT] [INPUTFILE...]

2.3 Exit status

Exit codes: 0 = success, 1 = invalid command/syntax/regex, 2 = input file open failure, 4 = I/O or runtime error. The q and Q commands can return custom exit codes.

๐Ÿ“œ 3 sed scripts

3.1 sed script overview

A sed program consists of one or more commands, passed via -e, -f, or first non-option argument. Command syntax: [addr]X[options]. Commands can be separated by ; or newlines. Some commands (a, c, i) cannot be followed by semicolons.

3.2 sed commands summary

3.3 The 's' Command

Syntax: s/REGEXP/REPLACEMENT/FLAGS. Replaces matched portion with REPLACEMENT. Supports backreferences \1โ€“\9, & for whole match. Case conversion: \L, \l, \U, \u, \E. Flags: g (global), NUMBER (replace Nth match), p (print), w FILENAME (write), e (execute result as command), I (case-insensitive), M (multi-line mode).

3.4 Often-Used Commands

3.5 Less Frequently-Used Commands

3.6 Commands for sed gurus

3.7 Commands Specific to GNU sed

3.8 Multiple commands syntax

Commands can be separated by newlines, -e options, or semicolons (for most commands). Commands a, c, i, #, r, R, w, W, e, and s///[we] require newlines to terminate text/filenames.

๐ŸŽฏ 4 Addresses: selecting lines

4.1 Addresses overview

Addresses determine which lines commands apply to. They can be a line number, a regular expression, or a range. Negation with !.

4.2 Selecting lines by numbers

4.3 Selecting lines by text matching

4.4 Range Addresses

Two addresses separated by comma. Special forms: 0,/REGEXP/ (includes first line), ADDR1,+N (N lines after), ADDR1,~N (up to next multiple of N).

๐Ÿ” 5 Regular Expressions: selecting text

5.1 Overview

Regular expressions are patterns matched against subject strings. Most characters are ordinary; special characters include ^, $, ., *, etc.

5.2 BRE vs ERE

Basic (default) vs Extended (-E). In BRE, +, ?, {}, (), | need backslash to be special. In ERE, they are special without backslash.

5.3 BRE syntax

5.4 ERE syntax

Same as BRE but with opposite escaping: +, ?, {}, (), | are special without backslash.

5.5 Character Classes and Bracket Expressions

Bracket expressions [...] match one character. Named classes: [:alnum:], [:alpha:], [:blank:], [:cntrl:], [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], [:xdigit:].

5.6 Regular expression extensions

5.7 Back-references and Subexpressions

Back-references \1โ€“\9 refer to parenthesized subexpressions. Used in both patterns and replacement.

5.8 Escape Sequences

Non-printable characters: \a (BEL), \f (form feed), \n (newline), \r (CR), \t (tab), \v (vertical tab), \cX (control), \dXXX (decimal), \oXXX (octal), \xXX (hex).

5.9 Multibyte and Locale Considerations

GNU sed handles UTF-8 multibyte characters. Invalid sequences are not matched by .. Use z command to clear pattern space. Upper/lower conversion works with multibyte. Use LC_ALL=C for bytewise processing.

๐Ÿง  6 Advanced sed: cycles and buffers

6.1 How sed Works

Two buffers: pattern space (active) and hold space (auxiliary). For each line: read, execute commands, print (unless -n), then next cycle.

6.2 Hold and Pattern Buffers

Use h, H, g, G, x to move data between buffers.

6.3 Multiline techniques

Use N, D, P, G, H to process multiple lines. Example: process paragraphs with sed '/./{H;$!d} ; x ; s/REGEXP/REPLACEMENT/'.

6.4 Branching and Flow Control

Commands b (unconditional), t (conditional on success), T (conditional on failure) change flow. Labels defined with :LABEL. Branching can create loops, join lines, etc.

๐Ÿ“š 7 Some Sample Scripts

7.1 Joining lines

Join specific lines: sed '2{N;s/\n//;}'. Join backslash-continued lines: sed -e ':x /\\$/ { N; s/\\\n//g ; bx }'. Join lines starting with whitespace: sed -E ':a ; $!N ; s/\n\s+/ / ; ta ; P ; D'.

7.2 Centering lines

Center text to 80 columns using hold space and pattern manipulations.

7.3 Increment a number

Replace trailing 9s with underscores, increment last digit, then replace underscores with zeros.

7.4 Rename files to lower case

Generate shell commands to rename files using sed and y command.

7.5 Print bash environment

Strip function definitions from set output.

7.6 Reverse characters of lines

Using markers and loops to reverse character order.

7.7 Text search across multiple lines

Find doubled words using N and D with backreferences.

7.8 Line length adjustment

Wrap lines at 40 characters using nested loops.

7.9 Reverse lines of files (tac)

Accumulate lines in hold space and print at end.

7.10 Numbering lines (cat -n)

Complex script using hold space and y for incrementing numbers.

7.11 Numbering non-blank lines (cat -b)

Similar but skip blank lines.

7.12 Counting characters (wc -c)

Map characters to letters, propagate carries, convert back to decimal.

7.13 Counting words (wc -w)

Convert words to 'a's, then count like characters.

7.14 Counting lines (wc -l)

Just $=.

7.15 Printing the first lines (head)

10q.

7.16 Printing the last lines (tail)

Sliding window technique using N and D.

7.17 Make duplicate lines unique (uniq)

Using N, P, D to compare adjacent lines.

7.18 Print duplicated lines (uniq -d)

Print only lines that appear more than once.

7.19 Remove all duplicated lines (uniq -u)

Print only unique lines.

7.20 Squeezing blank lines (cat -s)

Various scripts to reduce multiple blank lines to one.

โš ๏ธ 8 Limitations

GNU sed has no built-in line length limit (only memory). Recursion may limit stack for complex patterns.

๐Ÿ“Ž 9 Other Resources

๐Ÿ› 10 Reporting Bugs

Email to bug-sed@gnu.org with sed --version output. Provide a minimal test case. Known non-bugs include: N on last line, regex backslash semantics, -i on read-only files, 0a not working, [a-z] locale sensitivity, s/.*// not clearing multibyte patterns.

sed(1)
๐Ÿ“– NAME ๐Ÿš€ Quick Reference ๐Ÿ“ 1 Introduction โš™๏ธ 2 Running sed
2.1 Overview 2.2 Command-Line Options 2.3 Exit status
๐Ÿ“œ 3 sed scripts
3.1 sed script overview 3.2 sed commands summary 3.3 The 's' Command 3.4 Often-Used Commands 3.5 Less Frequently-Used Commands 3.6 Commands for sed gurus 3.7 Commands Specific to GNU sed 3.8 Multiple commands syntax
๐ŸŽฏ 4 Addresses: selecting lines
4.1 Addresses overview 4.2 Selecting lines by numbers 4.3 Selecting lines by text matching 4.4 Range Addresses
๐Ÿ” 5 Regular Expressions: selecting text
5.1 Overview 5.2 BRE vs ERE 5.3 BRE syntax 5.4 ERE syntax 5.5 Character Classes and Bracket Expressions 5.6 Regular expression extensions 5.7 Back-references and Subexpressions 5.8 Escape Sequences 5.9 Multibyte and Locale Considerations
๐Ÿง  6 Advanced sed: cycles and buffers
6.1 How sed Works 6.2 Hold and Pattern Buffers 6.3 Multiline techniques 6.4 Branching and Flow Control
๐Ÿ“š 7 Some Sample Scripts
7.1 Joining lines 7.2 Centering lines 7.3 Increment a number 7.4 Rename files to lower case 7.5 Print bash environment 7.6 Reverse characters of lines 7.7 Text search across multiple lines 7.8 Line length adjustment 7.9 Reverse lines of files (tac) 7.10 Numbering lines (cat -n) 7.11 Numbering non-blank lines (cat -b) 7.12 Counting characters (wc -c) 7.13 Counting words (wc -w) 7.14 Counting lines (wc -l) 7.15 Printing the first lines (head) 7.16 Printing the last lines (tail) 7.17 Make duplicate lines unique (uniq) 7.18 Print duplicated lines (uniq -d) 7.19 Remove all duplicated lines (uniq -u) 7.20 Squeezing blank lines (cat -s)
โš ๏ธ 8 Limitations ๐Ÿ“Ž 9 Other Resources ๐Ÿ› 10 Reporting Bugs

Generated by phpman v4.9.26-1-g511901d · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-02 06:25 @216.73.217.9
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^