A About SED
Unlike a full-screen editor such as VI, SED is a non-interactive text editor that edits text files and standard input, which can come from keyboards, file redirects, strings, variables, and even text from pipelines. SED is the line-by-line processing of lines in text.
Sed simply edits a copy of the original file in the buffer and does not edit the source file. How to save the changed content, you need to redirect the output to another file, you can use the following format command:
sed ' sed command ' input-file > Result-file
Two Two ways to invoke SED:
1. In the shell command line, enter the command to invoke SED, in the form:
sed [option] ' sed command ' input file
Example: Sed-n ' 1,5p '/etc/inittab
Note that the SED command needs to be enclosed in single quotation marks.
2../sed script file Input file
Three Options commonly used in SED
-N: Indicates that all rows are not printed to standard output
-E: To resolve the next string to the sed edit command, omit if only one edit command is passed
-R: Indicates that you can use extended regular expression meta-characters
-I: Modify the original file directly
Four Text Positioning method
NULL is global: the non-locating line number acts as the full-text
x:x to specify exact line numbers
X, Y: Specifies the range of line numbers from X. to Y
/pattern/: Querying for rows that contain patterns
/pattern/ pattern/: Query for rows with two patterns
/pattern/,x: line from line to pattern matching line to x number of rows
x,/pattern/: Rows from the X line to the line matching the pattern
x,y!: Query does not include rows that range between x and Y rows
! cannot be used for keyword matching and cannot represent rows that do not match/pattern/
$: Indicates the last line
Example: Sed-n '/\./p ' /etc/inittab match. Symbol
Sed-n ' $p ' /etc/inittab print the last line
Sed-n ' 2,10!p ' /etc/inittab print lines that are not in lines 2 to 10
Five Basic editing commands
i \: Inserting text in front of the matching line
A \: Insert text after matching line
C \: Replace matched lines of text with new text (whole line substitution)
D: Delete the specified line
W: Write to a new file
R: Reading text from text
Y: Character transformation, converting a series of characters to corresponding characters, processing characters individually
Sed ' y/12345/678910/' input (transformed character sequence/transformed character sequence/)
The sequence of characters that require transformation and the character sequences of the transforms are equal, otherwise the error
L: Display control characters (nonprinting characters)
Q: Exit immediately after completing the match for the specified address
S: Replace the matched line of text with the new text, and S can replace the string compared to C \, while C \ can only replace the entire row
s/replaced by string/new string/[replace option]
S/pattern/string/g
The substituted string can be used as a regular expression, and the new string cannot be
such as: s/m.*l/lx/g (/symbol can be changed to @,#,% and other symbols, s#m.*l#lx#g)
You can also specify a keyword to replace the first few matches, just add the corresponding number to the replacement option, 1-512
such as: Sed-n ' s/mxl/lx/2p ' (replace the 2nd time match to the MXL character is LX)
{}: Specifies the group of commands that can be executed on the anchor line, similar to option-E
Example: Sed-n '/mxl/p '-e '/mxl/= '/etc/inittab is equivalent to
Sed-n '/mxl/{p;=} '/etc/inittab
Sed-n '/mxl/{s/m/m/g;s/x/x/g;} '/input
This article is from the "Cold Moon Funeral Jade Soul" blog, please be sure to keep this source http://mxlmgl.blog.51cto.com/9834691/1753969
Linux operation and maintenance text processing the Three Musketeers ' sed command