Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sed4.4 manual

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Sed Stream Editor Catalog 1 introduction 2 run sed2.1 Overview 2.2 Command Line options 2.3 exit status 3sed script 3.1 sed script Overview 3.2 sed Command Summary 3.3 s commands 3.5 less used commands 3.6 sed Master commands 3.7 GNU specific commands sed3.8 multiple command syntax 3.8.1 Command 4 address that requires newline characters: select Line 4.1 address Overview 4.2 Select by number Line 4.3 Select Line 4.4 range address 5 regular expression: select text 5.1 regular expression Overview sed5.2 basic (BRE) and extended (ERE) regular expression 5.3 basic regular expression Syntax Overview 5.4 extended regular expression Grammar Overview 5.5 character classes and parenthesis expressions 5.6 regular expressions Then the expression extension 5.7 backreferences and subexpressions 5.8 escape sequences-specify special characters 5.9 locale considerations 6 advanced sed: loops and buffers 6.1 sed how it works 6.2 hold and pattern buffers 6.3 multiline technology-use D. G,H,N P to handle multiple lines 6.4 branches and flow control 6.4.1 branches and cycle 6.4.2 branches example: connection 7 some sample scripts 7.1 connection 7.2 centerline 7.3 increase the number 7.4 rename files to lowercase 7.5 print bash environment 7.6 lines reverse characters 7.7 across multiple lines text search 7.8 lines adjust the reverse lines of 7.9 files 7.10 No. 7.11 No. Non-blank lines 7.12 count characters 7.13 count words 7.14 count lines 7.15 print first line 7.16 print last line 7.17 repeat lines unique 7.18 print input duplicate lines 7.19 Delete all duplicate lines 7.20 squeeze blank lines 8 GNU sed restrictions and unrestricted 9 other resources learn about sed10 reporting errors Appendix A GNU Freedom File license Conceptual Index Command and options Index GNU sed

This file records GNU version 4.4 sed, a stream editor.

Copyright ©1998-2017 Free Software Foundation

Authorizes the reproduction, distribution and / or modification of this document under the terms of the GNU Free File license version 1.3 or any newer version issued by the Free Software Foundation; there is no unchanged part, no cover text, no back cover text. A copy of the license is included in the section entitled "GNU Free File license".

? Introduction: introduction? Call sed: call? Sed script: sed script? Sed address: address: select line? Sed regular expressions: regular expressions: select text? Advanced sed: advanced sed: loops and buffers? Example: some sample scripts? Restrictions: GNU restrictions and (non) restrictions sed? Other resources: other resources are used to learn sed? Report errors: report errors? GNU Free documentation license: copy and share this manual? Conceptual indicators: this manual contains menus for all topics. ? Command and option index: a menu with all sed commands and command line options. 1 introduction

Sed is a stream editor. The stream editor is used to perform basic text conversion on the input stream (the input of a file or pipeline). Although in some ways similar to editors that allow script editing (such as ed), sed can only be typed once, but it is more efficient. However, it has the ability of sed to filter text in pipes, which is particularly different from other types of editors.

2 run sed

This chapter describes how to run sed. Details of sed scripts and individual sed commands are discussed in the next chapter.

? Overview:? Command line options:? Exit status: 2.1 Overview

Usually sed is called like this:

Sed SCRIPT INPUTFILE...

For example, to replace all the occurrences of "Hello" to "World" in the file input.txt:

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

If you do not specify INPUTFILE, or if INPUTFILE is -, sed filters standard input. The following commands are equivalent:

Sed / s / hello / world / 'input.txt > output.txt sed's / hello / world /' output.txt cat input.txt | sed / s / hello / world /'- > output.txt

Sed writes the output to standard output. Use-I to edit files on the spot instead of printing to standard output. See also W and s _ bind _ foreign to write commands to other files. The following command modifies the file.txt and produces no output:

Sed-I / hello / world'file.txt

By default, sed prints all processed inputs (except input d that is modified / deleted by the command). Use-n to suppress the output, and p to print a specific line. The following command prints only line 45 of the input file:

Sed-n'45p'file.txt

Sed treats multiple input files as one long stream. The following example prints the first line of the first file (one.txt is saved at this point) and the last line of the last file (three.txt). Use-s to reverse this behavior.

Sed-nasty 1p; $p'one.txt two.txt three.txt

Without the-e or-F option, sed uses the first non-option parameter as the script, and the following non-option parameter as the input file. If the-e or-F option is used to specify the script, all non-option parameters are used as input files. The options-e and-F can be combined and can occur multiple times (in which case the final valid script will connect all individual scripts).

The following example is equivalent:

Sed'/ hello / world / 'input.txt > output.txt sed-estrangs / hello / world /' input.txt > output.txt sed-- expression ='s / hello / world / 'input.txt > output.txt echo's / hello / world /' > myscript.sed sed-f myscript.sed input.txt > output.txt sed-- file = myscript.sed input.txt > output.txt 2.2 command line options

The full format of the call sed is:

Sed option. [SCRIPT] [INPUTFILE...]

Sed can be called using the following command line options:

-- version

Print the running version and copyright notice of sed, and then exit.

-- help

Print the usage message, briefly summarize these command line options and error report addresses, and then exit.

-n-- quiet-- silent

By default, sed prints out the pattern space by script at the end of each loop (see how sed works). These options disable this automatic printing, and sed produces output only when explicitly told by the p command.

-e script-- expression=script

Add the commands in the script to the set of commands to run when you want to process input.

-f script-file-- file=script-file

Add the commands contained in the file script file to the set of commands to run when you want to process input.

-I [SUFFIX]-- in-place [= SUFFIX]

This option specifies that you want to edit the file in the field. GNU sed creates a temporary file and sends the output to this file instead of standard output. one.

This option means-s.

When the end of the file arrives, the temporary file is renamed to the original name of the output file. The extension, if provided, is used to back up copy 2 by modifying the name of the old file before renaming the temporary file.

This rule follows: if the extension does not contain a *, it is added to the end of the current file name as a suffix; if the extension contains one or more * characters, each asterisk is replaced with the current file name. This allows you to prefix the backup file instead of (or in addition to) the suffix, or even place a backup copy of the original file in another directory if the directory already exists.

If no extension is provided, the original file will be overwritten without backup.

-l N-- line-length=N

Specifies the default newline length l for the command. A length of zero (zero) means never winding a long wire. If not specified, it is 70.

-- posix

