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.