sed : Streameditor, stream editor, editing one or more files in an action unit; each SED processes a line of content in a given file.
When the SED processes the text, it stores the current row being processed in a temporary buffer, called the "pattern Space", matches the given pattern with the current line, and if it matches, uses command to edit the processing, and if it does not, the default output to standard output; Until the end of the file.
By default, all processing behavior of SED does not affect the contents of the source file;
We typically use the SED command to automatically edit one or more files, simplifying the loop of files;
sed command:
Sed-stream Editor for filtering andtransforming text
sed [OPTION] ... ' Script ' [Input-file] ...
Common options:
-N,--quiet,--silent : For lines that do not match the pattern, the default is not output to the screen;
-E script,--expression=script : Multi-conditional editing;
-F script-file,--file=script-file : Reads the script from the specified script-file
Note: Script-file is a path that can be positioned to a script file, preferably an absolute path
-i[suffix],--in-place[=suffix] : The original file direct editing operation;
-R,--regexp-extended : Enables SED to support extended regular expressions;
Script :
' Addressescommand '
Addresses ( address delimitation):
1. empty address: All lines of the file are processed;
2. single address: sed handles the only row that can match the address;
Num : Indicates the line number;
/pattern/ : All rows capable of matching the pattern;
$ : Represents the last line of the document
3. address range:
ADDR1,ADDR2 : All rows from ADDR1 to the end of ADDR2;
Example: 2,8
First~step : Starting with the line number of the first tag, the step is the number represented by step;
Example:: Odd rows 2~2 to even rows
Addr1,+n : Starting with ADDR1, including the Addr1 row, and continue to calculate n rows backwards;
For example: 2,+7 matches a total of 234,567,891 rows to eight rows, equivalent to 2,2+7
Addr1,~n : Starting from ADDR1, including the Addr1 row, calculates the addr1*n row backwards;
For example: 2,~5 equivalent to 2,2*5
/pattern1/,/pattern2/ : Starts from the first line that is matched by the PATTERN1, until the first line that is matched by the/pattern2/ends;
Command ( processing commands):
= : Displays the line number of the row being matched by the pattern
Example: Sed-n '/^$/= '/etc/grub2.cfg
A\text : Append text to the line after matching the pattern, support the use of \ n line, so as to achieve multi-line append;
I \text : Inserts text in front of the line that is matched by the pattern, and supports the use of \ n line wrapping for multi-line insertion;
C \text : Changes to the contents of the line that are matched by the pattern to text, support the use of \ n line wrapping, to achieve a row of more rows;
D : Delete the rows in the pattern space that are matched by the pattern, so that the rows are no longer standard output;
Note: the-n option is not appropriate when using the D command;
P : Displays the lines in the pattern space that are matched by the pattern;
Note: When using the P command, it is usually paired with the-n option;
W filepath : The rows in the pattern space that are matched by the pattern are saved to the FilePath file;
R filepath : Appends the contents of the FilePath file to a pattern-matched row in the pattern space;
! Command: Rows that are pattern-matched in pattern space, do not execute command commands, instead, rows that are not matched by a pattern, the delegate executes command
s/// : Find replacement, separators can be arbitrarily replaced, as long as the same;
[Email protected]@@, s###, s,,,
S/pattern/text/[control]
[Email protected]/etc/[email protected]/etc/[email protected]
pattern : What you plan to find and replace
text : The result to replace
Control : How to replace
g : All in-line replacement
P : Shows the row that replaced successfully
W filepath : Save the successfully replaced row to the FilePath file;
support for back references:
S/\ (string\)/&/
S/\ (string\)/\1/
Advanced editing commands:
h: overwrite the contents of the pattern space into the hold space
H : Appends the contents of the pattern space to the hold space
g: remove data from hold space to pattern space
G : Append content from hold space to mode space
x: swap the content in the pattern space with the content in the hold space
N: reads the next line of matching rows to the pattern space
N : Append the next line of matching rows to the mode space
D: Delete rows in a pattern space
D : Delete all rows in multi-line mode space
This article is from the "12657170" blog, please be sure to keep this source http://12667170.blog.51cto.com/12657170/1930754
SED command and Vim's last line command