Linux command-sed

Source: Internet
Author: User

    • Delete: D command

$ sed ' 2d ' example-– delete the second line of the example file.

$ sed ' 2, $d ' example-– delete the second line of the example file to the end of all lines.

$ sed ' $d ' example-– delete the last line of the example file.

$ sed '/test/' d example-– Delete The example file all rows containing test.

    • Replace: s command

$ sed ' s/test/mytest/g ' example-– replace test with mytest within the entire row range. If there is no G tag, only the first matching test of each row is replaced with mytest.

The $ Sed-n ' s/^test/mytest/p ' example-– (-N) option and the P flag are used together to indicate that only those rows that have substitution occur are printed. That is, if the test at the beginning of a line is replaced with mytest, it is printed.

The $ sed ' s/^192.168.0.1/&localhost/' example-–& symbol represents the part that is found in the replacement string. All lines starting with 192.168.0.1 will be replaced with a self-added localhost, which becomes 192.168.0.1localhost.

$ Sed-n ' s/\ (love\) able/\1rs/p ' Example-–love is marked as 1, all loveable are replaced with lovers, and the replacement line is printed.

$ sed ' s#10#100#g ' example-– no matter what character, followed by the S command is considered a new delimiter, so, "#" Here is the delimiter, instead of the default "/" delimiter. means to replace all 10 with 100.

    • Range of selected rows: comma

$ Sed-n '/test/,/check/p ' example-– all lines within the range determined by the template test and check are printed.

$ Sed-n ' 5,/^test/p ' example-– prints all rows starting from line fifth to the first one that contains the line starting with test.

$ sed '/test/,/check/s/$/sed test/' example-– for the line between the template test and West, the end of each line is replaced with 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 on the same line. As shown in the example, the first command deletes rows 1 through 5, and the second command replaces test with a check. The order in which the commands are executed has an effect on the results. If all two commands are replacement commands, the first substitution command affects 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 an SED expression.

    • Read from File: R command

The contents of the SED '/test/r file ' example-–file are read in and displayed after the line matching the test, and if multiple lines are matched, the contents of file will be displayed below all matching rows.

    • Write file: w command

$ Sed-n '/test/w file ' example-– all rows containing test in example are written to file.

    • Append command: A command

$ sed '/^test/a\\->this is a example ' example<-– ' This is a example ' appended to the line beginning with test, SED requires that command A has a backslash behind it.

    • Insert: I command

$ sed '/test/i\\new line ————————-' Example

If test is matched, the text that follows the backslash is inserted before the matching line.

    • Next: N command

$ sed '/test/{n; s/aa/bb/;} ' example-– if test is matched, move to the next line of the matching line, replace the AA for this line, change to BB, and print the line, and then continue.

    • Deform: Y command

$ sed ' 1,10y/abcde/abcde/' example-– converts all abcde in the 1–10 line to uppercase, note that regular expression metacharacters cannot use this command.

    • Exit: Q command

$ sed ' 10q ' example-– after printing line 10th, exit sed.

    • Hold and get: H command and G command

$ Sed-e '/test/h '-e ' $Gexample-when the SED processes the file, each row is saved in a temporary buffer called the pattern space, unless the row is deleted or the output is canceled, and all the processed rows are printed on the screen. The pattern space is then emptied and a new line is stored for processing. In this example, when a row matching test is found, it is stored in the pattern space, and the H command copies it into a special buffer called the hold cache. The second statement means that when the last line is reached, the G command takes out the row holding the buffer, then puts it back into the pattern space and appends it to the end of the line that already exists in the pattern space. In this example, it is appended to the last line. Simply put, any line containing test is copied and appended to the end of the file.

    • Hold and Swap: H command and x command

$ Sed-e '/test/h '-e '/check/x ' example-– swaps the mode space and preserves the contents of the buffer. That is, the line that contains test and check is swapped.

Linux command-sed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.