lihongjie0209 / myblog

4 stars 0 forks source link

Sed: 工作原理 #251

Open lihongjie0209 opened 3 years ago

lihongjie0209 commented 3 years ago

sed maintains two data buffers: the active pattern space, and the auxiliary hold space. Both are initially empty.

sed operates by performing the following cycle on each line of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed.

When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.8 Then the next cycle starts for the next input line.

伪代码

activePatternSpace = ""
holdSpace = ""

while(activePatternSpace  = readLine()!=null){

  for cmd in script:
    activePatternSpace = cmd.call(activePatternSpace)

}

print activePatternSpace 

关于命令的定义应该是

class Cmd{
   condtion;
   command;

   call(activePatternSpace){

     if condition(activePatternSpace){
        return command.run(activePatternSpace)

     }

   return activePatternSpace

   }

}

备注:

上述的伪代码知识一个框架, 实际上为了实现condition, 我们需要把一些上下文参数传递给cmd, 比如说当前的行号