What is sed?
Sed is a stream editor with the full name of stream editors, which means data that can be processed or piped. Mainly for rows, that is, line editing. The default is not to edit the file, when processing, the current processing of the row is stored in a temporary buffer, called the pattern space, followed by the SED command to process the contents of the buffer, after processing is done, the buffer contents are sent to the screen by default, followed by the next row. Processing only data in the pattern space, printing the pattern space to the screen after processing is finished
The use of sed
sed [options] ' Addresscommand ' file
Options
-e SCRIPT can execute multiple addresscommand,sed-e at the same time ...-e ...
-n Silent mode, does not display the contents of the mode space
-I directly modify the original file
-R SED does not support extended regular expressions by default, and-R supports
-F executes the specified file, which contains a series of formats for Addresscommand
Address
1 Startline,overline separated by commas
2/pattern/with regular expressions
3/pattern1/,/pattern2/the first time that a pattern-matching row begins, to the end of the first line that is matched to the pattern 2
4 LineNumber The specified line, where $ represents the last row.
5 Startline +n starts from the specified line, and the N-line is backward, with a total of n+1 rows.
command If multiple commands are used ; Separate
D Delete rows that match the criteria
P output line, N and p together to print only those rows where the transformation occurred
A\ Insert text below the current line
I\ inserting text above the current line
Q Exit SED
N reads the next line and uses the next command to process the new line instead of the first command.
A (here is a space) \ ' String ' Appends a new row after the specified line, with the contents of string
I (There are spaces here) \ ' string ' before line
R (there are spaces here) file adds the specified file contents to each line that meets the criteria, which can be used to merge two files
W (There are spaces here) the file specifies the range in which the expert is saved as the specified file.
The s////pattern/string/modifier finds the pattern and replaces it with a string, by default replacing only the first string in each row. /can be used @ #等代替.
Modifier g global Substitution
I ignore case
Assume that the processed text is Test.file
Add characters to the head of each line, such as "head", with the following command:
Sed ' s/^/head&/g ' test.file
Add characters at the end of each line, such as "TAIL", with the following command:
Sed ' s/$/&tail/g ' test.file
add content at the end of the specified line:
Cat file
Aaaa
Bbbb
Cccc
Sed ' s/cc.*/& eeee/g ' file
The SED of Linux basic commands