SED is the short name of the stream editor, which is the stream editors. It handles a line of content at a time, processing, storing the currently processed row in a temporary buffer, called the pattern space, and then processing the contents of the buffer with the SED command, and then sending the contents of the buffer to the screen after processing is complete. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output.
Using syntax
The rules for using SED commands are:
sed ' Command ' input_file
- - N uses quiet (silent) mode (wondering why not-s). In the usage of general sed, all content from stdin is generally listed on the screen. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed, where option is optional and the usual options are as follows:
- - e performs the action editing of SED directly in the instruction-list mode;
- - f to directly write the action of SED in a file, you
-f filename can execute the SED command within the filename;
- - R enables the SED command to support extended regular expressions (default is the underlying regular expression);
-I directly modifies the contents of the read file instead of the screen output.
The following commands are commonly used:
a \: append appends the string, followed by the string s (the multiline string can be separated by \ n), then the string s is appended to the currently selected row;
- c \: replace/replace string, c \ followed by string s (multiline string can be separated by \ n), the currently selected line is replaced with the string s;
- d: Delete is deleted, the command will delete the currently selected row;
- i \: insert is the insertion string, i \ followed by the string s (the multiline string can be separated by \ n), the string s is inserted before the currently selected line;
- P: Print is printed, and the command prints the currently selected line to the screen;
- s: Replace, usually the s command is used like this: replace the old string with the
1,2s/old/new/g new string
Action Description: [N1[,n2]]functionn1, N2: Not necessarily exist, generally represents "select the number of lines of action", for example, if my action is required between 10 to 20 rows, then "10,20[Action Behavior"
command example
Suppose there is a local file Test.txt, the file contents are as follows:
Cat Test.txt This was first linethis is second linethis are third linethis is fourth linethis fifth Linehappy everydayend
c1>
a command
sed ' 1a \add One ' Test.txtthis is first Lineadd onethis is second linethis are third linethis is fourth linethis fifth Linehappy Everyda Yend
The 1 in this command section represents the first row, the same second line is written in 2, the first line is written in the third line, and the 1,3 $ last line is represented, for example, the second line to the last row, 2,$ and all the rows in the middle (containing the second row and the last row).
The purpose of this example is to add the string "add one" after the first line, and to see the concrete effect from the output.
sed ' 1, $a \add one ' Test.txtthis is first Lineadd onethis is second Lineadd onethis are third Lineadd onethis is fourth Lineadd onethis fi Fth lineadd onehappy everydayadd oneendadd One
This example indicates that the "add one" string is appended to all rows in the first and last row, and the effect is visible from the output.
sed ' /first/a \add One ' Test.txtthis is first Lineadd onethis is second linethis are third linethis is fourth linethis fifth Linehappy Everyda Yend
This example adds the string "add one" after the line that contains the "first" string, and from the output you can see that the top row contains the primary, so the first line is added after the "add one"
sed ' /^ha.*day$/a \add One ' Test.txtthis is first linethis are second linethis is third linethis are fourth linethis fifth Linehappy Everydayadd on Eend
This example uses a regular expression to match a row, representing a line that begins with Ha and ends with day, so you ^ha.*day$ can match the file's "Happy everyday" so that the "add one" string is added after the line.
I command
The I command uses the same method as the a command, except that the string is inserted in front of the matching line, so I can just replace the A of the example of the a command above, and not be verbose here.
C command
sed ' $c \add One ' Test.txtthis is first linethis is second linethis are third linethis is fourth linethis fifth linehappy everydayadd o NE
This example replaces the last line with the string "Add one", which shows the effect from the output.
sed ' 4, $c \add One ' Test.txtthis is first linethis was second linethis is third Lineadd one
This example replaces the contents of line fourth to the last line with the string "Add one".
sed ' /^ha.*day$/c \replace Line ' Test.txtthis is first linethis are second linethis is third linethis are fourth linethis fifth Linereplace Lineend
d command
sed ' /^ha.*day$/d ' Test.txtthis is first linethis are second linethis is third linethis are fourth linethis fifth Lineend
This example deletes a line that begins with Ha and ends with day.
sed ' 4, $d ' Test.txtthis is first linethis is second linethis are third line
This example deletes the contents of line fourth to the last row.
P command
sed ' 4, $p ' Test.txtthis is fourth linethis fifth Linehappy Everydayend
This example prints the contents of line fourth to the last line on the screen, and the P command is generally used with the-n option.
sed ' /^ha.*day$/p ' Test.txthappy Everyday
This example prints a line that starts with HA and ends with day.
s command
The actual use of the S command is most commonly used.
sed ' s/line/text/g ' Test.txtthis is first textthis are second textthis is third textthis are fourth textthis fifth Texthappy everydayend
This example replaces all lines in the file with text, and the final G is the meaning of global, which is the replacement of the whole, and if no g is added, only the first line of our line will be replaced.
sed ' /^ha.*day$/s/happy/very happy/g ' Test.txtthis is first linethis are second linethis is third linethis are fourth linethis fifth Linevery Happy Everydaye nd
This example first matches a line starting with Ha, ending with day, in this case the line that matches is "happy everyday", and then replaces the happy in that row with very happy.
sed ' s/\ (. *\) line$/\1/g ' Test.txtthis is first this is the second this was third this is fourth this fifth happy Everydayend
This example is a little bit more complicated, first break it down. First the S command mode is s/old/new/g this, so the old part of this example \(.*\)line$ , the SED command uses the \(\) contents of the package to represent the nth part of the regular expression, the sequence number is calculated starting from 1, in this case there is only one \(\) so that the \(.*\) first part of the regular expression, This part matches any string, so the \(.*\)line$ match is any line ending with lines. The matched rows are then replaced with the first part of the regular expression (in this case, the line section is deleted), the first part that represents the match is used, the second part is represented, and the \1 \2 \3 third part, which can be referenced in turn. For example, the following:
sed ' s/\ (. *\) is\ (. *\) line/\1\2/g ' Test.txtthis First this second this third this fourth th fifth Happy Everydayend
isboth sides of the regular expression can be used \1 and \2 expressed, the role of this example is to delete the middle part is.
Sed flow editor commands in Linux