GNU sed includes several extensions for POSIX sed. To simplify writing portable scripts, this option disables the extension of all documents in this manual, including other commands. Most extended sed programs accept programs other than the syntax specified by POSIX, but some of them (such as the behavior of commands described by N in Reporting Bugs) actually violate the standard. If you want to disable only the latter extension, you can set the variable to a non-null value. POSIXLY_CORRECT

-b-- binary

This option is available on each platform, but only if the operating system distinguishes between text files and binaries. When such a distinction is made, as in the case of MS-DOS,Windows, the Cygwin text file consists of lines separated by carriage returns and newline characters, and the sed cannot see the CR at the end. When this option is specified, sed opens the input file in binary mode, so this special treatment is not required and lines ending with lines are considered.

-- follow-symlinks

This option is only available on platforms that support symbolic links, and has an effect only when it is selected-I is specified. In this case, if the file specified on the command line is a symbolic link, sed follows the link and edits the final destination of the link. The default behavior is to break the symbolic link so that the link destination is not modified.

-E-r-- regexp-extended

Use extended regular expressions instead of basic regular expressions. Extended regular expressions are accepted by those egrep; they can be clearer because they usually have fewer backslashes. Historically this is a GNU extension, but the-E extension has been added to the POSIX standard (http://austingroupbugs.net/view.php?id=528), so use-E for portability. GNU sed has accepted-E as an unlicensed option for years, and * BSD seds has accepted-E for years, but the script-E used may not be migrated to other older systems. See extended regular expressions.

-s-- separate

By default, sed treats the files specified on the command line as a single continuous long stream. The GNU sed extension allows users to think of them as separate files: range addresses (such as "/ ABC /, / DEF /') are not allowed to span multiple files, the line number is relative to the beginning of each file, $refers to the last line of each file, and files called from the R command are rewound at the beginning of each file.

-- sandbox

In sandboxed mode, e/w/r commands are rejected-the program that contains them will be aborted without running. Sandbox mode ensures that sed runs only on the input file specified on the command line, and that external programs cannot be run.

-u-- unbuffered

Buffer input and output as little as possible. (if the input comes from the tail, you want to see the converted output as soon as possible.)

-z-- null-data-- zero-terminated

Treat input as a set of lines, each with zero bytes (ASCII'NUL' characters) instead of newline characters. This option can be used for commands such as'- z 'and' find-print0' to handle arbitrary file names.

If the-e,-F,-expression, or-file option is given on the command line, the first non-option argument on the command line is considered the script to be executed.

If there are still any command-line arguments after processing the above, they are interpreted as the name of the input file to be processed. File name "- 'refers to the standard input stream. If no file name is specified, standard input will be processed.

2.3 exit status

A zero exit status indicates success, while a non-zero value indicates failure. GNU sed returns the following exit status error value:

0

Completed successfully

one

Invalid command, invalid syntax, invalid regular expression, or GNU sed extension command used with it-posix.

two

One or more input files specified on the command line cannot be opened (for example, if the file cannot be found or read permission is denied). Processing continues with other files.

four

GNU sed aborts immediately if there is an I / O error or a serious processing error at run time.

In addition, the commands Q and Q can be used to terminate sed and custom exit code values (this is a GNU sed extension):

$echo | sed'Q42'; echo $? forty-two

3 sed scripts? Sed script Overview: sed script Overview? Sed command list: summary of sed commands? "s" command: sed's Swiss Army knife? Common commands: frequently used commands? Other commands: commands that are not commonly used? Programming commands: master sed commands? Extension command: GNU-specific command sed? Multiple command syntax: extension easier script 3.1 sed script overview

A sed program consists of one or more sed commands, passed by one or more-e,-F,-expressions, and-file options or the first non-option argument, if using zeros of these options. This article will refer to the "the" sed script; this is understood to mean the sequential concatenation of all scripts and script files passed in. See the overview.

The sed command follows the following syntax:

[addr] X [options]

X is a single letter sed command. [addr] is an optional line address. If [addr] is specified, command X will be executed only on matching lines. [addr] can be a single line number, a regular expression, or a line range (see sed address). Extra [options] for some sed commands.

The following example deletes lines 30 through 35 from the input. 30735 is the address range. D is the delete command:

Sed '30meme 35dcentinput.txt > output.txt

The following example prints all input until it is found with "FOO'. If such a line is found, sed terminates in exit state 42. If no such line is found (and no other errors occur), sed exits state 0. / ^ foo/ is a regular expression address. Q is the exit command. 42 is the command option.

Sed'/ ^ foo / q42'input.txt > output.txt

A command script or script file within a can be separated by a semicolon (detached;) or a newline character (ASCII 10). You can specify multiple script-e or-F options.

The following examples are equivalent. They perform two sed operations: delete any line / ^ foo/, that matches the regular expression and replace all occurrences of the string "Hello" and "world":

Sed'/ ^ foo / d; s / hello / world / 'input.txt > output.txt sed-eBay / ^ foo / d'-e's / hello / world /' input.txt > output.txt echo'/ ^ foo / d'> script.sed echo's / hello / world /'> > script.sed sed-f script.sed input.txt > output.txt echo'/ hello / world /'> script2.sed sed-eBay / ^ foo / danceff script2.sed input.txt > output.txt

Because of their syntax, the commands arecoverc _ I cannot be followed by a command delimiter semicolon, so the end script or script file on which the newline is terminated or placed should be used. The command can also be preceded by optional non-white space characters. See multiple command syntax.

3.2 Summary of sed commands

GNU supports the following command sed. Some are standard POSIX commands, while others are GNU extensions. The details and examples of each command are as follows. (mnemonic) is displayed in parentheses.

A\ text

Append text after a line.

A text

Append text (alternative syntax) after a line.

B label

Assign labels unconditionally. The tag can be omitted, in which case, at the beginning of the next cycle.

C\ text

Replace (change) lines with text.

C text

Replace (change) lines (alternative syntax) with text.

D

Delete the pattern space; start the next loop immediately.

D

If the pattern space contains a newline character, the text in the mode space up to the first newline character is deleted and the loop and result mode space is restarted without reading the new input line.

If the pattern space does not contain a newline character, a normal new loop is started like a d command.

E

Execute the commands found in the pattern space and replace the pattern space with output; trailing newlines are suppressed.

E command

Executes the command and sends its output to the output stream. This command can be run across multiple lines, with a backslash in addition to the last line.

F

(filename) prints the file name of the current input file (with a trailing newline character).

G

Replace the contents of the pattern space with the contents that hold the space.

G

Append a newline character to the contents of the pattern space, and then append the contents of the reserved space to the contents of the pattern space.

H

(keep) replace the contents of the holding space with the contents of the pattern space.

H

Append a newline character to the contents of the reserved space, and then append the contents of the pattern space to the contents of the reserved space.

I\ text

Insert text before a line.

I text

Insert text (alternative syntax) before a line.

L

Print the pattern space in an explicit form.

N

(next) if automatic printing is not disabled, print the pattern space, and then replace the pattern space with the next line of input anyway. If there is no more input, sed exits without processing any commands.

N

Add a newline character to the pattern space, and then append the next line of input to the pattern space. If there is no more input, sed exits without processing any commands.

P

Print the pattern space.

P

Print the pattern space until the first one.

Q [exit-code]

(exit) exit sed without processing any commands or input.

Q [exit-code]

(exit) this command Q is the same as the contents of the drawing space, but does not print. Like Q, it provides the ability to return an exit code to the caller.

R filename

Read a text file, a file. Example:

R filename

At the end of the current cycle or when the next input line is read, a line of file names is queued for reading and inserted into the output stream.

S/regexp/replacement/ [flags]

(replace) matches the regular expression to the contents of the pattern space. If found, replace the matching string replacement.

T label

Branch to tag, which can be marked only if s has been successfully executed since the last input line was read or the conditional branch. The tag can be omitted, in which case, at the beginning of the next cycle.

T label

(test) label s only if the ubstitut branch has not been successful since the last input line was read or the conditional branch. The tag can be omitted, in which case, at the beginning of the next cycle.

V [version]

(version) this command does nothing, but sed fails if it does not support GNU sed extensions, or if the requested version is not available.

W filename

Writes the pattern space to the file name.

W filename

Writes the given file name to the part of the pattern space of the first newline character

X

Swap the contents of reserved and mode spaces.

Y/src/dst/

Transliterates any character in the pattern space that matches any source character with the corresponding character in dest-chars.

Z

(zap) this command clears the contents of the pattern space.

#

One comment until the next newline character.

{cmd; cmd...}

Combine a few commands.

=

Prints the current input line number (with a trailing newline).

: label

The label of the specified location is the branch command (b, t _ T).

3.3 s command

The s command (such as an alternative) is probably the most important sed, and there are many different options. The syntax of the s command is's / regexp / replacement / flags ".

Its basic concept is simple: the s command tries to match the pattern space with the provided regular expression regexp; if the match succeeds, that part of the matching pattern space is replaced.

For more information about regexp syntax, see regular expression addresses.

The replacement may include (? Is a reference from 1 to 9, including endpoints), which refers to the part contained in the match between? And their matches. In addition, the substitution can contain unescaped characters that reference the entire matching portion of the pattern space. \ n\ (\) &

The / character can be evenly replaced by the s command by any other single character within any given one. The / character (or any other character used instead of it) can be used when a regular expression appears or is replaced only if it is preceded by the character\.

Finally, as an GNU sed extension, it can include a special sequence of a backslash and a letter, LMagne, or E. Its meaning is as follows:

\ L

Will be replaced with lowercase until\ U or\ E is found

\ l

Convert the next character to lowercase

\ U

Will be replaced with uppercase until\ L or\ E is found

\ u

Convert the next character to uppercase

\ e

Stop the case conversion by\ L or start\ U.

When the g flag is used, the situation transformation does not propagate from one occurrence of the normal expression to another. For example, AB-' is in pattern space when executing the following command:

S /\ (B\? )-/ X\ U\ 1 / g

The output is' axxB'. When replacing the first'-', the'\ ü 'sequence affects only'\ 1'. It does not affect the x replacement when being added to the pattern space character b-with xB.

On the other hand,\ l and\ u do affect the rest of the replacement text if they are followed by an empty replacement. With 'AB-' in mode space, the following command:

S /\ (B\? )-/\ U\ 1x / g

Will replace "- 'and' X' (uppercase) and 'BMY' and'BX". If this behavior is undesirable, you can add a ".\ E' sequence'\ 1' in this case.

To include the text\, & or finally replace the newline character, be sure to be earlier than the expected\, & or newline replacement.

The s command can be followed by zero or more of the following flags:

G

Apply substitution to all matches to the regular expression, not just the first one.

Number

Only matching regular expressions can be replaced for several times.

S Command interaction Note: the POSIX standard does not specify what should happen when you mix g and numeric modifiers, and there is currently no widely agreed meaning in sed implementations. For GNU sed, interaction is defined as ignoring matches before numeric th, and then matching and replacing all matching digits.

P

If a replacement is made, the new pattern space is printed.

Note: when options p and e are specified, the relative ordering of the two produces very different results. Generally speaking, ep (evaluate and print) is what you want, but on the other hand, the operation may be useful for debugging. Therefore, the current version of GNU sed specifically explains the existence of the pre-and post-p option, e prints the mode space before and after the evaluation, while in general, the flag of the s command is displayed only once. However, this behavior may change in future versions.

W filename

If a replacement is made, the result is written to a named file. As a GNU sed extension, it supports two special file name values: / dev / standard error, write the result to standard error, and / dev / standard output, write to standard output. three

E

This command allows you to convert input pipes to pattern space from the shell command. If a replacement is made, the command found in the pattern space is executed and its space is replaced with its output. Trailing newlines are suppressed; if the command to be executed contains NUL characters, the result is not defined. This is an GNU sed extension.

I i

The modifier for I regular expression matching is an GNU extension that makes sed match regular expressions in a case-insensitive manner.

M m

The modifier for M regular expression matching is the GNU sed extension, which instructs GNU sed to match regular expressions in multiline mode. The modifier causes ^ and $to match the empty string after the newline character (except for normal behavior) and the empty string before the newline character. There are some special character sequences (\ 'and\') that always match the beginning or end of the buffer. In addition, in multiline mode, the period character does not match the new line character.

3.4 Common commands

If you are fully using sed, you probably want to know about these commands.

#

Comments at the beginning of the # character; comments will continue to the next newline character.

If you are concerned about portability, note that some implementations of sed (which are not POSIX compliant) may only support a single-line comment, and then support # only if the first character of the script is a.

Warning: if the first two characters of the sed script are # n, the-n (no-autoprint) option is forced. If you want to comment on the first line of the script, the comment uses the letter "?" You don't want this behavior, so be sure to use a capital "? or in'?"

Q [exit-code]

Exit sed without processing any commands or input.

Example: stop after printing the second line:

$seq 3 | sed 2Q 1 2

This command accepts only one address. Note that if automatic printing is not disabled, print the current pattern space-n option. The ability to return an exit code from a sed script is a GNU sed extension.

See also the GNU sed extension Q command to silently exit without printing the current mode space.

D

Delete the pattern space; start the next loop immediately.

Example: delete the second input line:

$seq 3 | sed 2d 1 3p

Print pattern space (to standard output). This command usually uses the-n command line option only with this command.

Example: print only the second input line:

$seq 3 | sed-n 2p 2 n

If automatic printing is not disabled, print the pattern space, and then, anyway, enter the replacement pattern space using the next line. If there is no more input, sed exits without processing any commands.

This command is useful for skipping lines (for example, per Nth line).

Example: perform a substitution every 3 lines (that is, two n commands skip two lines):

$seq 6 | sed'n; n; s /. / x /'1 2 x 4 5 x

The syntax of the extended address provided by GNU sed is first? Steps to achieve the same result:

$seq 6 | sed'0?3s /. / x /'1 2 x 4 5 x {commands}

A set of commands can be enclosed between {and} characters. This is especially useful when you want to trigger a set of commands through a single address (or address range) match.

Example: perform a replacement and then print the second input line:

$seq 3 | sed-nasty 2 {s / 2 / X /; p}'X 3.5 less used commands

Although it may be less used than in the previous section, sed can use these commands to build some very small but useful scripts.

Y/source-chars/dest-chars/

Transliterates any character in the pattern space that matches any source character with the corresponding character in dest-chars.

Example: transliteration of 'AJ' into' 0-9 ":

$echo Hello World | sed'y / abcdefghij / 0123456789 / '74llo worl3

(/ characters in any given y command can be evenly replaced by any other single character. )

The instance / (or any other character used instead of it),\ or newline character can appear in the source-character or DEST- character list, providing each instance by escaping\. Source-the list of characters and DEST- characters must contain the same number of characters (after escaping).

For similar features in tr, see the commands in GNU coreutils.

A text

After the addition of lines of text. This is the GNU extension a for standard commands-see below for more information.

Example: after adding the second line of "Hello":

$seq 3 | SED'2A Hello'1 2 Hello 3

The white space after the a command is ignored. The text to be added is read until the end of the line.

A\ text

After the addition of lines of text.

Example: the second line (- | indicates the printout line) after adding "Hello":

$seq 3 | sed'2a\ hello'-| 1-| 2-| hello-| 3

The a command will be queued according to this command (each but the last end and\ deleted from the output) to queue at the end of the current cycle or when the next input line is read.

As an GNU extension, this command accepts two addresses.

Handle escape sequences in text, so you should print a single backslash in the text.

The command is restored after the last line without a backslash (\)-'world' in the following example:

$seq 3 | sed'2a\ hello\ world 3s /. / X /'- | 1-| 2-| hello-| world-| X

As an GNU extension, the a command and text can be split into two-e arguments, making the script easier:

$seq 3 | sed-eBay 2a\'- e hello 1 2 hello 3$ sed-eBay 2a\'- e "$VAR" I text

Insert text before a line. This is the GNU extension I of the standard command-see below for more information.

Example: insert "Hello" before the second line:

$seq 3 | SED'2I Hello'1 Hello 2 3

The white space after the I command is ignored. The text to be added is read until the end of the line.

I\ text

Immediately output the lines of text that follow this command.

Example: insert "Hello" before the second line (- | indicates the printout line):

$seq 3 | sed'2i\ hello'-| 1-| hello-| 2-| 3

As an GNU extension, this command accepts two addresses.

Handle escape sequences in text, so you should print a single backslash in the text.

The command is restored after the last line without a backslash (\)-'world' in the following example:

$seq 3 | sed'2i\ hello\ world s /. / X /'- | X-| hello-| world-| X-| X

As an GNU extension, the I command and text can be split into two-e arguments, making the script easier:

$seq 3 | sed-eBay 2i\'- e hello 1 hello 2 3$ sed-eBay 2i\'- e "$VAR" c text

Replace lines with words. This is the GNU extension c for standard commands-see below for more information.

Example: replace lines 2 through 9 with "Hello":

$seq 10 | SED '2jue 9c Hello' 1 only feed 10

The white space after the C command is ignored. The text to be added is read until the end of the line.

C\ text

Delete the lines that match the address or address range, and output the lines of text following the command.

Example: replace "Hello" and "World" (- | for printout lines) with lines 2 through 4:

$seq 5 | sed'2,4c\ hello\ world'-| 1-| hello-| world-| 5

If no address is given, replace each line.

When this command is complete, a new loop will be started because the pattern space will be deleted. In the following example, c starts a new loop, and the replacement text does not execute the replace command:

$seq 3 | sed'2c\ hello s /. / X /'- | X-| hello-| X

As an GNU extension, the c command and text can be split into two-e arguments, making the script easier:

$seq 3 | sed-eBay 2c\'- e hello 1 hello 3$ sed-eBay 2c\'- e "$VAR" =

Prints the current input line number (with a trailing newline).

$printf'%s\ n'aaa bbb ccc | sed = 1 aaa 2 bbb 3 ccc

As an GNU extension, this command accepts two addresses.

L n

Print the pattern space in explicit form: non-printable characters (and\ characters) are printed in C-style escape format; long lines are divided, followed by\ characters to indicate division; each line is marked with a $at the end.

N specifies the desired newline length; a length of 0 (zero) means never winding a long line. If omitted, the default value specified on the command line is used. What is described? The parameter is an GNU sed extension.

R filename

Read a text file, a file. Example:

$seq 3 | / etc of SED'2R / hostname'1 2 fencepost.gnu.org 3

At the end of the current cycle or when the next input line is read, the contents of the file name to be read and inserted into the output stream are queued. Note that if the file name cannot be read, it is treated as an empty file without any error indication.

As an GNU sed extension, the / dev / standard input of special values supports the file name, which reads the contents of the standard input.

As an GNU extension, this command accepts two addresses. The file is then re-read and inserted into each addressing line.

W filename

Writes the pattern space to the file name. As a GNU sed extension, it supports two special file name values: / dev / standard error, write the result to standard error, and / dev / standard output, write to standard output. four

The file is created (or truncated) before the first input line is read; all w commands that reference the same file name (including instances of flags on the w success s command) are output without closing and reopening the file.

D

If the pattern space does not contain a newline character, a normal new loop is started like a d command. Otherwise, delete the text in the graphics space until the first newline character, and restart the loop and the resulting mode space without reading the new input line.

N

Add a newline character to the pattern space, and then append the next line of input to the pattern space. If there is no more input, sed exits without processing any commands.

When-z is used, zero bytes (ascii'NUL' characters) are added between lines (not new lines).

Sed does not terminate by default if it does not have a "next" input line. This is an GNU extension that can be disabled-- posix. See the N command on the last line.

P

Prints part of the pattern space to the first newline character.

H

Replace the contents of the reserved space with the contents of the pattern space.

H

Append a newline character to the contents of the reserved space, and then append the contents of the pattern space to the contents of the reserved space.

G

Replace the contents of the pattern space with the contents that hold the space.

G

Append a newline character to the contents of the pattern space, and then append the contents of the reserved space to the contents of the pattern space.

X

Swap the contents of reserved and mode spaces.

3.6 Master sed commands

In most cases, using these commands indicates that you may prefer programming like awk Perl. But occasionally someone insists on sed, and these commands allow one to write a very complex script.

: label

[no address]

Specifies the label location of the branch command. In all other respects, there is no operation.

B label

Unconditionally branch to the tag. The tag can be omitted, in which case, at the beginning of the next cycle.

T label

The s can be marked only if the ubstitution has been successfully executed since the last input line was read or the conditional branch. The tag can be omitted, in which case, at the beginning of the next cycle.

3.7Command sed specific to GNU

These commands are GNU-specific sed, so you must use them carefully, only if you are sure that blocking portability is not bad. They allow you to check for GNU sed extensions or tasks that you often need to perform, but do not support standard sed.

E [command]

This command allows you to convert input pipes to pattern space from the shell command. Without arguments, the e command executes the command found in the pattern space and replaces the pattern space with the output; the trailing newline is suppressed.

If a parameter is specified, the e command interprets it as a command and sends its output to the output stream. This command can be run across multiple lines, with a backslash in addition to the last line.

In both cases, if the command to be executed contains NUL characters, the result is undefined.

Note that, unlike the r command, the output of the command is printed immediately; the r command delays the output until the end of the current cycle.

F

Prints the file name of the current input file (with a trailing newline character).

Q [exit-code]

This command accepts only one address.

This command Q is the same as the content of the drawing space, but does not print. Like Q, it provides the ability to return an exit code to the caller.

This command can be useful because the only alternative to this obvious trivial function is to use the-n option, which may unnecessarily complicate your script, or to use the following code snippet, which wastes time by reading the entire file without any obvious effect:

Eat $d and quietly exit the N-read line on the last line, silently g overwrite the mode space every time to save memory b eat R filename

At the end of the current cycle or when the next input line is read, a line of file names is queued for reading and inserted into the output stream. Note that if the file name cannot be read, or if the file name cannot be reached, no lines are appended and there is no error indication.

Like the r command, / dev / standard input with special value is supported for reading a file name of a line from standard input.

T label

You can mark s only if there has been no successful ubstitut since the last input line was read or a conditional branch. The tag can be omitted, in which case, at the beginning of the next cycle.

V version

This command does nothing, but sed fails if it does not support the GNU sed extension because other versions of sed are not implemented. Alternatively, you can specify the version required for the sed script, for example, 4.0.5. The default is 4.0 because this is the first version of this command to be implemented.

This command enables all GNU extension POSIXLY_CORRECT even if it is set in the environment.

W filename

Writes the given file name to the part of the pattern space of the first newline character. W everything under the command for file processing is here.

Z

This command clears the contents of the pattern space. It usually corresponds to "trumpet /. * / /', but it is more efficient and works when there is an invalid multibyte sequence in the input stream. POSIX requires such sequences to mismatch". So sed has no portable way to clear the buffer in the middle of the script in most multibyte locales, including the UTF-8 locale

3.8 more than one command syntax

There are several ways to specify multiple commands in a sed program.

When running sed scripts from a file, it is most natural to use newline characters (using the-F option).

On the command line, all sed commands may be separated by line breaks. Alternatively, you can specify each command as the parameter-e option:

$seq 6 | SED'1D 3D 5D' 24 6 $6 sequence | sed-e 1D-e 3D-e 5d 2 46

The semicolon (';') can be used to separate the simplest commands:

$seq 6 | SED'1D; 3D; figure 5d' 2 4 6

Commands can be separated by semicolons (this is a non-portable GNU sed extension).

$seq 4 | sed' {1d; 3D}'24$ seq 6 | SED'{1D; 3D}; figure 5d' 24 6

In the use of the label brecedence tjort: the instruction is read until a semicolon is used. The gaps between leading and trailing are ignored. In the following example, the label is "X". The first example uses sed with GNU. The second is the portable equivalent. For more information about branches and labels, see branching and flow Control.

$seq 3 | sed'/ 1 / bx; s / ^ / = /;: X; 3D "1 = 2$ SEQ 3 | SED-e'/ 1 / BX'-e's / ^ / = /'- e': X'-e '3D' 1 = 23.8.1 commands requiring newline characters

The following commands cannot be separated by semicolons and require a newline character:

Arecrum cpeni (append / change / insert)

All the following characters are used as appended / changed / inserted text. Using a semicolon can lead to bad results:

$seq 2 | sed'1aHello; 2D "1 Hello; 2d 2

Use the command to separate-e or newline:

$seq 2 | sed-e 1aHello-e 2d 1 Hello $seq 2 | SED '1aHello 2D' 1 Hello

Please note that after specifying the text to be added ('Hello'), aPercenti is itself a GNU sed extension. The POSIX standard-compliant portable solution is:

$seq 2 | SED'1A\ Hello 2D' 1 Hello # (comment)

All characters "#" until the next newline character is ignored.

$seq 3 | sed'# this is a comment; 2D "1 2 3$ SEQ 3 | SED'# this is a comment 2D' 1 3 r Magi R Magi W (read and write files)

To parse the file name until the end of the line with the r.r _ W command. If spaces, comments, or semicolons are found, they will be included in the file name, resulting in unexpected results:

$seq 2 | sed'1w hello.txt; 2D "1 x 2$ LS-log Total 4-rw-RW-R-- 1 2 January 2 at 23: 03 hello.txt; 2d $cat'hello.txt; 2D" 1

Please note that sed is automatically ignoring read / write errors (such as file loss). In the following example, sed attempts to read the name "hello.txt;?". The file is missing and the error is ignored:

$echo x | sed'1rhello.txt; N "X e (command execution)

Any characters after the e command are sent to shell until the end of the line. If spaces, comments, or semicolons are found, they will be included in the shell command, resulting in unexpected results:

$echo a | SED'1E Touch Rich # Bar'a $LS-1 Rich # Bar $Echo | sed'1e touch foo; s / a / b / 'sh:1:s / a / b /: cannot find a we / [we] (use e or instead of w)

In the replacement command, the w flag writes the replacement result to the file, and the e flag executes the attribution result with the shell command. Like the r/R/w/W/e command, these must be terminated with a newline character. If spaces, comments, or semicolons are found, they will be included in the shell command or file name, resulting in unexpected results:

$echo a | SED's / A / B / w1.txt# rich'b $LS-1 1.txt#FOO 4 address: select a line? Address Overview: address Overview? Numeric address: select rows by number? Regular expression address: select lines by text matching? Range address: select a series of lines 4.1 address overview

The address determines which line of the command sed will execute. The following command replaces "Hello" and "World" only on line 144:

Sed'144s / hello / world / 'input.txt > output.txt

If no address is given, the command is executed on all lines. The following command replaces all lines of "Hello" and "World" in the input file:

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

Addresses can contain regular expressions to match lines based on content rather than line numbers. The following command replaces "Hello" and "World" only if it contains "Apple":

Sed'/ apple / s / hello / world / 'input.txt > output.txt

The address range is specified by two addresses separated by commas (,). The address can be a number, a regular expression, or a mixture of both. The following command replaces "Hello" and "World" with only 4 to 17 lines (inclusive):

Sed'4,17s / hello / world / 'input.txt > output.txt

Will! The character is appended to the end of the address specification (before the command letter) denying the sense of matching. That is to say, if! If the characters follow the address or address range, only lines that do not match the address are selected. The following command replaces "Hello" and "World" only without the word "Apple":

Sed'/ apple /! S / hello / world / 'input.txt > output.txt

The following command replaces "Hello" and "World" only on lines 1 through 3 and 18 up to the last line of the input file (that is, excluding lines 4 to 17):

Sed'4,17! S / hello / world / 'input.txt > output.txt 4.2 Select rows by number

The address in the sed script can be in any of the following forms:

Number

The specified line number matches only the lines in the input. Note that sed will not count continuously between all input files unless the-s option is specified. )

$

The address matches the last line of the last input file, or the last line of each file-lifetime or-s option is specified.

First~step

This GNU extension is the first line at the beginning of each matching step. In particular, when there is a non-negative n such that the current number of rows is equal to the first + (n * step), the row is selected. Therefore, 1x 2 can choose odd rows and 0x 2 even rows; pick the third row from the second, 2 through 3 "will be used; from the 10th, select the fifth row and use'10'5"; and'50'0 "is just a vague term 50.

The following command demonstrates the address usage of the step:

$seq 10 | SED-N '0room4P' 4 8 $10 sequence | SED-N' 1room3P' 1 4 7 10 4.3 Select lines by text matching

GNU sed supports the following regular expression addresses. The default regular expression is the basic regular expression (BRE). If-E or-r uses the option, the regular expression should be an extended regular expression (ERE) syntax. See BRE vs ERE.

/ regexp/

This selects any line that matches the regular expression. If the regexp itself contains any / characters, each must be escaped by a backslash (\).

The following command prints the line / etc / passwd file to "Celebration'5:

Sed-nailed / bash $/ paired / etc / passwd

Empty regular expression "/ / 'repeats the previous regular expression match (the same if an empty regular expression is passed to the s command). Note that regular expression modifiers are evaluated when regular expressions are compiled, so specifying them together with empty regular expressions is invalid.

\% regexp%

(% can be replaced by any other single character. )

This also matches the regular expression, but allows a different delimiter /. This is especially useful if the regular expression itself contains a large number of slashes because it avoids every tedious escape /. If regexp itself contains any delimiter characters, each must be escaped by a backslash (\).

The following two commands are equivalent. They print as'/ Family / Alice / document / ":

Sed-home / ^\ / home\ / alice\ / documents\ / / p'sed-n'\% ^ / home / alice / documents /% p'sed-n'\; ^ / home / alice / documents /; p "/ regexp/I\% regexp%I

The modifier for I regular expression matching is the GNU extension, which makes regular expressions match in a case-insensitive manner.

In many other programming languages, the lowercase letter I is used for case-insensitive regular expression matching. However, in sed this I is used to insert the command (TODO: add pxref).

Observe the differences between the following examples.

In this example, / bbind I is the address: a regular expression with an I modifier. D is the delete command:

$printf "% s\ n" abc | SED'/ B / ID'?

Here / b / is the address: regular expression. I is the insert command. D is the value to insert. A line and'?' Then insert above the matching line:

$printf "% s\ n" abc | SED'/ B / ID'? B? / regexp/M\% regexp%M

The modifier for M regular expression matching is the GNU sed extension, which instructs GNU sed to match regular expressions in multiline mode. The modifier causes ^ and $to match the empty string after the newline character (except for normal behavior) and the empty string before the newline character. There are some special character sequences (\ 'and\') that always match the beginning or end of the buffer. In addition, in multiline mode, the period character does not match the new line character.

4.4 range address

You can specify an address range by specifying two addresses separated by commas (,). Address range matching starts with the line where the first address matches until the second address matches (including):

$seq 10 | SED-n '4jue 6p' 4 5 6

If the second address is a regular expression, then check that the end of the game will start with the line below the line, which matches the first address: a range will always span at least two lines (except, of course, if the input stream ends).

$seq 10 | SED-n'4 / [0-9] / P' 4 5

If the second address is a row with a number less than (or equal to) matching the first address, then only one line matches:

$seq 10 | SED-n '4jue 1p' 4

GNU sed also supports some special dual-address formats; all of these are GNU extensions:

0,/regexp/

0 can use line numbers in the address specification to try to match the regular expression on the first input line. In other words, similar to, except that if the addr2 matches the first line of the input, the form will assume its end range, and the form will match the beginning of its range, thus making the range span up to the second occurrence of the regular expression. 0,/regexp/sed0,/regexp/1,/regexp/0,/regexp/1,/regexp/

Note that this is the only place where the 0 address makes sense; no line 0 and command 0 giving the address in any other way will give an error.

The following example shows the difference between addresses 1 and 0:

$seq 10 | SED-n'1 addr1,+N / [0-9] / P'12$ SEQ 10 | SED-n'0 addr1,+N / [0-9] / P'1 addr1,+N

Match ADDR1 and? To downlink ADDR1.

$seq 10 | SED-N'6J + 2P' 6 7 8

Addr1 can be a line number or a regular expression.

Addr1,~N

Matches the line after addr1 and addr1 until the next line is entered in a multiple of N. The following command starts at line 6 until the next line is a multiple of 4 (that is, line 8):

$seq 10 | SED-n '6jurisdiction 4P' 6 7 8

Addr1 can be a line number or a regular expression.

5 regular expressions: select text? Overview of regular expressions: Overview of regular expressions sed? BRE vs ERE: BRE and ERE regular expression syntax? BRE syntax: an overview of basic regular expression syntax? ERE syntax: an overview of extended regular expression syntax? Character classes and parenthesis expressions:? Regular expression extension: attach regular expression commands? Backreferences and subexpressions: backreferences and subexpressions? Escape: specify special characters? Regional considerations: 5.1 regular expressions Overview sed

To know how to use sed, people should know regular expressions (short for regular expressions). Regular expressions are patterns that match topic strings from left to right. Most characters are normal: they represent their own patterns and match the corresponding characters. The regular expression is specified between the two slashes of the sed.

The following command prints out the word "Hello":

Sed-nasty / hello / p'

The above example is equivalent to the grep command:

Hello, grep'.

The power of regular expressions comes from the ability to include substitution and repetition in patterns. These are encoded in the pattern using special characters that do not represent themselves, but are interpreted in some special way.

The characters (caret) in the ^ regular expression match the beginning of the line. Characters. (dot) matches any single character. The following sed command matches and prints with the letter "baked, followed by any character, followed by the letter'?":

$printf "% s\ n" abode bad bed bit bid byte body | sed-n bidding / ^ bd / p' Bad bed bidding Agency

The following sections describe the meaning and usage of special characters in regular expressions.

5.2 basic (BRE) and extended (ERE) regular expressions

Basic and extended regular expressions are two variants of the specified pattern syntax. The basic regular expression (BRE) is the default value of grep in sed (and similar). Extended regular expression syntax (ERE) is activated with the-r or-E option (similarly, grep-E).

In GNU, the only difference between basic and extended regular expressions is the behavior of several special characters:?'' +', braces, braces ('{}'), and'|'.

Using basic (BRE) syntax, these characters have no special meaning except for the prefix backslash ('\ "); although using extended (ERE) syntax is the opposite: these characters are special unless they have a backslash ('\").

Required pattern basic (BRE) Grammar extension (ERE) Syntax text'+'(plus sign) $echo "a + b = c" | sed-n characters / a + b / pendant a + b = c $echo "a + b = c" | sed-E-n characters / a\ + b / pruna + b = c one or more 'one' characters followed by'b'(plus sign as a special metacharacter) $echo "aab" | sed-n' / a\ + b / p'aab $echo "aab" | sed-E-nouns / a + b / p'aab 5.3 basic regular expression syntax overview

The following is a brief description of regular expression syntax, sed.

Char

An ordinary character matches itself.

*

Matches the sequence of zero or more matching instances of the previous regular expression, which must be a normal character with a prefix of\ a., grouped regular expression (see below) or special character of parenthesis expression. As an GNU extension, the suffix regular expression can also be followed by *; for example, a regular expression is equivalent to a *. POSIX 1003.1-2001 indicates that * it represents itself when it appears at the beginning of a regular expression or subexpression, but many non-GNU implementations do not support this, and portable scripts should be used in these contexts.

.

Matches any character, including newline characters.

^

Matches the empty string at the beginning of the pattern space, that is, what must be displayed after the pattern space begins.

In most scripts, the pattern space is initialized to the contents of each line (see how sed works). So, this is a useful simplification, thinking that ^ # include only matches lines, and # including'is the first thing on the line-if there was a space before, such as a match failure. This simplification is valid as long as the original content of the schema space is not modified, such as using the s command.

^ is used as a special character only at the beginning of a regular expression or subexpression (that is, after\ (or after\ |). However, portable scripts should be avoided at the beginning of subexpressions because POSIX allows ^ to treat them as normal characters in that context.

$

It is the same ^, but refers to the end of the pattern space. $can only be used as a special character at the end of a regular expression or subexpression (that is, before\) or before\ |), and the use at the end of a subexpression is not portable.

[list] [^ list]

Matches any single character in the list: for example, [aeiou] matches all vowels. The list can include sequences that match, including, any character between char1 and char2. See character classes and parenthetical expressions. Char1-char2

\ +

Because *, but match one or more. This is an GNU extension.

\?

As *, but only matches zero or one. This is an GNU extension.

\ {I\}

As *, but matches the I sequence exactly (I am a decimal integer; keep it between 0 and 255 for portability).

\ {iMagneur j\}

Match I and j, including sequences.

\ {I,\}

The match is greater than or equal to the I sequence.

\ (regexp\)

Combine internal regular expressions into a whole, which is used to:

Apply the postfix operator, such as\ (abcd\) *: this will search for zero or more entire sequence 'ABC codes, while abcd* will search for' ABC 'followed by zero or multiple occurrences. Please note that POSIX 1003.1-2001\ (abcd\) * requires support, but many non-GNU implementations do not support it, so it is not universally portable. Use back reference (see below). Regexp1\ | regexp2

Match regexp1 or regexp2. Use parentheses to use complex alternative regular expressions. The matching process tries each substitution from left to right, and uses the first successful one. This is an GNU extension.

Regexp1regexp2

Matches the connection between regexp1 and regexp2. Cascaded combinations are different from closer\ |, ^ and $, but not more closely than other regular expression operators.

\ digit

The matching number\ (… \) regular expression bracketed subexpression. This is called anti-reference. Subexpressions are implicitly numbered by counting the appearance from left to right.

\ n

Matches the newline character.

\ char

Matching charcoal, where coke is a $, *,., [,\, or ^. Note that you can portably assume that only C-like backslash sequences are\ n and\; in particular,\ t is not portable and matches'? "in most implementations of sed, rather than tab characters.

Note that the regular expression matcher is greedy, that is, try to match from left to right, and if you can match two or more times starting with the same character, select the longest match.

Example:

"ABCDEF"

Match 'ABCDEF'.

"A * B"

Matching zero or more 'one' is followed by a single "b". For example,'b'or 'AAAAAB ".

"one\ 2B"

Matches'b'or'AB'.

"A\ + B\ +"

Match one or more 'one followed by one or more' b's:'AB'is the shortest match, but the other example is "AAAAB' or 'abbbbb' or' aaaaaabbbbbbb".

"*. ".\ +"

Both match all characters in the string; however, the first matches each string (including an empty string), while the second matches a string that contains at least one character.

"^ is the main. * (. *)"

This matches a 'primary', followed by an opening and a closing parenthesis. "?'('and')" need not be adjacent.

"^ #"

This matches a'#'.

"\ $"

This matches a string that ends with a single backslash. The regular expression contains two backslashes for escape.

"\ $"

Instead, it matches a string consisting of a dollar sign because it is escaped.

"[A-ZA-Z0-9]"

In the C language environment, it matches any ASCII letter or number.

"[^ tab]\ +"

(here tab represents a single tab. This matches a string of one or more characters, none of which is a space or tab Usually this means one word.

"^\ (. *\)\ n\ $1"

This matches a string consisting of two equal substrings separated by newline characters.

"\ {9\} $"

This matches nine characters, followed by a 'one' at the end of the line.

"^ {15\} A"

This matches the beginning of a 16-character string, and the last one is' one'.

5.4 Overview of extended regular expression syntax

The only difference between basic and extended regular expressions is the behavior of a few characters:? +', parentheses, curly braces ('{}'), and'| ". Although basic regular expressions require them to be escaped as special characters, if you want them to match literal characters, you must escape them when using extended regular expressions." | because'\ |'is an GNU extension-the standard basic regular expression does not provide its function.

Example:

Abc?

Become 'ABC\?' When using extended regular expressions. It matches the text string 'ABC?'.

C\ +

When using extended regular expressions to become'C +'. It matches one or more'C's.

A\ {3,\}

When an extended regular expression is used as a {3,}. It matches three or more 'one'.

\ (abc\)\ {2pr 3\}

When using extended regular expressions as'(ABC) {2pr 3}'. It matches "ABCABC' or 'ABCABCABCABC...".

\ (abc*\)\ 1

When using extended regular expressions as'(ABC *)\ 1'. When using extended regular expressions, you also need to escape the reference reference.

A\ | b

When using extended regular expressions as'A | B'. It matches'a'or'b ".

5.5 character classes and parenthesis expressions

A parenthesis expression is surrounded by a list of characters'['and']'. It matches any single character in the list; if the first character of the list is the caret'^', it matches any character that is not in the list. For example, the following command replaces "gray" or "gray" and "blue":

/ gr [ae] y / blue /'of sed

Parenthetical expressions can be used for both basic and extended regular expressions (that is, with or without the-Epicurer option).

In a parenthesis expression, a range expression consists of two characters separated by a hyphen. It matches any single character sorted between two characters, including. In the default C environment, the sort order is the local character order; for example,'[advertisement]'is equivalent to'[A B C D]'.

Finally, the characters of some named classes are predefined in parenthetical expressions, as shown below.

These named classes must be used in parentheses. Use it correctly:

$echo 1 | sed's / [: digit:] / X /'X

Incorrect use was rejected by newer versions of sed. The older version accepts it but treats it as a single parenthetical expression (equivalent to "[DGIT:]', that is, only the characters d / g / I / t /:):

# current GNU sed version-incorrect use rejected $echo 1 | sed / [: digit:] / X / 'sed: the syntax of the character class is [: space:], not [: space:] # the old GNU sed version $echo 1 | sed / s / [: digit:] / X /' 1 "[: alnum:]"

Alphanumeric characters:'[: α:] 'and' [: numeric:]'; inside the'C' locale and ASCII character encoding, which is the same as'[0-9A-ZA-Z].

"[: α:]"

Alphabetic characters:'[: lower:] 'and' [: above:]'; inside the'C' locale and ASCII character encoding, which is similar to'[A-ZA-Z]'.

"[: blank:]"

White space characters: spaces and tabs.

"[: CNTRL:]"

Control characters in ASCII, these characters have octal codes 000 to 037 and 177 (DEL). In other character sets, these are the same characters, if any.

"[: number:]"

Number: 0 1 2 3 4 5 6 7 8 9.

"[: graphics:]"

Graphic characters:'[: alnum:] 'and' [: PUNCT:] ".

"[: lower:]"

Lowercase letters; inside the'C 'locale and ASCII character encoding, this is a b c d e f g h i j k l m n o p q r s t u v w x y z.

"[: print:]"

Printable characters:'[: alnum:]''[: PUNCT:] 'and space.

"[: PUNCT:]"

Punctuation marks are in the'C 'locale and ASCII character encoding, which is! "# $% &' () * +, -. /:

< = >

? @ [\] ^ _ `{|} ~

"[: space:]"

Spatial characters: in 'C'locale, this is the tag, newline character, vertical tag, form feed, carriage return and space.

"[: above:]"

Uppercase letters: in the'C 'locale and ASCII character encoding, this is A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.

"[: xdigit:]"

Hexadecimal number: 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f.

Note that the parentheses in these class names are part of the symbolic name and must be included in addition to the parentheses of the parenthesis expression.

Most metacharacters lose their special meaning in parenthesis expressions:

"]"

If it is not the first list item, end the parenthetical expression. So, if you want the'] 'character to be a list item, you have to put it first.

"-"

Represents a range if it is not the first or last in the list or the end of the range.

"^"

Represents a character that is not in the list. If you want the'^ 'character list item, put it anywhere, but first.

TODO: incorporate this paragraph (copied verbatim from the BRE section).

A special list of characters $, *,., [, and\ that are not usually included. For example, [*] matches "\'or'* 'because\ this is not special. However, strings such as [.ch.], [= a =] and [: space:] are special lists and symbols for contrast, equivalence classes, and character classes, respectively, and [so when a special inner list is listed, it is followed by., =, or:. In addition, in non-POSIXLY_CORRECT mode, special escape likes to confirm the list in\ n and\ t. Saw the escape.

"["

Represents an open finishing symbol.

"]"

Stands for close arrangement of symbols.

"[="

Represents an open equivalence class.

"=]"

Represents a close equivalence class.

[:]

Represents an open character class symbol, followed by a valid character class name.

":]"

Represents to turn off the character class symbol.

5.6 regular expression extension

The following sequence has a special meaning in regular expressions (used in addresses and s commands).

These can be used for basic and extended regular expressions (that is, with or without the-Epicure option).

\ w

Matches any word character. The word character is any letter or number or underscore character.

$echo "abc%-= def". | sed / / w / X / grub XXX%-= XXX. \ W

Matches any "non-word" character.

$echo "abc%-= def". | | sed /\ W / X / g'abcXXXXXdefX\ b |

Matches the word boundary; if the character on the left is a word character and the character on the right is a non-word character, and vice versa.

$echo "abc%-= def". | /\ b / X / sed XabcX%-= XdefX. \ B

Matching is full of word boundaries; if the character on the left and the character on the right can be either "word" or "non-word", it is a match.

$echo "abc%-= def". | | sed /\ w / X / g'aXbXc X%XX = X dXeXf.X\ s |

Matches space characters (spaces and tabs). Line breaks that embed the pattern / keep spaces will also match:

$echo "abc%-= def". | sed / s / X / grub abcX%-= Xdef. \ s

Matches non-space characters.

$echo "abc%-= def". | | sed /\ w / X / g'XXX XXX XXXX\ < |

Match the beginning of a word.

$echo "abc%-= def". | | sed's /\ |

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report