Sed command to edit text

Source: Internet
Author: User

1. Sed Overview

Sed is a non-interactive text editor that can edit text files and standard input. Standard input can be from keyboard input, file redirection, String, variable, or even pipe text.

2. Sed workflow Overview

When processing a file, sed reads data from a text line or standard input and is savedMode SpaceIn the temporary buffer, unless deleted or the output is canceled, all the processed rows will be printed on the screen after the SED command is executed. The mode space is cleared, and a new row is saved for processing.

3. Sed Buffer

Mode Buffer: the buffer of the mode space, which is processed by the SED command.

Buffer holding: swap or save with the number of buffer zones in the mode. The default value is null.

There are two types of commands between the mode buffer and the retention Buffer: replace and append (replace the command with lowercase letters and append the command with uppercase letters)

Replace: Mode buffer-> preserve buffer H (copy the content in the mode buffer to keep the buffer, replace the content in keep the buffer)

Buffer holding-> mode buffer g

Append: Mode buffer-> preserve buffer H (append the content of the mode buffer to the keep buffer)

Buffer holding-> mode buffer g

4. Sed call methods and options

  • Sed [Option] 'sed command 'input file (the command line calls SED)
  • Sed [Option] 'sed command '-F sed script file input file (SED command inserts the script file and then calls it Through SED command)
  • Regardless of the call method, if no input file is specified, sed accepts the input from the standard input.
Option Meaning
-N Disable auto-print mode Buffer
-E Parses the Next string into a sed edit command.
-F The SED script file is being called.


5. Sed command

The SED command is usually composed of two parts: the positioning text line and the SED edit command.

1) locate a text line: Use a row number to specify a row or a range of a row. Use a positive expression.

Option Meaning
X X indicates the specified row number.
X, Y Specifies the row number range from X to Y
/Pattern/ Query rows in the include Mode
/Pattern/ Query rows that contain two modes
/Pattern/, X From the matched row with pattern to the row x
X,/pattern/ The row from Line X to the matched row with pattern.
X, Y! Query rows that do not contain rows from X to Y
 

Sed metacharacters:

^

The start of the anchor row is as follows:/^ SED/matches all rows starting with sed.

$

The end of the anchor row is as follows:/SED $/matches all rows ending with sed.

.

Match a non-linefeed character, for example,/S. D/match s, followed by any character, and then D.

*

Match zero or multiple characters, such as:/* SED/match all templates with one or more spaces followed by sed rows.

[]

Matches a character in a specified range, such as/[ss] ED/matches SED and sed.

[^]

Match a character that is not within the specified range, such as:/[^ A-RT-Z] ED/match a line that is followed by a letter that does not contain a A-R and a T-Z.

\(..\)

Save matched characters, such as S/\ (love \) Able/\ 1RS, and loveable is replaced with lovers.

&

Save the search characters to replace other characters, such as S/love/** & **/. The love character is ** love **.

\ <

Anchor specifies the start of a word, for example,/\ <love/matches a row that contains a word starting with love.

\>

Anchor specifies the end of a word, for example,/love \>/matches a row containing a word ending with love.

X \ {M \}

Repeat the characters X and M, for example,/0 \ {5 \}/matches the rows containing 5 o.

X \ {M ,\}

Repeated characters X, at least m times, such as:/O \ {5, \}/matched rows with at least 5 o.

X \ {M, N \}

Repeated characters X, at least m times, no more than N times, such as:/O \ {5, 10 \}/rows matching 5-10 o

2) Sed edit command
Sed edit command table
Option Meaning
P Print matching rows
= Print the file line number
A \ Append text information after the row number is located
I \ Insert text information before locating the row number
D Delete row
C \ Use new text to replace the positioning text (line)
S Replace the corresponding mode with the replacement mode.
R Read text from another file
W Write text to a file
Y Character conversion (replace the corresponding character with a single character)
Q Exit after the first pattern match is complete.
{} The Command Group executed on the positioning line has similar functions as the-e option.
N Buffer line in output mode. Read the buffer line in replacement mode in the next line and execute the next command, not the first command.
H Copy the text in the mode buffer to keep the buffer
H The text in the mode buffer is appended to the persistence buffer.
G Copy the text in the buffer to the mode Buffer
G Append the text in the buffer to the mode buffer.
X Swap mode buffer and keep buffer content


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.