Linux运维知识:sed执行流程是怎样的?

  Linux运维三剑客学习知识中,sed执行流程是怎样的?老男孩教育Linux培训带你们一块儿来看看。html

一. 简易流程

1.读取文件(从文件或管道)的第1行
2.读取到sed使用的内存区域中(模式空间)
3.进行判断这一行是不是我要的less

1.若是是则则执行对应的命令(p d c a i s )
2.若是不是则读取下一行(注若是没加上-n sed会默认显示这行内容(模式空间内容))运维

二.详细执行过程

详细过程必定要看官方的说明:info sedide

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

sed命令包含两个用来存放数据的内存区域(两个buffer区域):spa

pattern space 模式空间
hold sapce 保持空间
两个空间默认都是空的code

`sed' operates by performing the following cycle on each line of
input:orm

sed命令的执行过程(一个循环过程)以下所示:htm

first, `sed' reads one line from the input stream, removes any
trailing newline, and places it in the pattern space.blog

第1步,sed从输入(文件或管道)中读取1行,删除每一行的回车符号而后存放在模式空间(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.

第2步,开始执行对应的命令,每一个命令前面能够有一个条件(地址),只有知足条件的时候才会执行对应的命令(p d s c a i等等).

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.(1) Then the
next cycle starts for the next input line.

第3步,当命令执行完成(这里命令指的是sed内部的p d s c a i 等等),若是没有-n参数(取消sed命令默认输出)则sed会把当前模式空间的内容显示出来而后再加上1个回车(第1步被删除的).而后开始读取下一行,进行下一个循环.

Unless special commands (like D') are used, the pattern space is deleted between two cycles. The hold space, on the other hand, keeps its data between cycles (see commandsh', H',x', g',G' to move
data between both buffers).

补充说明:

模式空间的内容会在每一个循环结束后清空(除非加上D)。
保持空间的内容会在每次循环之间保留不被清空 (经过 h H x g G 移动保持空间和模式空间的数据)

(1) Actually, if sed' prints a line without the terminating newline, it will nevertheless print the missing newline as soon as more text is sent to the same output stream, which gives the "least expected surprise" even though it does not make commands like `sed -n p' exactly identical to cat'.

实际上,sed若是显示没有回车的行,他会在你屡次显示到相同输出的时候替你加上缺乏的回车。这个特色sed -n p 和cat并不彻底相同.
这算是极特殊用法了,了解便可.

[root@oldboyedu-show01 ~]# cat old.txt   #这个文件就没有回车
oldboy[root@oldboyedu-show01 ~]# 

[root@oldboyedu-show01 ~]# sed -n p  old.txt   #经过sed显示的时候 没有显示每行的结尾
oldboy[root@oldboyedu-show01 ~]# 

[root@oldboyedu-show01 ~]# sed -n p  old.txt old.txt old.txt   #屡次输出的时候sed会自动加上 回车
oldboy
oldboy
oldboy[root@oldboyedu-show01 ~]# 

[root@oldboyedu-show01 ~]# cat old.txt old.txt old.txt  #一样输出到相同的地方(屏幕) cat不会自动加上回车
oldboyoldboyoldboy[root@oldboyedu-show01 ~]#

转自::https://www.oldboyedu.com/blog/1938.html

相关文章
相关标签/搜索