官方文档:sed 命令
常用命令:https://www.gnu.org/software/software.html
官方文档:https://www.gnu.org/software/sed/manual/
sed命令:https://www.gnu.org/software/sed/manual/sed.html
sed, a stream editor
1 Introduction
- 2.1 Overview
- 2.2 Command-Line Options
- 2.3 Exit status
sed scripts
- 3.1
sedscript overview - 3.2
sedcommands summary - 3.3 The
sCommand - 3.4 Often-Used Commands
- 3.5 Less Frequently-Used Commands
- 3.6 Commands for
sedgurus - 3.7 Commands Specific to GNU
sed - 3.8 Multiple commands syntax
- 3.8.1 Commands Requiring a newline
- 4.1 Addresses overview
- 4.2 Selecting lines by numbers
- 4.3 selecting lines by text matching
- 4.4 Range Addresses
- 5.1 Overview of regular expression in
sed - 5.2 Basic (BRE) and extended (ERE) regular expression
- 5.3 Overview of basic regular expression syntax
- 5.4 Overview of extended regular expression syntax
- 5.5 Character Classes and Bracket Expressions
- 5.6 regular expression extensions
- 5.7 Back-references and Subexpressions
- 5.8 Escape Sequences - specifying special characters
- 5.8.1 Escaping Precedence
- 5.9 Multibyte characters and Locale Considerations
- 5.9.1 Invalid multibyte characters
- 5.9.2 Upper/Lower case conversion
- 5.9.3 Multibyte regexp character classes
sed: cycles and buffers
- 6.1 How
sedWorks - 6.2 Hold and Pattern Buffers
- 6.3 Multiline techniques - using D,G,H,N,P to process multiple lines
- 6.4 Branching and Flow Control
- 6.4.1 Branching and Cycles
- 6.4.2 Branching example: joining lines
- 7.1 Joining lines
- 7.2 Centering Lines
- 7.3 Increment a Number
- 7.4 Rename Files to Lower Case
- 7.5 Print
bashEnvironment - 7.6 Reverse Characters of Lines
- 7.7 Text search across multiple lines
- 7.8 Line length adjustment
- 7.9 Reverse Lines of Files
- 7.10 Numbering Lines
- 7.11 Numbering Non-blank Lines
- 7.12 Counting Characters
- 7.13 Counting Words
- 7.14 Counting Lines
- 7.15 Printing the First Lines
- 7.16 Printing the Last Lines
- 7.17 Make Duplicate Lines Unique
- 7.18 Print Duplicated Lines of Input
- 7.19 Remove All Duplicated Lines
- 7.20 Squeezing Blank Lines
sed’s Limitations and Non-limitationssedIntroduction, Up: (dir) [Contents][Index]
Introduction:
sed scriptssed: cycles and bufferssedsedsed commands and command-line options.Invoking sed, Previous: Top, Up: Top [Contents][Index]
sed scripts, Previous: Introduction, Up: Top [Contents][Index]
Overview:
Command-Line Options, Up: Invoking sed [Contents][Index]
Exit status, Previous: Overview, Up: Invoking sed [Contents][Index]
How sed works). These options disable this automatic printing, and sed only produces output when explicitly told to via the p command.
--debugThis option implies -s.
When the end of the file is reached, the temporary file is renamed to the output file’s original name. The extension, if supplied, is used to modify the name of the old file before renaming the temporary file, thereby making a backup copy2).
Reporting Bugs) actually violate the standard. If you want to disable only the latter kind of extension, you can set thePOSIXLY_CORRECT variable to a non-empty value.
-b--binary-s--separatesed addresses, Previous: Invoking sed, Up: Top [Contents][Index]
sed script overview:
sed script overviewsed commands summarysed’s Swiss Army Knifesed gurussedsed commands list, Up: sed scripts [Contents][Index]
Overview.
sed addresses). Additional [options] are used for some sed commands.
Multiple commands syntax.
The "s" Command, Previous: sed script overview, Up: sed scripts [Contents][Index]
Common Commands, Previous: sed commands list, Up: sed scripts [Contents][Index]
Regular Expression Addresses.
3
eProgramming Commands, Previous: Common Commands, Up: sed scripts [Contents][Index]
4
The file will be created (or truncated) before the first input line is read; all w commands (including instances of the w flag on successful s commands) which refer to the same filename are output without closing and reopening the file.
DPMultiple commands syntax, Previous: Programming Commands, Up: sed scripts [Contents][Index]
Extended Commands, Up: sed scripts [Contents][Index]
Branching and flow control.
$ seq 3 | sed '/1/b x ; s/^/=/ ; :x ; 3d' 1 =2 $ seq 3 | sed -e '/1/bx' -e 's/^/=/' -e ':x' -e '3d' 1 =2
sed regular expressions, Previous: sed scripts, Up: Top [Contents][Index]
Addresses overview:
Numeric Addresses, Up: sed addresses [Contents][Index]
Regexp Addresses, Previous: Addresses overview, Up: sed addresses [Contents][Index]
Range Addresses, Previous: Numeric Addresses, Up: sed addresses [Contents][Index]
Basic Regular Expression (BRE). If -E or -r options are used, The regular expression should be in Extended Regular Expression (ERE) syntax. See BRE vs ERE.
/regexp/- 5:
sed -n '/bash$/p' /etc/passwd
insert command).Observe the difference between the following examples.
In this example,
/b/Iis the address: regular expression withImodifier.dis the delete command:$ printf "%s\n" a b c | sed '/b/Id' a c
Here,
/b/is the address: a regular expression.iis the insert command.dis the value to insert. A line with ‘d’ is then inserted above the matched line:$ printf "%s\n" a b c | sed '/b/id' a d b c
/regexp/M\%regexp%M- Regexp Addresses, Up: sed addresses [Contents][Index]
advanced sed, Previous: sed addresses, Up: Top [Contents][Index]
Regular Expressions Overview:
Overview of Regular expression in sed? BRE vs ERE: Basic (BRE) and extended (ERE) regular expression syntax ? BRE syntax: Overview of basic regular expression syntax ? ERE syntax: Overview of extended regular expression syntax ? Character Classes and Bracket Expressions: ? regexp extensions: Additional regular expression commands ? Back-references and Subexpressions: Back-references and Subexpressions ? Escapes: Specifying special characters ? Locale Considerations: Multibyte characters and locale considrations
BRE vs ERE, Up: sed regular expressions [Contents][Index]
BRE syntax, Previous: Regular Expressions Overview, Up: sed regular expressions [Contents][Index]
ERE syntax, Previous: BRE vs ERE, Up: sed regular expressions [Contents][Index]
How
sedworks). So, it is a useful simplification to think of^#includeas matching only lines where ‘#include’ is the first thing on line—if there are spaces before, for example, the match fails. This simplification is valid as long as the original content of pattern space is not modified, for example with anscommand.^acts as a special character only at the beginning of the regular expression or subexpression (that is, after\(or\|). Portable scripts should avoid^at the beginning of a subexpression, though, as POSIX allows implementations that treat^as an ordinary character in that context. $-
It is the same as
^, but refers to end of pattern space.$also acts as a special character only at the end of the regular expression or subexpression (that is, before\)or\|), and its use at the end of a subexpression is not portable. [list][^list]-
Matches any single character in list: for example,
[aeiou]matches all vowels. A list may include sequences likechar1-char2, which matches any character between (inclusive) char1 and char2. See Character Classes and Bracket Expressions. \+- Character Classes and Bracket Expressions, Previous: BRE syntax, Up: sed regular expressions [Contents][Index]
regexp extensions, Previous: ERE syntax, Up: sed regular expressions [Contents][Index]
basic and extended regular expressions (that is, with or without the -E/-r options).
Escapes.
- ‘[.’
-
represents the open collating symbol.
- ‘.]’
-
represents the close collating symbol.
- ‘[=’
-
represents the open equivalence class.
- ‘=]’
-
represents the close equivalence class.
- ‘[:’
-
represents the open character class symbol, and should be followed by a valid character class name.
- ‘:]’
-
represents the close character class symbol.
Back-references and Subexpressions, Previous: Character Classes and Bracket Expressions, Up: sed regular expressions [Contents][Index]
addresses and the
scommand).These can be used in both basic and extended regular expressions (that is, with or without the -E/-r options).
\w-
Matches any “word” character. A “word” character is any letter or digit or the underscore character.
$ echo "abc %-= def." | sed 's/\w/X/g' XXX %-= XXX.
\W-
Matches any “non-word” character.
$ echo "abc %-= def." | sed 's/\W/X/g' abcXXXXXdefX
\b-
Matches a word boundary; that is it matches if the character to the left is a “word” character and the character to the right is a “non-word” character, or vice-versa.
$ echo "abc %-= def." | sed 's/\b/X/g' XabcX %-= XdefX.
\B-
Matches everywhere but on a word boundary; that is it matches if the character to the left and the character to the right are either both “word” characters or both “non-word” characters.
$ echo "abc %-= def." | sed 's/\B/X/g' aXbXc X%X-X=X dXeXf.X
\s-
Matches whitespace characters (spaces and tabs). Newlines embedded in the pattern/hold spaces will also match:
$ echo "abc %-= def." | sed 's/\s/X/g' abcX%-=Xdef.
\S-
Matches non-whitespace characters.
$ echo "abc %-= def." | sed 's/\S/X/g' XXX XXX XXXX
\<-
Matches the beginning of a word.
$ echo "abc %-= def." | sed 's/\
\>-
Matches the end of a word.
$ echo "abc %-= def." | sed 's/\>/X/g' abcX %-= defX.
\`-
Matches only at the start of pattern space. This is different from
^in multi-line mode.Compare the following two examples:
$ printf "a\nb\nc\n" | sed 'N;N;s/^/X/gm' Xa Xb Xc $ printf "a\nb\nc\n" | sed 'N;N;s/\`/X/gm' Xa b c
\'-
Matches only at the end of pattern space. This is different from
$in multi-line mode.
Escapes, Previous: regexp extensions, Up: sed regular expressions [Contents][Index]
Regular Expression Addresses and The "s" Command).
In a regular expression pattern, back-references are used to match the same content as a previously matched subexpression. In the following example, the subexpression is ‘.’ - any single character (being surrounded by parentheses makes it a subexpression). The back-reference ‘\1’ asks to match the same content (same character) as the sub-expression.
The command below matches words starting with any character, followed by the letter ‘o’, followed by the same character as the first.
$ sed -E -n '/^(.)o\1$/p' /usr/share/dict/words bob mom non pop sos tot wow
Multiple subexpressions are automatically numbered from left-to-right. This command searches for 6-letter palindromes (the first three letters are 3 subexpressions, followed by 3 back-references in reverse order):
$ sed -E -n '/^(.)(.)(.)\3\2\1$/p' /usr/share/dict/words redder
In the
scommand, back-references can be used in the replacement part to refer back to subexpressions in the regexp part.The following example uses two subexpressions in the regular expression to match two space-separated words. The back-references in the replacement part prints the words in a different order:
$ echo "James Bond" | sed -E 's/(.*) (.*)/The name is \2, \1 \2./' The name is Bond, James Bond.
When used with alternation, if the group does not participate in the match then the back-reference makes the whole match fail. For example, ‘a(.)|b\1’ will not match ‘ba’. When multiple regular expressions are given with -e or from a file (‘-f file’), back-references are local to each expression.
Locale Considerations, Previous: Back-references and Subexpressions, Up: sed regular expressions [Contents][Index]
6—that is, escapes that are applied to a character or sequence of characters that ordinarily are taken literally, and that
sedreplaces with a special character. This provides a way of encoding non-printable characters in patterns in a visible manner. There is no restriction on the appearance of non-printing characters in asedscript but when a script is being prepared in the shell or by text editing, it is usually easier to use one of the following escape sequences than the binary character it represents:The list of these escapes is:
\a-
Produces or matches a BEL character, that is an “alert” (ASCII 7).
\f-
Produces or matches a form feed (ASCII 12).
\n-
Produces or matches a newline (ASCII 10).
\r-
Produces or matches a carriage return (ASCII 13).
\t-
Produces or matches a horizontal tab (ASCII 9).
\v-
Produces or matches a so called “vertical tab” (ASCII 11).
\cx-
Produces or matches CONTROL-x, where x is any character. The precise effect of ‘\cx’ is as follows: if x is a lower case letter, it is converted to upper case. Then bit 6 of the character (hex 40) is inverted. Thus ‘\cz’ becomes hex 1A, but ‘\c{’ becomes hex 3B, while ‘\c;’ becomes hex 7B.
\dxxx-
Produces or matches a character whose decimal ASCII value is xxx.
\oxxx-
Produces or matches a character whose octal ASCII value is xxx.
\xxx-
Produces or matches a character whose hexadecimal ASCII value is xx.
‘\b’ (backspace) was omitted because of the conflict with the existing “word boundary” meaning.
Escapes, Up: sed regular expressions [Contents][Index]
7
The following example uses the Greek letter Capital Sigma (Σ, Unicode code point
0x03A3). In aUTF-8locale,sedcorrectly processes the Sigma as one character despite it being 2 octets (bytes):$ locale | grep LANG LANG=en_US.UTF-8 $ printf 'a\u03A3b' aΣb $ printf 'a\u03A3b' | sed 's/./X/g' XXX $ printf 'a\u03A3b' | od -tx1 -An 61 ce a3 62
To force
sedto process octets separately, use theClocale (also known as thePOSIXlocale):$ printf 'a\u03A3b' | LC_ALL=C sed 's/./X/g' XXXX
The "s" Command.
Examples, Previous: sed regular expressions, Up: Top [Contents][Index]
Execution Cycle:
How sedworks? Hold and Pattern Buffers: ? Multiline techniques: Using D,G,H,N,P to process multiple lines ? Branching and flow control:
Hold and Pattern Buffers, Up: advanced sed [Contents][Index]
8 Then the next cycle starts for the next input line.
Unless special commands (like ‘D’) are used, the pattern space is deleted between two cycles. The hold space, on the other hand, keeps its data between cycles (see commands ‘h’, ‘H’, ‘x’, ‘g’, ‘G’ to move data between both buffers).
Multiline techniques, Previous: Execution Cycle, Up: advanced sed [Contents][Index]
Branching and flow control, Previous: Hold and Pattern Buffers, Up: advanced sed [Contents][Index]
Text search across multiple lines and Line length adjustment.
Multiline techniques, Up: advanced sed [Contents][Index]
Execution Cycle and Addresses overview.
seddoes not support a typicalif/thenconstruct. Instead, some commands can be used as conditionals or to change the default flow control:d-
delete (clears) the current pattern space, and restart the program cycle without processing the rest of the commands and without printing the pattern space.
D-
delete the contents of the pattern space up to the first newline, and restart the program cycle without processing the rest of the commands and without printing the pattern space.
[addr]X[addr]{ X ; X ; X }/regexp/X/regexp/{ X ; X ; X }-
Addresses and regular expressions can be used as an
if/thenconditional: If [addr] matches the current pattern space, execute the command(s). For example: The command/^#/dmeans: if the current pattern matches the regular expression^#(a line starting with a hash), then execute thedcommand: delete the line without printing it, and restart the program cycle immediately. b-
branch unconditionally (that is: always jump to a label, skipping or repeating other commands, without restarting a new cycle). Combined with an address, the branch can be conditionally executed on matched lines.
t-
branch conditionally (that is: jump to a label) only if a
s///command has succeeded since the last input line was read or another conditional branch was taken. T-
similar but opposite to the
tcommand: branch only if there has been no successful substitutions since the last input line was read.
The following two
sedprograms are equivalent. The first (contrived) example uses thebcommand to skip thes///command on lines containing ‘1’. The second example uses an address with negation (‘!’) to perform substitution only on desired lines. They///command is still executed on all lines:$ printf '%s\n' a1 a2 a3 | sed -E '/1/bx ; s/a/z/ ; :x ; y/123/456/' a4 z5 z6 $ printf '%s\n' a1 a2 a3 | sed -E '/1/!s/a/z/ ; y/123/456/' a4 z5 z6
Reporting Bugs.
- To further examine the difference between the two examples, try the following commands:
printf '%s\n' aa bb cc dd | sed ':x ; n ; = ; bx' printf '%s\n' aa bb cc dd | sed ':x ; N ; = ; bx' printf '%s\n' aa bb cc dd | sed ':x ; n ; s/\n/***/ ; bx' printf '%s\n' aa bb cc dd | sed ':x ; N ; s/\n/***/ ; bx'
quoted-printable files, typically used to encode email messages. In these files long lines are split and marked with a soft line break consisting of a single ‘=’ character at the end of the line:
$ cat jaques.txt All the wor= ld's a stag= e, And all the= men and wo= men merely = players: They have t= heir exits = and their e= ntrances; And one man= in his tim= e plays man= y parts.
The following program uses an address match ‘/=$/’ as a conditional: If the current pattern space ends with a ‘=’, it reads the next input line using
N, replaces all ‘=’ characters which are followed by a newline, and unconditionally branches (b) to the beginning of the program without restarting a new cycle. If the pattern space does not ends with ‘=’, the default action is performed: the pattern space is printed and a new cycle is started:$ sed ':x ; /=$/ { N ; s/=\n//g ; bx }' jaques.txt All the world's a stage, And all the men and women merely players: They have their exits and their entrances; And one man in his time plays many parts.Here’s an alternative program with a slightly different approach: On all lines except the last,
Nappends the line to the pattern space. A substitution command then removes soft line breaks (‘=’ at the end of a line, i.e. followed by a newline) by replacing them with an empty string. if the substitution was successful (meaning the pattern space contained a line which should be joined), The conditional branch commandtjumps to the beginning of the program without completing or restarting the cycle. If the substitution failed (meaning there were no soft line breaks), Thetcommand will not branch. Then,Pwill print the pattern space content until the first newline, andDwill delete the pattern space content until the first new line. (To learn more aboutN,PandDcommands see Multiline techniques).$ sed ':x ; $!N ; s/=\n// ; tx ; P ; D' jaques.txt All the world's a stage, And all the men and women merely players: They have their exits and their entrances; And one man in his time plays many parts.
For more line-joining examples see Joining lines.
Limitations, Previous: advanced sed, Up: Top [Contents][Index]
Joining lines:
Some exotic examples:
? Centering lines: ? Increment a number: ? Rename files to lower case: ? Print bash environment: ? Reverse chars of lines: ? Text search across multiple lines: ? Line length adjustment: Emulating standard utilities:
? tac: Reverse lines of files ? cat -n: Numbering lines ? cat -b: Numbering non-blank lines ? wc -c: Counting chars ? wc -w: Counting words ? wc -l: Counting lines ? head: Printing the first lines ? tail: Printing the last lines ? uniq: Make duplicate lines unique ? uniq -d: Print duplicated lines of input ? uniq -u: Remove all duplicated lines ? cat -s: Squeezing blank lines
Centering lines, Up: Examples [Contents][Index]
Multiline techniques and Branching and flow control.
Join specific lines (e.g. if lines 2 and 3 need to be joined):
$ cat lines.txt hello hel lo hello $ sed '2{N;s/\n//;}' lines.txt hello hello helloJoin backslash-continued lines:
$ cat 1.txt this \ is \ a \ long \ line and another \ line $ sed -e ':x /\\$/ { N; s/\\\n//g ; bx }' 1.txt this is a long line and another line #TODO: The above requires gnu sed. # non-gnu seds need newlines after ':' and 'b'Join lines that start with whitespace (e.g SMTP headers):
$ cat 2.txt Subject: Hello World Content-Type: multipart/alternative; boundary=94eb2c190cc6370f06054535da6a Date: Tue, 3 Jan 2017 19:41:16 +0000 (GMT) Authentication-Results: mx.gnu.org; dkim=pass header.i=@gnu.org; spf=pass Message-ID:From: John Doe To: Jane Smith $ sed -E ':a ; $!N ; s/\n\s+/ / ; ta ; P ; D' 2.txt Subject: Hello World Content-Type: multipart/alternative; boundary=94eb2c190cc6370f06054535da6a Date: Tue, 3 Jan 2017 19:41:16 +0000 (GMT) Authentication-Results: mx.gnu.org; dkim=pass header.i=@gnu.org; spf=pass Message-ID: From: John Doe To: Jane Smith # A portable (non-gnu) variation: # sed -e :a -e '$!N;s/\n */ /;ta' -e 'P;D'
Increment a number, Previous: Joining lines, Up: Examples [Contents][Index]
Rename files to lower case, Previous: Centering lines, Up: Examples [Contents][Index]
9 but must be done manually.
To increment one number you just add 1 to last digit, replacing it by the following digit. There is one exception: when the digit is a nine the previous digits must be also incremented until you don’t have a nine.
This solution by Bruno Haible is very clever and smart because it uses a single buffer; if you don’t have this limitation, the algorithm used in Numbering lines, is faster. It works by replacing trailing nines with an underscore, then using multiple
scommands to increment the last digit, and then again substituting underscores with zeros.#!/usr/bin/sed -f /[^0-9]/ d
# replace all trailing 9s by _ (any other character except digits, could # be used) :d s/9\(_*\)$/_\1/ td
# incr last digit only. The first line adds a most-significant # digit of 1 if we have to add a digit.
s/^\(_*\)$/1\1/; tn s/8\(_*\)$/9\1/; tn s/7\(_*\)$/8\1/; tn s/6\(_*\)$/7\1/; tn s/5\(_*\)$/6\1/; tn s/4\(_*\)$/5\1/; tn s/3\(_*\)$/4\1/; tn s/2\(_*\)$/3\1/; tn s/1\(_*\)$/2\1/; tn s/0\(_*\)$/1\1/; tn
:n y/_/0/
Print bash environment, Previous: Increment a number, Up: Examples [Contents][Index]
Reverse chars of lines, Previous: Rename files to lower case, Up: Examples [Contents][Index]
Text search across multiple lines, Previous: Print bash environment, Up: Examples [Contents][Index]
10
#!/usr/bin/sed -f /../! b
# Reverse a line. Begin embedding the line between two newlines s/^.*$/\ &\ /
# Move first character at the end. The regexp matches until # there are zero or one characters between the markers tx :x s/\(\n.\)\(.*\)\(.\n\)/\3\2\1/ tx
# Remove the newline markers s/\n//g
Line length adjustment, Previous: Reverse chars of lines, Up: Examples [Contents][Index]
Multiline techniques.
These examples deal with finding doubled occurrences of words in a document.
Finding doubled words in a single line is easy using GNU
grepand similarly with GNUsed:$ cat two-cities-dup1.txt It was the best of times, it was the worst of times, it was the the age of wisdom, it was the age of foolishness, $ grep -E '\b(\w+)\s+\1\b' two-cities-dup1.txt it was the the age of wisdom, $ grep -n -E '\b(\w+)\s+\1\b' two-cities-dup1.txt 3:it was the the age of wisdom, $ sed -En '/\b(\w+)\s+\1\b/p' two-cities-dup1.txt it was the the age of wisdom, $ sed -En '/\b(\w+)\s+\1\b/{=;p}' two-cities-dup1.txt 3 it was the the age of wisdom,- The regular expression ‘\b\w+\s+’ searches for word-boundary (‘\b’), followed by one-or-more word-characters (‘\w+’), followed by whitespace (‘\s+’). See regexp extensions.
- Adding parentheses around the ‘(\w+)’ expression creates a subexpression. The regular expression pattern ‘(PATTERN)\s+\1’ defines a subexpression (in the parentheses) followed by a back-reference, separated by whitespace. A successful match means the PATTERN was repeated twice in succession. See Back-references and Subexpressions.
- The word-boundery expression (‘\b’) at both ends ensures partial words are not matched (e.g. ‘the then’ is not a desired match).
- The -E option enables extended regular expression syntax, alleviating the need to add backslashes before the parenthesis. See ERE syntax.
When the doubled word span two lines the above regular expression will not find them as
grepandsedoperate line-by-line.By using
NandDcommands,sedcan apply regular expressions on multiple lines (that is, multiple lines are stored in the pattern space, and the regular expression works on it):$ cat two-cities-dup2.txt It was the best of times, it was the worst of times, it was the the age of wisdom, it was the age of foolishness, $ sed -En '{N; /\b(\w+)\s+\1\b/{=;p} ; D}' two-cities-dup2.txt 3 worst of times, it was the the age of wisdom,- The
Ncommand appends the next line to the pattern space (thus ensuring it contains two consecutive lines in every cycle). - The regular expression uses ‘\s+’ for word separator which matches both spaces and newlines.
- The regular expression matches, the entire pattern space is printed with
p. No lines are printed by default due to the -n option. - The
Dremoves the first line from the pattern space (up until the first newline), readying it for the next cycle.
See the GNU
coreutilsmanual for an alternative solution usingtr -sanduniqat https://gnu.org/s/coreutils/manual/html_node/Squeezing-and-deleting.html.
tac, Previous: Text search across multiple lines, Up: Examples [Contents][Index]
Multiline techniques and Branching and flow control.
This (somewhat contrived) example deal with formatting and wrapping lines of text of the following input file:
$ cat two-cities-mix.txt It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness,
The following sed program wraps lines at 40 characters:
$ cat wrap40.sed # outer loop :x # Appead a newline followed by the next input line to the pattern buffer N # Remove all newlines from the pattern buffer s/\n/ /g # Inner loop :y # Add a newline after the first 40 characters s/(.{40,40})/\1\n/ # If there is a newline in the pattern buffer # (i.e. the previous substitution added a newline) /\n/ { # There are newlines in the pattern buffer - # print the content until the first newline. P # Remove the printed characters and the first newline s/.*\n// # branch to label 'y' - repeat inner loop by } # No newlines in the pattern buffer - Branch to label 'x' (outer loop) # and read the next input line bxThe wrapped output:
$ sed -E -f wrap40.sed two-cities-mix.txt It was the best of times, it was the wor st of times, it was the age of wisdom, i t was the age of foolishness,
cat -n, Previous: Line length adjustment, Up: Examples [Contents][Index]
cat -b, Previous: tac, Up: Examples [Contents][Index]
wc -c, Previous: cat -n, Up: Examples [Contents][Index]
wc -w, Previous: cat -b, Up: Examples [Contents][Index]
11: first we convert units, removing ‘a’s from the number; then we rotate letters so that tens become ‘a’s, and so on until no more letters remain.
#!/usr/bin/sed -nf
# Add n+1 a's to hold space (+1 is for the newline) s/./a/g H x s/\n/a/
# Do the carry. The t's and b's are not necessary, # but they do speed up the thing t a : a; s/aaaaaaaaaa/b/g; t b; b done : b; s/bbbbbbbbbb/c/g; t c; b done : c; s/cccccccccc/d/g; t d; b done : d; s/dddddddddd/e/g; t e; b done : e; s/eeeeeeeeee/f/g; t f; b done : f; s/ffffffffff/g/g; t g; b done : g; s/gggggggggg/h/g; t h; b done : h; s/hhhhhhhhhh//g
: done $! { h b }# On the last line, convert back to decimal
: loop /a/! s/[b-h]*/&0/ s/aaaaaaaaa/9/ s/aaaaaaaa/8/ s/aaaaaaa/7/ s/aaaaaa/6/ s/aaaaa/5/ s/aaaa/4/ s/aaa/3/ s/aa/2/ s/a/1/
: next y/bcdefgh/abcdefg/ /[a-h]/ b loop p
wc -l, Previous: wc -c, Up: Examples [Contents][Index]
head, Previous: wc -w, Up: Examples [Contents][Index]
tail, Previous: wc -l, Up: Examples [Contents][Index]
uniq, Previous: head, Up: Examples [Contents][Index]
uniq -d, Previous: tail, Up: Examples [Contents][Index]
uniq -u, Previous: uniq, Up: Examples [Contents][Index]
cat -s, Previous: uniq -d, Up: Examples [Contents][Index]
uniq -u, Up: Examples [Contents][Index]
Other Resources, Previous: Examples, Up: Top [Contents][Index]
Reporting Bugs, Previous: Limitations, Up: Top [Contents][Index]
https://www.gnu.org/software/sed/.
Send general questions and suggestions to sed-devel@gnu.org. Visit the mailing list archives for past discussions at https://lists.gnu.org/archive/html/sed-devel/.
http://sed.sf.net
- sed FAQ: http://sed.sf.net/sedfaq.html
- seder’s grabbag: http://sed.sf.net/grabbag
- The
sed-usersmailing list maintained by Sven Guckes: http://groups.yahoo.com/group/sed-users/ (note this is not the GNUsedmailing list).
GNU Free Documentation License, Previous: Other Resources, Up: Top [Contents][Index]
bug-sed@gnu.org. Also, please include the output of ‘sed --version’ in the body of your report if at all possible.
Please do not send a bug report like this:
while building frobme-1.3.4 $ configure error→ sed: file sedscr line 1: Unknown option to 's'If GNU
seddoesn’t configure your favorite package, take a few extra minutes to identify the specific problem and make a stand-alone test case. Unlike other programs such as C compilers, making such test cases forsedis quite simple.A stand-alone test case includes all the data necessary to perform the test, and the specific invocation of
sedthat causes the problem. The smaller a stand-alone test case is, the better. A test case should not involve something as far removed fromsedas “try to configure frobme-1.3.4”. Yes, that is in principle enough information to look for the bug, but that is not a very practical prospect.Here are a few commonly reported bugs that are not bugs.
- 12. Or, when writing a script to read the next few lines following a pattern match, traditional implementations of
sedwould force you to write something like/foo/{ $!N; $!N; $!N; $!N; $!N; $!N; $!N; $!N; $!N }instead of just
/foo/{ N;N;N;N;N;N;N;N;N; }Invocation) lets you clobber protected files. This is not a bug, but rather a consequence of how the Unix file system works.The permissions on a file say what can happen to the data in that file, while the permissions on a directory say what can happen to the list of files in that directory. ‘sed -i’ will not ever open for writing a file that is already on disk. Rather, it will work on a temporary file that is finally renamed to the original name: if you rename or delete files, you’re actually modifying the contents of the directory, so the operation depends on the permissions of the directory, not of the file. For this same reason,
seddoes not let you use -i on a writable file in a read-only directory, and will break hard or symbolic links when -i is used on such a file. 0adoes not work (gives an error)- Concept Index, Previous: Reporting Bugs, Up: Top [Contents][Index]
https://fsf.org/ Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
- PREAMBLE
The purpose of this License is to make a manual, textbook, or other functional and useful document free in the sense of freedom: to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modifications made by others.
This License is a kind of “copyleft”, which means that derivative works of the document must themselves be free in the same sense. It complements the GNU General Public License, which is a copyleft license designed for free software.
We have designed this License in order to use it for manuals for free software, because free software needs free documentation: a free program should come with manuals providing the same freedoms that the software does. But this License is not limited to software manuals; it can be used for any textual work, regardless of subject matter or whether it is published as a printed book. We recommend this License principally for works whose purpose is instruction or reference.
- APPLICABILITY AND DEFINITIONS
This License applies to any manual or other work, in any medium, that contains a notice placed by the copyright holder saying it can be distributed under the terms of this License. Such a notice grants a world-wide, royalty-free license, unlimited in duration, to use that work under the conditions stated herein. The “Document”, below, refers to any such manual or work. Any member of the public is a licensee, and is addressed as “you”. You accept the license if you copy, modify or distribute the work in a way requiring permission under copyright law.
A “Modified Version” of the Document means any work containing the Document or a portion of it, either copied verbatim, or with modifications and/or translated into another language.
A “Secondary Section” is a named appendix or a front-matter section of the Document that deals exclusively with the relationship of the publishers or authors of the Document to the Document’s overall subject (or to related matters) and contains nothing that could fall directly within that overall subject. (Thus, if the Document is in part a textbook of mathematics, a Secondary Section may not explain any mathematics.) The relationship could be a matter of historical connection with the subject or with related matters, or of legal, commercial, philosophical, ethical or political position regarding them.
The “Invariant Sections” are certain Secondary Sections whose titles are designated, as being those of Invariant Sections, in the notice that says that the Document is released under this License. If a section does not fit the above definition of Secondary then it is not allowed to be designated as Invariant. The Document may contain zero Invariant Sections. If the Document does not identify any Invariant Sections then there are none.
The “Cover Texts” are certain short passages of text that are listed, as Front-Cover Texts or Back-Cover Texts, in the notice that says that the Document is released under this License. A Front-Cover Text may be at most 5 words, and a Back-Cover Text may be at most 25 words.
A “Transparent” copy of the Document means a machine-readable copy, represented in a format whose specification is available to the general public, that is suitable for revising the document straightforwardly with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file format whose markup, or absence of markup, has been arranged to thwart or discourage subsequent modification by readers is not Transparent. An image format is not Transparent if used for any substantial amount of text. A copy that is not “Transparent” is called “Opaque”.
Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly available DTD, and standard-conforming simple HTML, PostScript or PDF designed for human modification. Examples of transparent image formats include PNG, XCF and JPG. Opaque formats include proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machine-generated HTML, PostScript or PDF produced by some word processors for output purposes only.
The “Title Page” means, for a printed book, the title page itself, plus such following pages as are needed to hold, legibly, the material this License requires to appear in the title page. For works in formats which do not have any title page as such, “Title Page” means the text near the most prominent appearance of the work’s title, preceding the beginning of the body of the text.
The “publisher” means any person or entity that distributes copies of the Document to the public.
A section “Entitled XYZ” means a named subunit of the Document whose title either is precisely XYZ or contains XYZ in parentheses following text that translates XYZ in another language. (Here XYZ stands for a specific section name mentioned below, such as “Acknowledgements”, “Dedications”, “Endorsements”, or “History”.) To “Preserve the Title” of such a section when you modify the Document means that it remains a section “Entitled XYZ” according to this definition.
The Document may include Warranty Disclaimers next to the notice which states that this License applies to the Document. These Warranty Disclaimers are considered to be included by reference in this License, but only as regards disclaiming warranties: any other implication that these Warranty Disclaimers may have is void and has no effect on the meaning of this License.
- VERBATIM COPYING
You may copy and distribute the Document in any medium, either commercially or noncommercially, provided that this License, the copyright notices, and the license notice saying this License applies to the Document are reproduced in all copies, and that you add no other conditions whatsoever to those of this License. You may not use technical measures to obstruct or control the reading or further copying of the copies you make or distribute. However, you may accept compensation in exchange for copies. If you distribute a large enough number of copies you must also follow the conditions in section 3.
You may also lend copies, under the same conditions stated above, and you may publicly display copies.
- COPYING IN QUANTITY
If you publish printed copies (or copies in media that commonly have printed covers) of the Document, numbering more than 100, and the Document’s license notice requires Cover Texts, you must enclose the copies in covers that carry, clearly and legibly, all these Cover Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on the back cover. Both covers must also clearly and legibly identify you as the publisher of these copies. The front cover must present the full title with all words of the title equally prominent and visible. You may add other material on the covers in addition. Copying with changes limited to the covers, as long as they preserve the title of the Document and satisfy these conditions, can be treated as verbatim copying in other respects.
If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed (as many as fit reasonably) on the actual cover, and continue the rest onto adjacent pages.
If you publish or distribute Opaque copies of the Document numbering more than 100, you must either include a machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque copy a computer-network location from which the general network-using public has access to download using public-standard network protocols a complete Transparent copy of the Document, free of added material. If you use the latter option, you must take reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque copy (directly or through your agents or retailers) of that edition to the public.
It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version of the Document.
- MODIFICATIONS
You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3 above, provided that you release the Modified Version under precisely this License, with the Modified Version filling the role of the Document, thus licensing distribution and modification of the Modified Version to whoever possesses a copy of it. In addition, you must do these things in the Modified Version:
- Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from those of previous versions (which should, if there were any, be listed in the History section of the Document). You may use the same title as a previous version if the original publisher of that version gives permission.
- List on the Title Page, as authors, one or more persons or entities responsible for authorship of the modifications in the Modified Version, together with at least five of the principal authors of the Document (all of its principal authors, if it has fewer than five), unless they release you from this requirement.
- State on the Title page the name of the publisher of the Modified Version, as the publisher.
- Preserve all the copyright notices of the Document.
- Add an appropriate copyright notice for your modifications adjacent to the other copyright notices.
- Include, immediately after the copyright notices, a license notice giving the public permission to use the Modified Version under the terms of this License, in the form shown in the Addendum below.
- Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document’s license notice.
- Include an unaltered copy of this License.
- Preserve the section Entitled “History”, Preserve its Title, and add to it an item stating at least the title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is no section Entitled “History” in the Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add an item describing the Modified Version as stated in the previous sentence.
- Preserve the network location, if any, given in the Document for public access to a Transparent copy of the Document, and likewise the network locations given in the Document for previous versions it was based on. These may be placed in the “History” section. You may omit a network location for a work that was published at least four years before the Document itself, or if the original publisher of the version it refers to gives permission.
- For any section Entitled “Acknowledgements” or “Dedications”, Preserve the Title of the section, and preserve in the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein.
- Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the equivalent are not considered part of the section titles.
- Delete any section Entitled “Endorsements”. Such a section may not be included in the Modified Version.
- Do not retitle any existing section to be Entitled “Endorsements” or to conflict in title with any Invariant Section.
- Preserve any Warranty Disclaimers.
If the Modified Version includes new front-matter sections or appendices that qualify as Secondary Sections and contain no material copied from the Document, you may at your option designate some or all of these sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version’s license notice. These titles must be distinct from any other section titles.
You may add a section Entitled “Endorsements”, provided it contains nothing but endorsements of your Modified Version by various parties—for example, statements of peer review or that the text has been approved by an organization as the authoritative definition of a standard.
You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-Cover Text and one of Back-Cover Text may be added by (or through arrangements made by) any one entity. If the Document already includes a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher that added the old one.
The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modified Version.
- COMBINING DOCUMENTS
You may combine the Document with other documents released under this License, under the terms defined in section 4 above for modified versions, provided that you include in the combination all of the Invariant Sections of all of the original documents, unmodified, and list them all as Invariant Sections of your combined work in its license notice, and that you preserve all their Warranty Disclaimers.
The combined work need only contain one copy of this License, and multiple identical Invariant Sections may be replaced with a single copy. If there are multiple Invariant Sections with the same name but different contents, make the title of each such section unique by adding at the end of it, in parentheses, the name of the original author or publisher of that section if known, or else a unique number. Make the same adjustment to the section titles in the list of Invariant Sections in the license notice of the combined work.
In the combination, you must combine any sections Entitled “History” in the various original documents, forming one section Entitled “History”; likewise combine any sections Entitled “Acknowledgements”, and any sections Entitled “Dedications”. You must delete all sections Entitled “Endorsements.”
- COLLECTIONS OF DOCUMENTS
You may make a collection consisting of the Document and other documents released under this License, and replace the individual copies of this License in the various documents with a single copy that is included in the collection, provided that you follow the rules of this License for verbatim copying of each of the documents in all other respects.
You may extract a single document from such a collection, and distribute it individually under this License, provided you insert a copy of this License into the extracted document, and follow this License in all other respects regarding verbatim copying of that document.
- AGGREGATION WITH INDEPENDENT WORKS
A compilation of the Document or its derivatives with other separate and independent documents or works, in or on a volume of a storage or distribution medium, is called an “aggregate” if the copyright resulting from the compilation is not used to limit the legal rights of the compilation’s users beyond what the individual works permit. When the Document is included in an aggregate, this License does not apply to the other works in the aggregate which are not themselves derivative works of the Document.
If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the Document is less than one half of the entire aggregate, the Document’s Cover Texts may be placed on covers that bracket the Document within the aggregate, or the electronic equivalent of covers if the Document is in electronic form. Otherwise they must appear on printed covers that bracket the whole aggregate.
- TRANSLATION
Translation is considered a kind of modification, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License, and all the license notices in the Document, and any Warranty Disclaimers, provided that you also include the original English version of this License and the original versions of those notices and disclaimers. In case of a disagreement between the translation and the original version of this License or a notice or disclaimer, the original version will prevail.
If a section in the Document is Entitled “Acknowledgements”, “Dedications”, or “History”, the requirement (section 4) to Preserve its Title (section 1) will typically require changing the actual title.
- TERMINATION
You may not copy, modify, sublicense, or distribute the Document except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, or distribute it is void, and will automatically terminate your rights under this License.
However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice.
Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, receipt of a copy of some or all of the same material does not give you any rights to use it.
- FUTURE REVISIONS OF THIS LICENSE
The Free Software Foundation may publish new, revised versions of the GNU Free Documentation License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. See https://www.gnu.org/copyleft/.
Each version of the License is given a distinguishing version number. If the Document specifies that a particular numbered version of this License “or any later version” applies to it, you have the option of following the terms and conditions either of that specified version or of any later version that has been published (not as a draft) by the Free Software Foundation. If the Document does not specify a version number of this License, you may choose any version ever published (not as a draft) by the Free Software Foundation. If the Document specifies that a proxy can decide which future versions of this License can be used, that proxy’s public statement of acceptance of a version permanently authorizes you to choose that version for the Document.
- RELICENSING
“Massive Multiauthor Collaboration Site” (or “MMC Site”) means any World Wide Web server that publishes copyrightable works and also provides prominent facilities for anybody to edit those works. A public wiki that anybody can edit is an example of such a server. A “Massive Multiauthor Collaboration” (or “MMC”) contained in the site means any set of copyrightable works thus published on the MMC site.
“CC-BY-SA” means the Creative Commons Attribution-Share Alike 3.0 license published by Creative Commons Corporation, a not-for-profit corporation with a principal place of business in San Francisco, California, as well as future copyleft versions of that license published by that same organization.
“Incorporate” means to publish or republish a Document, in whole or in part, as part of another Document.
An MMC is “eligible for relicensing” if it is licensed under this License, and if all works that were first published under this License somewhere other than this MMC, and subsequently incorporated in whole or in part into the MMC, (1) had no cover texts or invariant sections, and (2) were thus incorporated prior to November 1, 2008.
The operator of an MMC Site may republish an MMC contained in the site under CC-BY-SA on the same site at any time before August 1, 2009, provided the MMC is eligible for relicensing.
Command and Option Index, Previous: GNU Free Documentation License, Up: Top [Contents][Index]
- 0 ;
A B C D E F G H I J L M N O P Q R S T U V W X ZIndex Entry Section -e, example: Overview -e, example: sed script overview –expression, example: Overview -f, example: Overview -f, example: sed script overview –file, example: Overview -i, example: Overview -n, example: Overview -s, example: Overview 0address:Reporting Bugs ;, command separator: sed script overview a, and semicolons:sed script overview Additional reading about sed:Other Resources addr1,+N: Range Addresses addr1,~N: Range Addresses address range, example: sed script overview Address, as a regular expression: Regexp Addresses Address, last line: Numeric Addresses Address, numeric: Numeric Addresses addresses, excluding: Addresses overview Addresses, in sedscripts:Numeric Addresses addresses, negating: Addresses overview addresses, numeric: Addresses overview addresses, range: Addresses overview addresses, regular expression: Addresses overview addresses, syntax: sed script overview alphabetic characters: Character Classes and Bracket Expressions alphanumeric characters: Character Classes and Bracket Expressions Append hold space to pattern space: Other Commands Append next input line to pattern space: Other Commands Append pattern space to hold space: Other Commands Appending text after a line: Other Commands b, joining lines with: Branching and flow control b, versus t: Branching and flow control back-reference: Back-references and Subexpressions Backreferences, in regular expressions: The "s" Command blank characters: Character Classes and Bracket Expressions bracket expression: Character Classes and Bracket Expressions Branch to a label, if s///failed:Extended Commands Branch to a label, if s///succeeded:Programming Commands Branch to a label, unconditionally: Programming Commands branching and n, N: Branching and flow control branching, infinite loop: Branching and flow control branching, joining lines: Branching and flow control Buffer spaces, pattern and hold: Execution Cycle Bugs, reporting: Reporting Bugs c, and semicolons:sed script overview case insensitive, regular expression: Regexp Addresses Case-insensitive matching: The "s" Command Caveat — #n on first line: Common Commands character class: Character Classes and Bracket Expressions character classes: Character Classes and Bracket Expressions classes of characters: Character Classes and Bracket Expressions Command groups: Common Commands Comments, in scripts: Common Commands Conditional branch: Programming Commands Conditional branch: Extended Commands control characters: Character Classes and Bracket Expressions Copy hold space into pattern space: Other Commands Copy pattern space into hold space: Other Commands cycle, restarting: Branching and flow control d, example:sed script overview Delete first line from pattern space: Other Commands digit characters: Character Classes and Bracket Expressions Disabling autoprint, from command line: Command-Line Options empty regular expression: Regexp Addresses Emptying pattern space: Extended Commands Emptying pattern space: Reporting Bugs Evaluate Bourne-shell commands: Extended Commands Evaluate Bourne-shell commands, after substitution: The "s" Command example, address range: sed script overview example, regular expression: sed script overview Exchange hold space with pattern space: Other Commands Excluding lines: Addresses overview exit status: Exit status exit status, example: Exit status Extended regular expressions, choosing: Command-Line Options Extended regular expressions, syntax: ERE syntax File name, printing: Extended Commands Files to be processed as input: Command-Line Options Flow of control in scripts: Programming Commands Global substitution: The "s" Command GNU extensions, /dev/stderr file: The "s" Command GNU extensions, /dev/stderr file: Other Commands GNU extensions, /dev/stdin file: Other Commands GNU extensions, /dev/stdin file: Extended Commands GNU extensions, /dev/stdout file: Command-Line Options GNU extensions, /dev/stdout file: The "s" Command GNU extensions, /dev/stdout file: Other Commands GNU extensions, 0address:Range Addresses GNU extensions, 0address:Reporting Bugs GNU extensions, 0,addr2 addressing: Range Addresses GNU extensions, addr1,+N addressing: Range Addresses GNU extensions, addr1,~N addressing: Range Addresses GNU extensions, branch if s///failed:Extended Commands GNU extensions, case modifiers in scommands:The "s" Command GNU extensions, checking for their presence: Extended Commands GNU extensions, debug: Command-Line Options GNU extensions, disabling: Command-Line Options GNU extensions, emptying pattern space: Extended Commands GNU extensions, emptying pattern space: Reporting Bugs GNU extensions, evaluating Bourne-shell commands: The "s" Command GNU extensions, evaluating Bourne-shell commands: Extended Commands GNU extensions, extended regular expressions: Command-Line Options GNU extensions, gand number modifier:The "s" Command GNU extensions, Imodifier:The "s" Command GNU extensions, Imodifier:Regexp Addresses GNU extensions, in-place editing: Command-Line Options GNU extensions, in-place editing: Reporting Bugs GNU extensions, Mmodifier:The "s" Command GNU extensions, Mmodifier:Regexp Addresses GNU extensions, modifiers and the empty regular expression: Regexp Addresses GNU extensions, ‘n~m’ addresses: Numeric Addresses GNU extensions, quitting silently: Extended Commands GNU extensions, Rcommand:Extended Commands GNU extensions, reading a file a line at a time: Extended Commands GNU extensions, returning an exit code: Common Commands GNU extensions, returning an exit code: Extended Commands GNU extensions, setting line length: Other Commands GNU extensions, special escapes: Escapes GNU extensions, special escapes: Reporting Bugs GNU extensions, special two-address forms: Range Addresses GNU extensions, subprocesses: The "s" Command GNU extensions, subprocesses: Extended Commands GNU extensions, to basic regular expressions: BRE syntax GNU extensions, to basic regular expressions: BRE syntax GNU extensions, to basic regular expressions: BRE syntax GNU extensions, to basic regular expressions: BRE syntax GNU extensions, to basic regular expressions: BRE syntax GNU extensions, to basic regular expressions: Reporting Bugs GNU extensions, two addresses supported by most commands: Other Commands GNU extensions, two addresses supported by most commands: Other Commands GNU extensions, two addresses supported by most commands: Other Commands GNU extensions, two addresses supported by most commands: Other Commands GNU extensions, unlimited line length: Limitations GNU extensions, writing first line to a file: Extended Commands Goto, in scripts: Programming Commands graphic characters: Character Classes and Bracket Expressions Greedy regular expression matching: BRE syntax Grouping commands: Common Commands hexadecimal digits: Character Classes and Bracket Expressions Hold space, appending from pattern space: Other Commands Hold space, appending to pattern space: Other Commands Hold space, copy into pattern space: Other Commands Hold space, copying pattern space into: Other Commands Hold space, definition: Execution Cycle Hold space, exchange with pattern space: Other Commands i, and semicolons:sed script overview In-place editing: Reporting Bugs In-place editing, activating: Command-Line Options In-place editing, Perl-style backup file names: Command-Line Options infinite loop, branching: Branching and flow control Inserting text before a line: Other Commands joining lines with branching: Branching and flow control joining quoted-printable lines: Branching and flow control labels: Branching and flow control Labels, in scripts: Programming Commands Last line, selecting: Numeric Addresses Line length, setting: Command-Line Options Line length, setting: Other Commands Line number, printing: Other Commands Line selection: Numeric Addresses Line, selecting by number: Numeric Addresses Line, selecting by regular expression match: Regexp Addresses Line, selecting last: Numeric Addresses List pattern space: Other Commands lower-case letters: Character Classes and Bracket Expressions Mixing gand number modifiers in thescommand:The "s" Command multiple files: Overview multiple sedcommands:sed script overview n, and branching: Branching and flow control N, and branching: Branching and flow control named character classes: Character Classes and Bracket Expressions newline, command separator: sed script overview Next input line, append to pattern space: Other Commands Next input line, replace pattern space with: Common Commands Non-bugs, 0address:Reporting Bugs Non-bugs, in-place editing: Reporting Bugs Non-bugs, localization-related: Reporting Bugs Non-bugs, localization-related: Reporting Bugs Non-bugs, Ncommand on the last line:Reporting Bugs Non-bugs, regex syntax clashes: Reporting Bugs numeric addresses: Addresses overview numeric characters: Character Classes and Bracket Expressions omitting labels: Branching and flow control output: Overview output, suppressing: Overview p, example: Overview paragraphs, processing: Multiline techniques parameters, script: Overview Parenthesized substrings: The "s" Command Pattern space, definition: Execution Cycle Portability, comments: Common Commands Portability, line length limitations: Limitations Portability, Ncommand on the last line:Reporting Bugs POSIXLY_CORRECTbehavior, bracket expressions:Character Classes and Bracket Expressions POSIXLY_CORRECTbehavior, enabling:Command-Line Options POSIXLY_CORRECTbehavior, escapes:Escapes POSIXLY_CORRECTbehavior,Ncommand:Reporting Bugs Print first line from pattern space: Other Commands printable characters: Character Classes and Bracket Expressions Printing file name: Extended Commands Printing line number: Other Commands Printing text unambiguously: Other Commands processing paragraphs: Multiline techniques punctuation characters: Character Classes and Bracket Expressions Q, example: Exit status q, example:sed script overview Quitting: Common Commands Quitting: Extended Commands quoted-printable lines, joining: Branching and flow control range addresses: Addresses overview range expression: Character Classes and Bracket Expressions Range of lines: Range Addresses Range with start address of zero: Range Addresses Read next input line: Common Commands Read text from a file: Other Commands Read text from a file: Extended Commands regex addresses and input lines: Regexp Addresses regex addresses and pattern space: Regexp Addresses regular expression addresses: Addresses overview regular expression, example: sed script overview Replace hold space with copy of pattern space: Other Commands Replace pattern space with copy of hold space: Other Commands Replacing all text matching regexp in a line: The "s" Command Replacing only nth match of regexp in a line: The "s" Command Replacing selected lines with other text: Other Commands Requiring GNU sed:Extended Commands restarting a cycle: Branching and flow control Sandbox mode: Command-Line Options script parameter: Overview Script structure: sed script overview Script, from a file: Command-Line Options Script, from command line: Command-Line Options sedcommands syntax:sed script overview sedcommands, multiple:sed script overview sedscript structure:sed script overview Selecting lines to process: Numeric Addresses Selecting non-matching lines: Addresses overview semicolons, command separator: sed script overview Several lines, selecting: Range Addresses Slash character, in regular expressions: Regexp Addresses space characters: Character Classes and Bracket Expressions Spaces, pattern and hold: Execution Cycle Special addressing forms: Range Addresses standard input: Overview Standard input, processing as input: Command-Line Options standard output: Overview stdin: Overview stdout: Overview Stream editor: Introduction subexpression: Back-references and Subexpressions Subprocesses: The "s" Command Subprocesses: Extended Commands Substitution of text, options: The "s" Command suppressing output: Overview syntax, addresses: sed script overview syntax, sedcommands:sed script overview t, joining lines with: Branching and flow control t, versus b: Branching and flow control Text, appending: Other Commands Text, deleting: Common Commands Text, insertion: Other Commands Text, printing: Common Commands Text, printing after substitution: The "s" Command Text, writing to a file after substitution: The "s" Command Transliteration: Other Commands Unbuffered I/O, choosing: Command-Line Options upper-case letters: Character Classes and Bracket Expressions Usage summary, printing: Command-Line Options Version, printing: Command-Line Options whitespace characters: Character Classes and Bracket Expressions Working on separate files: Command-Line Options Write first line to a file: Extended Commands Write to a file: Other Commands xdigit class: Character Classes and Bracket Expressions Zero, as range start address: Range Addresses Jump to: - 0 ;
A B C D E F G H I J L M N O P Q R S T U V W X Z
Concept Index, Up: Top [Contents][Index]
# - : = {
A B C D E F G H I L N P Q R S T U V W X Y ZIndex Entry Section # (comments):Common Commands --binary:Command-Line Options --debug:Command-Line Options --expression:Command-Line Options --file:Command-Line Options --follow-symlinks:Command-Line Options --help:Command-Line Options --in-place:Command-Line Options --line-length:Command-Line Options --null-data:Command-Line Options --posix:Command-Line Options --quiet:Command-Line Options --regexp-extended:Command-Line Options --sandbox:Command-Line Options --separate:Command-Line Options --silent:Command-Line Options --unbuffered:Command-Line Options --version:Command-Line Options --zero-terminated:Command-Line Options -b:Command-Line Options -e:Command-Line Options -E:Command-Line Options -f:Command-Line Options -i:Command-Line Options -l:Command-Line Options -n:Command-Line Options -n, forcing from within a script:Common Commands -r:Command-Line Options -s:Command-Line Options -u:Command-Line Options -z:Command-Line Options : (label) command:Programming Commands = (print line number) command:Other Commands {} command grouping:Common Commands a (append text lines) command:Other Commands alnum character class:Character Classes and Bracket Expressions alpha character class:Character Classes and Bracket Expressions b (branch) command:Programming Commands blank character class:Character Classes and Bracket Expressions c (change to text lines) command:Other Commands cntrl character class:Character Classes and Bracket Expressions D (delete first line) command:Other Commands d (delete) command:Common Commands digit character class:Character Classes and Bracket Expressions e (evaluate) command:Extended Commands F (File name) command:Extended Commands G (appending Get) command:Other Commands g (get) command:Other Commands graph character class:Character Classes and Bracket Expressions H (append Hold) command:Other Commands h (hold) command:Other Commands i (insert text lines) command:Other Commands l (list unambiguously) command:Other Commands lower character class:Character Classes and Bracket Expressions N (append Next line) command:Other Commands n (next-line) command:Common Commands P (print first line) command:Other Commands p (print) command:Common Commands print character class:Character Classes and Bracket Expressions punct character class:Character Classes and Bracket Expressions q (quit) command:Common Commands Q (silent Quit) command:Extended Commands r (read file) command:Other Commands R (read line) command:Extended Commands s command, option flags:The "s" Command space character class:Character Classes and Bracket Expressions T (test and branch if failed) command:Extended Commands t (test and branch if successful) command:Programming Commands upper character class:Character Classes and Bracket Expressions v (version) command:Extended Commands w (write file) command:Other Commands W (write first line) command:Extended Commands x (eXchange) command:Other Commands xdigit character class:Character Classes and Bracket Expressions y (transliterate) command:Other Commands z (Zap) command:Extended Commands Jump to: # - : = {
A B C D E F G H I L N P Q R S T U V W X Y Z1 Introduction
- 2 Running sed
- 2.1 Overview
- 2.2 Command-Line Options
- 2.3 Exit status
- 3
sedscripts- 3.1
sedscript overview - 3.2
sedcommands summary - 3.3 The
sCommand - 3.4 Often-Used Commands
- 3.5 Less Frequently-Used Commands
- 3.6 Commands for
sedgurus - 3.7 Commands Specific to GNU
sed - 3.8 Multiple commands syntax
- 3.8.1 Commands Requiring a newline
- 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 of regular expression in
sed - 5.2 Basic (BRE) and extended (ERE) regular expression
- 5.3 Overview of basic regular expression syntax
- 5.4 Overview of extended regular expression syntax
- 5.5 Character Classes and Bracket Expressions
- 5.6 regular expression extensions
- 5.7 Back-references and Subexpressions
- 5.8 Escape Sequences - specifying special characters
- 5.8.1 Escaping Precedence
- 5.9 Multibyte characters and Locale Considerations
- 5.9.1 Invalid multibyte characters
- 5.9.2 Upper/Lower case conversion
- 5.9.3 Multibyte regexp character classes
- 6 Advanced
sed: cycles and buffers- 6.1 How
sedWorks - 6.2 Hold and Pattern Buffers
- 6.3 Multiline techniques - using D,G,H,N,P to process multiple lines
- 6.4 Branching and Flow Control
- 6.4.1 Branching and Cycles
- 6.4.2 Branching example: joining lines
- 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
bashEnvironment - 7.6 Reverse Characters of Lines
- 7.7 Text search across multiple lines
- 7.8 Line length adjustment
- 7.9 Reverse Lines of Files
- 7.10 Numbering Lines
- 7.11 Numbering Non-blank Lines
- 7.12 Counting Characters
- 7.13 Counting Words
- 7.14 Counting Lines
- 7.15 Printing the First Lines
- 7.16 Printing the Last Lines
- 7.17 Make Duplicate Lines Unique
- 7.18 Print Duplicated Lines of Input
- 7.19 Remove All Duplicated Lines
- 7.20 Squeezing Blank Lines
- 8 GNU
sed’s Limitations and Non-limitations- 9 Other Resources for Learning About
sed- 10 Reporting Bugs
- Appendix A GNU Free Documentation License
- Concept Index
- Command and Option Index
Footnotes
(1)
This applies to commands such as
=,a,c,i,l,p. You can still write to the standard output by using thew(2)Note that GNU
sedcreates the backup file whether or not any output is actually changed.(3)
This is equivalent to
punless the -i option is being used.(4)
This is equivalent to
punless the -i option is being used.(5)
There are of course many other ways to do the same, e.g.
grep 'bash$' /etc/passwd awk -F: '$7 == "/bin/bash"' /etc/passwd
(6)
All the escapes introduced here are GNU extensions, with the exception of
\n. In basic regular expression mode, settingPOSIXLY_CORRECTdisables them inside bracket expressions.(7)
Some regexp edge-cases depends on the operating system and libc implementation. The examples shown are known to work as-expected on GNU/Linux systems using glibc.
(8)
Actually, if
sedprints a line without the terminating newline, it will nevertheless print the missing newline as soon as more text is sent to the same output stream, which gives the “least expected surprise” even though it does not make commands like ‘sed -n p’ exactly identical tocat.(9)
sedguru Greg Ubben wrote an implementation of thedcRPN calculator! It is distributed together with sed.(10)
This requires another script to pad the output of banner; for example
#! /bin/sh banner -w $1 $2 $3 $4 | sed -e :a -e '/^.\{0,'$1'\}$/ { s/$/ /; ba; }' | ~/sedscripts/reverseline.sed(11)
Some implementations have a limit of 199 commands per script
(12)
which is the actual “bug” that prompted the change in behavior
- PREAMBLE