Sed of shell

Source: Internet
Author: User

Sed is an online editor that handles a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer, known as pattern spaces, followed by the SED command to handle the contents of the buffer, and the contents of the buffer are sent to the screen after processing is complete. The next line is processed so that it repeats until the end of the file . The contents of the file do not change unless you use redirected storage output. SED is used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program, etc.


Delete: D command    *   $ sed  ' 2d '  example-----Delete the second line of the example file.    *   $ sed  ' 2, $d '  example-----Delete the second line of example file to all rows at the end.    *   $ sed  ' $d '  example deletes the last line 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-----replaces test with mytest across the entire line. If there is no G-tag, only the first matching test for each row is replaced with mytest.    *   $ sed -n  ' s/^test/mytest/p '  example-----(-N) option is used with the P flag to print only those lines that have been replaced. In other words, if test at the beginning of a line is replaced with mytest, it is printed.    *   $ sed  ' s/^192.168.0.1/&localhost/'  example-----& The symbol represents the part that is found in the replacement string. All rows beginning with 192.168.0.1 are replaced with  localhost, which becomes 192.168.0.1localhost.    *   $ sed -n  ' s/\ (love\) able/\1rs/p '  example-----Love was marked as 1, All loveable are replaced with lovers, and the replaced rows are printed.    *   $ sed  ' s#10#100#g '  example----- whatever the character, followed by the S command is considered a new delimiter, so, "#" Here is the separator, instead of the default "/" separator. means to replace all 10 with 100.    range of selected rows: Comma    *   $ sed -n  '/test/,/check/p '   Example-----All rows that are within the scope determined by the template test and check are printed.    *   $ sed -n  ' 5,/^test/p '  example-----Print all rows from line fifth to the first line that contains the start of 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 a string sed test.    Multi-point edit: E-command    *   $ sed -e  ' 1,5d '  -e  ' s/test/check/'   The example-----(-e) option allows multiple commands to be executed on the same line. As the example shows, the first command deletes 1 to 5 rows, and the second command replaces test with check. The command   order has an effect on the result. 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 a value to a SED expression.    read from File: R command    *   $ sed  '/test/r file '   The contents of the example-----file are read in, displayed after the row that matches the test, and if multiple rows are matched, the contents of file are displayed below all matching rows.    write to file: w command    *   $&NBsp;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 ' is appended to the line beginning with test, and SED requires that a backslash be followed by command a.    insert: I command    $ sed  '/test/i\\   new line  ----------------- --------'  example   if test is matched, insert the text after the backslash into the front of the matching row.    Next: N command    *   $ sed  '/test/{ n; s/aa/bb/; } '   Example-----if test is matched, move to the next line of the matching row, replace the AA of the line, change to BB, print the line, and then continue.    variants: y command    *   $ sed  ' 1,10y/abcde/abcde/'   Example-----turn all ABCDE in the 1--10 line to uppercase, note that the regular expression metacharacters cannot use this command.    exit: Q command    *   $ sed  ' 10q '  example exit sed after printing line 10th.    Keep and get: H command and G command    *   $ sed -e  '/test/h '  -e  ' $G   Example-----in SWhen Ed processes files, each row is saved in a temporary buffer called the mode space, and all rows processed will be   printed on the screen unless the row is deleted or the output is canceled. The mode space is then emptied and stored in a new line waiting to be processed. In this example, when the row that matches the test is found, it is stored in the mode space, and the H command copies it and stores it in a special buffer called the   hold buffer. The second statement means that when the last line is reached, the G command takes out the line that holds the buffer and then puts it back in the pattern space and appends to the end of the line that already exists in the mode space  . In this case, append to the last line. In simple terms, any row containing test is copied and appended to the end of the file.    Maintain and interchange: H command and x command    *   $ sed -e  '/test/h '  -e  '/check/x '  example -----Swap mode space and keep the contents of the buffer. That is, the line that contains test and check is swapped.    7.  Script    sed script is a list of SED commands that boot the script file name with the-F option when you start sed. Sed is very picky about the commands entered in the script and cannot have any white space or text at the end of the command, separated by semicolons if there are multiple commands on one line. Actions that begin with # comment on rows and cannot span rows.  





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.