Delete:D command $ sed '2d 'example ----- Delete the second line of the example file.
$ Sed '2, $ d' example ----- delete all rows from the second row to the end of the example file.
$ Sed '$ d' example ----- Delete the last row of the example file.
$ Sed '/test/'d example ----- delete all rows containing test in the example file.
Replace:S command $ SED's/test/mytest/G' example ----- replace test with mytest in the entire line. If no G tag exists, only the first matched test in each row is replaced with mytest.
$ Sed-n's/^ test/mytest/P 'example ----- (-N) option and the P Flag are used together to print only the replaced rows. That is to say, if the test at the beginning of a row is replaced with mytest, print it.
$ SED's/^ 192.168.0.1/& localhost/'example ----- &; symbol represents the part found in the replacement string. All rows starting with 192.168.0.1 are replaced with their own localhost and changed to 192.168.0.1localhost.
$ Sed-n's/\ (love \) Able/\ 1RS/P 'example ----- love is marked as 1, and all loveable will be replaced with lovers, the replaced line is printed out.
$ SED's #10 #100 # g'example ----- whatever the character, followed by the S command is considered as a new separator. Therefore, "#" is a separator here, replaces the default "/" separator. Replace all 10 with 100.
Range of selected rows:Comma $ sed-n'/test/,/check/P' example ----- all rows within the range specified by the template test and check are printed.
$ Sed-n' 5,/^ test/P' example ----- print all rows starting from the fifth line to the first line that contains the beginning of test.
$ Sed '/test/,/check/S/$/SED test/' example ----- for the rows between the template test and west, the end of each row is replaced by the string sed test.
Multi-Point Editing:E command $ sed-e '1, 5d '-E's/test/check/'example ----- (-e) option allows multiple commands to be executed in the same line. As shown in the example, the First Command deletes lines 1 to 5, and the second command replaces test with check. The command execution sequence has an impact on the result. If both commands are replacement commands, the first replacement command will affect the result of the second replacement command.
$ Sed -- Expression ='s/test/check/'-- Expression ='/love/d' example ----- a better command than-E is -- expression. It can assign values to SED expressions.
Read from file:R command $ sed '/test/R file 'example ----- the content in file is read and displayed after the row matching test. If multiple lines match, the file content is displayed under all matched rows.
Write files: W command $ sed-n'/test/W file' example ----- in example, all rows containing test are written into file.
APPEND Command: A command $ sed '/^ test/A \ ---> This Is A example 'example <----- 'this is a example' is appended to the end of the line starting with test, sed requires that command a be followed by a backslash.
Insert: I command $ sed '/test/I \\
New Line
------------------------- 'Example
If test is matched, the text following the backslash is inserted before the matching row.
Next: N command $ sed '/test/{n; S/AA/BB/;}' example ----- if test is matched, move it to the next line of the matching line, replace the AA of this row with BB, print the row, and continue.
Deformation: The y command $ sed '1, 10y/ABCDE/'example ----- converts all ABCDE in line 1-10 to uppercase. Note that this command cannot be used for the Regular Expression metacharacters.
Exit: Q command $ sed '10q' example ----- exit sed after printing the 10th lines.
Keep and get: H and G commands $ sed-e '/test/H'-e' $ g example ----- when sed processes files, each row is saved in a temporary buffer called the mode space. Unless the row is deleted or the output is canceled, all processed rows are printed on the screen. The mode space is cleared, and a new row is saved for processing. In this example, the row Matching Test is found and saved to the mode space. The H Command copies the row and saves it to a special buffer zone called keep cache. The second statement means that when the last line is reached, the G command extracts the row that maintains the buffer and places it back in the mode space, and append it to the end of the row that already exists in the mode space. In this example, It is appended to the last row. Simply put, any row containing test is copied and appended to the end of the file.
Keep and swap: The H command and the X command $ sed-e '/test/H'-e'/check/X' example ----- swap mode space and keep the buffer content. That is, to swap the rows containing test and check.
Detailed explanation of SED commands: http://www.tsnc.edu.cn/default/tsnc_wgrj/doc/sed.htm or encyclopedia http://baike.baidu.com/view/432091.htm#5