Introduction to the Unix sed flow editor get you started sed

Source: Internet
Author: User

have been looking at the Linux shell for a while to program, but also to complete some basic use, in order to deepen understanding, just see the Unix School a Sed&awk tutorial, not a simple command parameter accumulation, but a fairly practical series, therefore, I hope to complete the translation in a few days. The translation process will not translate verbatim, will be interspersed with some comments, including some of their own understanding and other references, as the opening, simply mention the work of the SED mechanism, the understanding of the following will be a great help.

What is sed:


SED is a non-interactive flow editor (stream editor). Non-interactive refers to the use of SED to edit the text only at the command line, and then to view the output on the screen, and the so-called flow editor, which means that SED reads only one line at a time from a file (or input) and then handles the specified row. and output the results to the screen (unless you cancel the screen output without explicitly using the Print command), and then read the next line. The entire file is processed and output line by line, like running water. (via Walk in Mindfields)

sed working mechanism:

SED maintains two buffers, pattern spaces, and hold space, which are empty until the command starts executing.

The pattern space buffer is used to temporarily save the contents of each read row, and most of the matching and substitution actions are made for the content in pattern spaces, so there is no effect on the input file, and hold space is used as a fallback buffer. Unless you specify some special commands (such as the D Delete command), the contents of pattern spaces are emptied after a row is processed, but the contents of hold space are not deleted when each row is processed.

That is, the pattern spaces are equivalent to our memory, hold space is the same as the hard disk. When processed in memory, the processed is put back to the hard drive. (This is my understanding, a little bit inappropriate, but so some concepts will be better understood.)

Specifically, it can be roughly divided into the following steps:

1. First, read a line from the standard input stream, remove the line break, and then save it in pattern spaces

2. Execute the specified command (each command has an optional address (either a line number or a regular expression match), which is used as a test before executing the command, specifying the actions that need to be performed on those rows. The command is executed only if the current row matches. )

3. When all the commands are executed, the pattern space content is processed, and SED defaults to print the contents of the pattern spaces to the standard output, and the line breaks that are removed are printed. Bank operation completed.

4. Then sed reads the contents of the next line and performs the same operation again. Until the end of the line.

Basically, the most basic theory is almost the same. The main point is that this mechanism is more important. The following is a slowly deepening understanding from the example.

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.