`
watershitter
  • 浏览: 42999 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

sed 学习记录

    博客分类:
  • sed
sed 
阅读更多
记录一下常用的sed 的 要点

sed 的执行过程:
In default operation, sed cyclically copies a line of input, less its terminating newline character, into a pattern space (unless there is something left after a D command), applies in sequence all commands whose addresses select that pattern space, and at the end of the script copies the pattern space to standard output (except when -n is specified) and deletes the pattern space. Whenever the pattern space is written to standard output or a named file, sed will immediately follow it with a newline character.
Some of the commands use a hold space to save all or part of the pattern space for subsequent retrieval. The pattern and hold spaces will each be able to hold at least 8192 bytes.
--------------------------------------------------------------------

重要的 空间名词
input :  输入 一般指 待处理的文件
pattern space: ‘模式空间' sed 利用命令对读入的每行进行处理的 ‘空间’
output: 打印出来的空间
hold space:Some of the commands use a hold space to save all or part of the pattern space for subsequent retrieval.
貌似是 sed 执行过程中用来 临时存储和替换的空间! 只有 g G h H x 五个命令用到.
从 g,和G 的效果来看,默认为空行什么也没有。  sed g file,则file有多少行,就会打印出多少空行...

options:

-n:
Suppress the default output (in which each line, after it is examined for editing, is written to standard output). Only lines explicitly selected for output will be written.
解析:默认的,sed 每处理一行都打印到stdout, 有了 -n,之后,则只打印“选择了”的行,
如: sed -n ‘/holy/p’ file,只打印 有 /holy/ 选择的行
     没有-n, /holy/选则的行会被打印两遍,一遍默认,一遍是'p'的效果!
     而sed -n 's/holy/shit/g' file, 什么都打印! p命令表示"explicitly selected for output", 其余默认都不打印

-f:
用来使用脚本跑sed命令
The script_files named by the -f option will consist of editing commands, one per line.

如: sed -f xxx file, xxx内容为's/holy/shit/g'
script 格式的注解:
The script consists of editing commands, one per line, of the following form: 
[address[,address]]command[arguments]
Zero or more blank characters are accepted before the first address and before command.

address这个词会多次出现,大概表示“定址”的意思?
Addresses in sedAn address is either empty, a decimal number that counts input lines cumulatively across files, a "$" character that addresses the last line of input, or a context address (which consists of a regular expression as described in Regular Expressions in sed , preceded and followed by a delimiter, usually a slash). A command line with no addresses selects every pattern space.

A command line with one address selects each pattern space that matches the address.

A command line with two addresses selects the inclusive range from the first pattern space that matches the first address to the next pattern space that matches the second. (If the second address is a number less than or equal to the line number first selected, only one line will be selected.) Starting at the first line following the selected range, sed looks again for the first address. Thereafter the process is repeated.
Editing commands can be applied only to non-selected pattern spaces by use of the negation command "!"

command list:
Two of the commands take a command-list, which is a list of sed commands separated by newline characters, as follows:
{ command
command
. . .
}
The "{" can be preceded with blank characters and can be followed with white space. The commands can be preceded by white space. The terminating "}" must be preceded by a newline character and then zero or more blank characters.

类似
a \
text  (append text) ,表示写法上在 slash'\'后换行,然后写要插入的text
这些需要换行的写法一般出现在script里面吧,直接命令行不常用,有些偏高级)

N : 读入下一行, (吧下一行和当前 pattern space 行的内容以 \n 连接起来,改变文件的行号)
Append the next line of input to the pattern space, using an embedded newline character to separate the appended material from the original material. Note that the current line number changes.

D     Delete  up  to  the  first embedded newline in the pattern space.  Start next cycle, but skip reading from the input if there is still data in the pattern space.
      删除直到遇到第一个 \n,"p  to  the  first embedded newline"(删除的内容包括\n)。如果之前用了 N,命令,达到了两行拼接后删除前行的效果


待补充...

------------------------------------------------------------------
关于 , 和 ;
, 用于两个 addrees之间, 如:
  1,10  1到10行
  /begin/,/end/
  1,10!    ' ! ' 表示去饭,即 1,10 之外的行
-----
; 对于没一个读入pattern space后,以 ; 为命令为分隔,依次执行各个命令
如 sed -n '/aa/,/cc/ n; p' hhh
   其中,hhh文件内容为
   holy
   aa#
   bb#
   cc#
   dd#
   shit
则输出:
holy
bb#
dd#
shit
奇怪吧? 解释。
1 sed 首先读入第一行 'holy' 到 patternspace, 第一个命令 /aa/,/cc/ n, 地址被过滤,不应用于该行,第二个命令 p 打印其到 stdout,因此看到holy打印
2 第二个cycle开始,读入 aa# 到pattern space,满组 /aa/,/cc/ 地址过滤,命令n起效,效果1:Write the pattern space to standard output if the default output has not been suppressed,意图打印 该行到stdout,但是被 -n 参数屏蔽,| 随后n命令的效果2 replace the pattern space with the next line of input-- 替换pattern space下一行 bb# ,| 之后第二个命令p,打印出 bb#
3 第三个cycle开始,注意此时sed不是读取第 bb#, 而是读取 cc#,说明在第二个cycle中,n的效果2消耗了行。并影响了下一个cycle。 也可以理解为 input里面的东西只允许被“拿出”一次, cursor是始终往前的!  显示,cycle3打印出 dd#4 cycle 4 打印 shit,如 cycle1


;与 -e, ; 分隔的命令对对 pattern space中的"一行"(引号表示实际可能影响了几行,如N命令) 做连续处理。 而 -e,则是对文本处理完一遍之后,接着开始下一轮,如:
sed -e 's/#.*//' -e '/^$/ d'
Removing comments and blank lines takes two commands. The first removes every character from the "#" to the end of the line, and the second deletes all blank lines.


(这些玩意好像使用用不上啊,理解起来还挺费劲....)

------------------------------------------------------------------

references:
1 http://pubs.opengroup.org/onlinepubs/007908799/xcu/sed.html
  这篇简要而基础! 非常棒!
  附带扩展:unix使用工具的参数规则:
  http://pubs.opengroup.org/onlinepubs/007908799/xbd/utilconv.html#usg
  看完这个也许对阅读man手册有帮助

2 中文参考手册:
  http://www.tsnc.edu.cn/tsnc_wgrj/doc/sed.htm

3 一个更详细的tutorial  e文
  http://www.grymoire.com/Unix/Sed.html#uh-64
  详细,浅显易懂!

4 http://sed.sourceforge.net/sed1line_zh-CN.html
  这里面提供了一些示例,解释不清楚(但是正确),考验自己理解的好地方!

分享到:
评论

相关推荐

    Sed学习笔记

    Sed学习笔记

    linux shell Sed学习笔记

    linux shell Sed学习笔记.相信你看完会明白的

    sed学习笔记

    sed学习笔记,很好的总结,在其它地方淘到的,和大家分享

    sed与awk(第二版)学习笔记

    sed与awk(第二版)学习笔记

    sed高级用法学习笔记和实例

    根据个人平时多年的学习积累,总结的sed所有用法,包括sed中删除,修改,添加,插入,合并,交换,读入,替换,跳转,标签,转换等高级用法!

    一些sed命令

    压缩包里包含有一个word文档,说我平时使用sed命令的记录,一个学习笔记,还有一个sed使用手册。sed是个强大的流编辑器,灵活使用可以减少很多麻烦。

    sed命令详解 快速入门

    sed用了文本处理,功能强大 本文档是我学习的一些记录,也用于我以后的查询,很多命令和用法很久不用就容易忘记。 读了本文档希望能给予你快速上手sed命令。

    Linux学习笔记【博文整理系列】

    学习linux的笔记,发到博文了,鉴于文档方便保存和查阅,发出来,免费的哈 博文来自http://blog.csdn.net/wklken,共12篇,类似手册,可快速练习和查阅,容易上手 文档列表: Linux笔记——vim常用操作及扩展补充...

    shell基础+sed+awk的使用

    学习shell时做的笔记,包含sed,awk,sort,uniq的使用

    老男孩老师-Linux正则表达式实战 学习笔记

    根据老男孩老师视频教程《Linux正则表达式实战》的精彩讲解,进行了学习笔记记录,主要讲解Linux三剑客中的grep用法、捎带sed的讲解笔记。 学习笔记进行了规整,相信Linux运维工作者能够一目了然笔记的内容。 (1...

    sed 与 awk(第二版)

     本笔记参考书籍《sed 与 awk(第二版)》书中的重点部分基本都已记录在笔记中。  笔记摘录了书中较多示例,以及来自网络的示例辅助学习。  笔记中的示例已制作成文件存放在 testfile 文件夹中,可上传至 ...

    Linux的常用命令cat、sed、zip等用法,以及shell编程的基本语法,以及makefile编写方式等等

    这个是我在学习Linux的时候做的思维导图笔记,主要是Linux的一些基础知识。免费提供下载,学习分享。 该文档分为以下几个部分: 1.shell语法:包含格式化输入输出、数组定义、变量定义方法、运算方法、流程控制、...

    Shell脚本学习笔记

    4.6.4 记录消息 96 第5章 脚本控制 97 5.1 处理信号 97 5.1.1 Linux信号回顾 97 5.1.2 生成信号 97 5.1.3捕获信号 99 5.1.4捕获脚本退出 99 5.1.4移除捕获 100 5.2 以后台模式运行脚本 100 5.3 作业控制 102 5.3.1...

    《Unix Shell 实例精解》学习笔记

    《Unix Shell 实例精解》学习笔记 第一章 关于UNIX Shell的介绍 第2章 UNIX工具箱 第3章 grep家族 第4章 流编辑器(sed) 第5章 awk 实用程序:awk作为一种UNIX工具 第6章 awk实用程序:awk编程结构 ...

    shell脚本学习课件笔记整理.rar

    shell从入门到应用,也包含AWK sed等操作,提供课件以及各种完整脚本以便学习。此课件视频来源于B站,链接为:https://www.bilibili.com/video/BV1st411N7WS?from=search&seid=5240570590491899209。

    《unix_shell_实例精解》学习笔记

    shell编程宝典非常实用的shell编程开发资料,详细的sed,awk。grep的用法。

    guo-cheng#Learning-notes-2021#Linux学习笔记1

    第二列按数值大小排序 第二列相同的再按第一列的字母顺序的逆序排序(-r) 第一步所有行变为一行,这一步使用tr 是因为tr里面可以直接识别换行符,而sed不可以

    bash-study:shell学习笔记

    BASH STUDY这个项目目的在于学习 bash.#说明由于初学shell,所以进程管理和系统级shell 没有记录。#历史2014.3,读了 《学习 bash》第二版。2014.3.14,建立目录框架2014.3.28,增加了 awk,windows bat, python 脚本。...

Global site tag (gtag.js) - Google Analytics