Chinaunix首页 | 论坛 | 博客
  • 博客访问: 870659
  • 博文数量: 647
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 6044
  • 用 户 组: 普通用户
  • 注册时间: 2019-11-08 16:30
文章分类

全部博文(647)

文章存档

2021年(268)

2020年(297)

2019年(82)

我的朋友

分类: LINUX

2020-12-02 15:00:30

  Linux运维三剑客学习知识中,sed执行流程是怎样的?
  一。 简易流程
  1.读取文件(从文件或管道)的第1行
  2.读取到sed使用的内存区域中(模式空间)
  3.进行判断这一行是否是我要的
  1.如果是则则执行对应的命令(p d c a i s )
  2.如果不是则读取下一行(注如果没加上-n sed会默认显示这行内容(模式空间内容))
  二。详细执行过程
  详细过程一定要看官方的说明:info sed
  ——sed' maintains two data buffers: the active pattern space, and the
  auxiliary hold space. Both are initially empty.
  sed命令包含两个用来存放数据的内存区域(两个buffer区域):
  pattern space 模式空间
  hold sapce 保持空间
  两个空间默认都是空的
  ——sed' operates by performing the following cycle on each line of
  input:
  sed命令的执行过程(一个循环过程)如下所示:
  first, ——sed' reads one line from the input stream, removes any
  trailing newline, and places it in the pattern space.
  第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 ——]#
  以上便是今天分享的Linux运维学习中sed执行流程的相关内容。
  转自:https://www.oldboyedu.com/blog/1938.html
阅读(1452) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~