The practice of SED

Source: Internet
Author: User

1.sed (stream editor) background knowledge

(1) The basic format of the SED command line is:

SED option ' script ' file1 file2 ... sed option-f scriptfile file1 file2.

(2) SED processing files: can either be redirected from the standard input, or when the command line parameters are passed in, command line parameters can be passed through multiple files at one time, SED will be processed sequentially.

(3) sed edit command: Can be directly when the command line arguments passed in, you can also write a script file and then specify with the-f parameter.

(4) The format of the edit command is:/pattern/action

    1. Pattern is a regular expression, and action is an edit operation.

    2. The SED program reads the pending file one line at a time,

    • If a line matches the pattern, the corresponding action is executed;

    • If a command has no pattern and only action, this action will be used for each row of the file to be processed.

(5) How SED works

    1. Sed is an online editor that processes a single line of content at a time.

    2. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file.

    3. The file content does not change unless you use redirection to store the output.

    4. SED is mainly used to automatically edit one or more files, simplifying the repeated operation of files;


2. Use

(1)/pattern/p: Print lines matching pattern

    1. Note: Print the original file contents and the line matching the pattern (i.e. re-print the matching lines on the basis of the source file)

    2. Just want to output processing results, plus the-n option

(2)/PATTERN/D: Delete lines that match pattern

    1. Note: The SED command does not modify the original file, and the Delete command only indicates that some lines do not print out the output, rather than deleting from the original file.

    2. You can use Sed-i to modify the original file.

(3)/pattern/s/pattern1/pattern2/: Find the line that matches the pattern and replace the string with the first match pattern1 of the line with PATTERN2

    1. If you want to replace all the satisfying conditions of the text with the G, i.e./pattern/s/pattern1/pattern2/g

    2. Pattern2 in the &: A string that matches pattern1 in the current line of the original file


    • The \1 in pattern2 represents the content that matches the first () parenthesis of pattern1, and \2 represents the content that matches the second () parenthesis of pattern1.

    • SED uses the basic regular expression specification by default, and if the-r option is specified with the extended specification, then the () parentheses do not have to be escaped.

(4) addressing

Sed-n ' 3p ' file #打印第三行sed-n ' 100,300p ' file #打印100 ~300 line information (including 100 and 300)

(5) Order

A\: Adds one or more rows after the current line. Multiple rows In addition to the last line, the end of each line requires "\" To continue c\: Replace the text in the current line with the new text after this symbol. Multiple rows In addition to the last row, the end of each line requires "\" to continue i\: Insert text before the current line. Multiple rows except the last line, the end of each line requires "\" to continue the line D Delete line h: The contents of the pattern space is copied to the staging buffer H: The contents of the schema space is appended to the staging buffer G: The contents of the staging buffer are copied to the schema space, overwriting the original content g: Append the contents of the staging buffer Append to the original content L: List nonprinting characters p: Print line N: reads the next input line and starts processing it from the next command instead of the first command Q: End or exit Sedr: reads the input line from the file! : Apply command S to all rows except the selected row: replace one string with another G: global substitution in a row w: Write the selected row to file x: Swap staging buffer vs. contents of pattern space y: replace the character with another character (cannot use the Y command for regular expressions)-e: Make multiple edits, That is, when applying multiple SED commands to an input row, use-N: Cancels the default output-F: Specifies the file name of the SED script

(6) Exit status

Regardless of whether the specified pattern is found, its exit status is 0. The exit status of SED is not 0 only if there is a syntax error in the command.


3. Use

(1) Add a blank line to the end of each line

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/83/5A/wKiom1dw8NiwCc6dAABMKc9-ytg569.png "title=" sed. PNG "alt=" Wkiom1dw8niwcc6daabmkc9-ytg569.png "/>

(2) using SED to simulate the function of TAC (reverse output)

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/83/5A/wKiom1dw8W3xGV6OAABYSzTQbCk761.png "title=" sed1. PNG "alt=" Wkiom1dw8w3xgv6oaabysztqbck761.png "/>

    • 1! G: Line 1th does not execute the "G" command and executes from line 2nd. (Can be compared by the right figure-1 less!) , and finally output multiple lines of blank line)

    • $!d: The last row is not deleted (last 1 rows are retained)

Append the data to the pattern space (except for the first row), assign it to hold space, and then delete it from the schema space (except for the last row).

(3) Append match line to end of file

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/59/wKioL1dw8ZjyWktgAAA7513TZjM577.png "title=" Sed2. PNG "alt=" Wkiol1dw8zjywktgaaa7513tzjm577.png "/>

(4) Transformation of ranks

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/83/5A/wKiom1dw8bCAJK78AAAaeYe755o438.png "title=" sed3. PNG "alt=" Wkiom1dw8bcajk78aaaaeye755o438.png "/>

(5) Seeking the summation of 1~100

Method One:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/83/59/wKioL1dw8cuifRgYAAA-6vOQsso555.png "title=" sed4. PNG "alt=" Wkiol1dw8cuifrgyaaa-6voqsso555.png "/>

Method Two:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/83/59/wKioL1dw8f-ijJIUAAA9UHxmXAA405.png "title=" Sed6. PNG "alt=" Wkiol1dw8f-ijjiuaaa9uhxmxaa405.png "/>

(6) Print odd even lines

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/5A/wKiom1dw8eeSiRdiAAAxNMSMSr8915.png "title=" Sed5. PNG "alt=" Wkiom1dw8eesirdiaaaxnmsmsr8915.png "/>




This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1793405

The practice of SED

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.