免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1916 | 回复: 3
打印 上一主题 下一主题

转帖老文:sed in (pseudo) microcode [复制链接]

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-10-31 16:15 |只看该作者 |倒序浏览
sed in (pseudo) microcode, by Carlos J. Duarte.

  1. sed in (pseudo) microcode, by Carlos J. Duarte.
  2. [ This article was posted to the seders mailing list ]

  3. Al, this is a little doc, with some pseudo code, of
  4. a sed implementation.

  5. The idea, is when people (that knows sed), has a question
  6. or need some fine tunning, to quickly answered themselves.

  7. One particulary example, are the strange behaviors of D, and a\
  8. (well, sort of).

  9. As a last note remark, I am revieing my scripts packages, and
  10. also some documentation on sed (do-it-with-sed, advanced-sed,
  11. hints-sed? or sed-fine-tunning). This should be ok on august.
  12. For now (upto end of july), I will not (probably), dedicate
  13. any time to that.

  14. So, there goes the text.

  15. ADDRESSES
  16. =========

  17. (0) no address, just the command per se
  18. (1) a single address or none, is permitted
  19. (2) a single address, a double address, or none is permitted

  20. any address, if specified, may be a number -- specifies
  21. the input line number, or a regular expression -- specifies
  22. all pattern spaces, that matches it.

  23. one address, appears per se, before the command
  24. two addresses, appears separated per a comma `,'

  25. if an address is omitted, the command will be apllied
  26. on all pattern spaces that passes through it

  27. y/a/b/                  executes on all ps
  28. 3 y/a/b/                executes on ps which last line loaded, was 3
  29. /foo/ y/a/b/            executes on ps that matches /foo/
  30. 4,/foo/ y/a/b/          executes from ps which last line loaded was 4,
  31.                         up to, including, ps that matches /foo/
  32. /foo/,5 y/a/b/          as above, but from /foo/ up to 5

  33. COMMANDS MICROCODE
  34. ==================

  35. notes:
  36. - first, everything begins with a (sic) `goto [end]', after
  37.   all variables have been zeroed/offed/emptyed

  38. - all commands, have a `pop ps' and a `push ps', to enforce that
  39.   they really operate on patern space

  40. - the `print ps to stdout' "microcode" below, prints an extra newline
  41.   after the print the `ps' itself

  42.         [end]
  43.         . pop ps
  44.         . if not nflag and line_counter>=1, print ps to stdout
  45.         . print append_buffer to stdout (which might be empty)
  46.         . empty append_buffer
  47.         . set tflag off (see s, t)
  48.         . if come_from_D
  49.         .       set come_from_D off (see D)
  50.         . else
  51.         .       if no more input lines, exit
  52.         .       load next line into ps, increment line_counter
  53.         . push ps
  54.         . goto start

  55. (2) s/RE/replacement/[gp#w], #: 1-9
  56.         . pop ps
  57.         . if # not given, or g given, #=1
  58.         . set last_match = begin of ps
  59.         !! do not set tflag off (see t, [end])
  60.         [1]
  61.         . repeat # searches for RE, on ps, from last_match to end
  62.         . if found
  63.         .       tflag on
  64.         .       replace the found match, per `replacement'
  65.         .       if w file, append ps at end of `file'
  66.         .       if p, print ps to stdout
  67.         .       if g, set last_match to end of replacement+1 and goto [1]
  68.         . push ps

  69. (2) y/list1/list2/
  70.         . pop ps
  71.         . foreach character c1 on list1
  72.         .       fetch character c2 from list2
  73.         .       replace all occurrences of c1 per c2
  74.         . push ps
  75. (1)i\
  76. text
  77.         . print text to stdout immediatly

  78. (1)a\
  79. text
  80.         . collect (i.e append) text to append_buffer (see [end])

  81. (2)c\
  82. text
  83.         . print text to stdout immediatly
  84.         . pop ps
  85.         . empty ps
  86.         . push ps
  87.         . goto [end]
  88.         !! because ps is empty, the `print ps' at [end] will not
  89.            produce output

  90. (1)r file
  91.         . print contents of file, immediatly to stdout

  92. (2)w file
  93.         . pop ps
  94.         . append ps at end of file
  95.         . push ps

  96. (0): label
  97.         . name next command, as `label'

  98. (2)b label
  99.         . goto command named `label'

  100. (2)t label
  101.         . if tflag on (see s///)
  102.         .       set tflag off
  103.         .       goto command `label'

  104. (1)q
  105.         . pop ps
  106.         . if not nflag, print ps to stdout
  107.         . print append_buffer (might be empty, which cause no output)
  108.         . exit

  109. (2)p
  110.         . pop ps
  111.         . print ps to stdout
  112.         . push ps

  113. (2)l
  114.         . pop ps
  115.         . copy ps to tmp
  116.         . replace on tmp, some special characters, per their
  117.           conventional equivalents \x
  118.         . print tmp
  119.         . push ps

  120. (1)=
  121.         . print line_counter to stdout

  122. (2)n
  123.         . if no more lines on input, goto [end]
  124.         . pop ps
  125.         . if not nflag, print ps to stdout
  126.         . load next line into ps, increment line_counter
  127.         . push ps
  128.         !! and continues, does not jump to start, or end

  129. (2)d
  130.         . pop ps
  131.         . empty ps
  132.         . push ps
  133.         . goto [end]

  134. (2)h
  135.         . pop ps
  136.         . set hold_buffer as ps
  137.         . push ps

  138. (2)H
  139.         . pop ps
  140.         . set hold_buffer to hold_buffer+\n+ps
  141.         . push ps

  142. (2)g
  143.         . pop ps
  144.         . set ps as hold_buffer
  145.         . push ps

  146. (2)G
  147.         . pop ps
  148.         . set ps as ps+\n+hold_buffer
  149.         . push ps

  150. (2)x
  151.         . pop ps
  152.         . push hold_buffer
  153.         . set hold_buffer as ps
  154.         !! exchange hold buffer with pattern space

  155. (2)N
  156.         . if no more lines on input, goto [end]
  157.         . pop ps
  158.         . load next line into tmp, increment line_counter
  159.         . set ps to ps+\n+tmp
  160.         . push ps
  161.         !! and continues, does not jump to start, or end

  162. (2)P
  163.         . pop ps
  164.         . copy ps to tmp
  165.         . print remove from first \n upto end
  166.         . print ps to stdout
  167.         . push tmp

  168. (2)D
  169.         . pop ps
  170.         . if ps does not contain a \n
  171.         .       empty ps
  172.         .       push ps
  173.         .       goto [end]
  174.         !! i.e. does the same as `d'
  175.         . remove from beginning of ps, upto, including, first \n
  176.         . push ps
  177.         . set come_from_D on (see [end])
  178.         . goto [end]
  179.         !! do not load next line, if ps contained \n

  180. ==
  181. Carlos Duarte, 980712



复制代码

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
2 [报告]
发表于 2011-10-31 16:16 |只看该作者
最近有不少SED痴汉,可以把一些代码对照这个和sedsed的结果琢磨一下

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
3 [报告]
发表于 2011-10-31 20:39 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
4 [报告]
发表于 2011-11-01 08:36 |只看该作者
mark 备查
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP