`

sed系列:pattern buffer和hold buffer

 
阅读更多
As its name implies, sed hold buffer is used to save all or part of the sed pattern space for subsequent retrieval.
The contents of the pattern space can be copied to the hold space, then back again.
No operations are performed directly on the hold space. sed provides a set of hold and get functions to handle these movements.

Sed h function
The h (hold) function copies the contents of the pattern space into a holding area (also called as sed hold space), destroying any previous contents of the holding area.

Sed H function
The H function appends the contents of the pattern space to the contents of the holding area. The former and new contents are separated by a newline.

Sed g function
The g function copies the contents of the holding area into the pattern space, destroying the previous contents of the pattern space.

Sed G function
The G function appends the contents of the holding area to the contents of the pattern space. The former and new contents are separated by a newline. The maximum number of addresses is two.

Sed x function
The exchange function interchanges the contents of the pattern space and the holding area. The maximum number of addresses is two.
创建示例文件
$ cat thegeekstuff.txt
#Linux
        Administration
        Scripting
        Tips and Tricks

#Windows
        Administration

#Database
        Mysql
        Oracle
        Queries
        Procedures


例1:给文件每行之间加一个空行
$sed 'G' thegeekstuff.txt
#Linux

        Administration

        Scripting

        Tips and Tricks

#Windows

        Administration

#Database

        Mysql

        Oracle

        Queries

        Procedures

$
例2:以相反的顺序输出文件内容,以行为单位
$sed -n '1!G;h;$p' thegeekstuff.txt
        Procedures
        Queries
        Oracle
        Mysql
#Database

        Administration
#Windows

        Tips and Tricks
        Scripting
        Administration
#Linux
$
1.First line will be placed into the hold space as it is.
2.From the 2nd line onwards, just append the hold space content with the pattern space. (Remember 2nd line is in pattern space, and 1st line is in hold space).
3.Now 1st and 2nd line got reversed and move this to the hold space.
4.Repeat the above steps till last line.
5.Once the last line is reached, just append the hold space content with the pattern space and print the pattern space.
例3:模式匹配输出一段内容
$ sed -e '/./{H;$!d;}' -e 'x;/Administration/!d' thegeekstuff.txt

#Linux
        Administration
        Scripting
        Tips and Tricks

#Windows
        Administration
$
1.Till the empty line comes, keep appending the non empty lines into the hold space
2.When empty line comes i.e paragraph ends, exchange the data between pattern and hold space. So that whole paragraph will be available in pattern space.
Check if pattern “Administration” is available, if yes don’t delete it i.e print the pattern space
例4:输出模式匹配的前一行
$ sed -n '/Mysql/{g;1!p;};h' thegeekstuff.txt
#Database

For each cycle, place the line into hold buffer, if it doesn’t match with the pattern “Mysql”.
If the line matches with the pattern, get the data from the hold space(previous line) using g command and print it.
In case, if the first line matches with the pattern “Mysql”,anyway hold space will be empty.(There is no previous line to the first line).So first line should not get printed(1!p)

例5:删除每个段落的最后一行
$ sed -n -e '/^$/{x;d}' -e '/./x;p' thegeekstuff.txt

#Linux
        Administration
        Scripting

#Windows

#Database
        Mysql
        Oracle
        Queries

If the line is not empty,then exchange the line between pattern and hold space. So first line will be placed in the hold space.
When next non empty line comes, exchange the pattern space and hold space, and print the pattern space. i.e first non empty line will be printed and 2nd line goes to hold. And in next cycle, 2nd non empty line is printed when 3rd line goes to hold and goes on like this.
When empty line comes (previous line to the empty line will be available in hold buffer) just exchange pattern and hold space, and delete the line (last line of the paragraph) and start the next cycle.

例6:对于每一行,把上一行的内容添加到下一行的末尾
$sed 'H;x;s/^\(.*\)\n\(.*\)/\2\1/' thegeekstuff.txt
#Linux
        Administration#Linux
        Scripting        Administration
        Tips and Tricks        Scripting
        Tips and Tricks
#Windows
        Administration#Windows
        Administration
#Database
        Mysql#Database
        Oracle        Mysql
        Queries        Oracle
        Procedures        Queries
$
Place the first line in Hold buffer.
When the second line comes, append to Hold space (first line)
Then exchange pattern and hold buffer. So now pattern space will have first and second line separated by \n, Hold space will have only second line.
So interchange the lines in the pattern space.
The above steps happens till the end of the file

例7:把标签放到以一行内容的行头
$sed '
> /^#/{
> h
> d
> }
> G
> s/^\(.*\)\n#\(.*\)/\2 \1/' thegeekstuff.txt
Linux         Administration
Linux         Scripting
Linux         Tips and Tricks
Linux
Windows         Administration
Windows
Database         Mysql
Database         Oracle
Database         Queries
Database         Procedures
$
1.When the first line of a block is met (beginning with #)
keep that line to the Hold Space via command `h’
Then delete using ‘d’ to start another cycle.
2.For the rest lines of a block, Command `G’ appends the tag line from the
Hold Space and substitute command interchanges tag and lines properly.
分享到:
评论

相关推荐

    玩透sed:探究sed原理

    **玩透sed:探究sed原理** `sed`(Stream Editor)是Linux/Unix环境中的一款强大的文本处理工具,它能够对输入流(标准输入、文件或管道)进行读取、处理并输出。`sed`的工作原理基于行处理,逐行读取输入,执行...

    sed安装包:sed-4.2.1-setup.exe

    默认安装即可,会生成GnuWin32文件夹

    玩透sed:探究sed原理.pdf

    sed原理、机制探究,适合sed深入。 文档包含4大篇修炼文章: sed修炼系列(一):花拳绣腿之入门篇 sed修炼系列(二):武功心法(info sed翻译+注解) sed修炼系列(三):sed高级应用之...sed修炼系列(四):sed中的疑难杂症

    sedsed:sed脚本的调试器和代码格式化程序

    网站: : 下载/安装Sedsed可作为pip包提供,只需安装即可: pip install --user sedsed与Python 2.7和Python 3.x兼容许可证:GPLv3以获取每个版本中的更改列表备选:sedsed是单个文件应用程序,因此您也可以下载并...

    sed命令详解

    - **-f script-file, --file=script-file**: 从文件中读取一系列的`sed`脚本命令。这些命令将会按顺序被执行。 - **-i[SUFFIX], --in-place[=SUFFIX]**: 此选项允许直接在原文件上进行编辑。如果提供了后缀`SUFFIX`...

    vimsed:vimsed脚本使vim的行为类似于sed。 句法

    vimsed脚本使vim的行为类似于sed。 句法: vimsed " <vim> " vimsed会将标准输入“管道”到标准输出,首先通过运行击键对其进行修改。 警告:使用临时文件~/vimsedin , ~/vimsedcmd.vim和~/vimsedout 。 例子: ...

    实践大师:UNIX awk和sed编程篇

    在IT领域,UNIX系统以其强大的命令行工具和脚本处理能力而备受推崇,其中awk和sed是两个不可或缺的文本处理工具。这篇“实践大师:UNIX awk和sed编程篇”无疑是一份深入学习这两个工具的宝贵资源。让我们一起探索awk...

    基本的SED命令有大量的SED命令

    本文将详细介绍SED中的四个核心编辑命令:`d`(删除)、`a`(附加)、`i`(插入)和`c`(变更),同时探讨如何在命令脚本中控制执行流程。 #### 二、SED命令语法 在深入讨论各个命令之前,我们先回顾一下SED命令的...

    sedsed工具

    3. **脚本文件**:复杂操作可以写入脚本文件,如`sed -f script.sed file.txt`,`script.sed`包含一系列连续的`sed`命令。 ### 总结 `sedsed`工具在`sed`基础上增强了删除操作的便利性,简化了文本处理流程,尤其...

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

    - **非交互式**:sed不是交互式的编辑器,它不需要用户实时输入命令,而是通过命令行或者脚本文件来执行一系列预定义的编辑操作。 - **行编辑**:sed每次只处理一行文本,因此特别适合处理大型文件,因为它不需要将...

    GNU-Sed-:GNU Sed手册翻译

    #sed, a stream editor ##sed, a stream editor 本文档基于GNU sed 4.2.1版本。 Copyright :copyright: 1998, 1999, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. 本文档遵循自由软件基金会发布的GNU ...

    SEDC:SEDC的作业

    【SEDC:SEDC的作业】是针对学习者在SEDC(Software Engineering Development Center,软件工程开发中心)课程中的一项家庭作业。这个作业可能涵盖了HTML(HyperText Markup Language)的基础知识,HTML是一种用于...

    sed调试器debug

    prompt$ sedsed --help usage: sedsed OPTION [-e sedscript] [-f sedscriptfile] [inputfile] OPTIONS: -f, --file add file contents to the commands to be parsed -e, --expression add the script to the ...

    Shell、awk、sed面试题汇总(无答案).doc

    18. awk 和 sed 命令:可以使用 awk 和 sed 命令来处理数据和文本文件。 19. 正则表达式:可以使用正则表达式来匹配字符串,例如 `/^[^&][a-z-]+[%!]_[0-2]{3}.*$/`。 20. 数字字符串处理:可以使用 shell 命令来...

    sed 高级用法

    - **G (Append Hold Space to Pattern Space)**:将辅助缓冲区的内容追加到模式空间,并添加换行符。 - **g (Copy Hold Space to Pattern Space)**:将辅助缓冲区的内容复制到模式空间。 - **x (Exchange Pattern ...

    sed和awk单行命令比较

    ### sed和awk单行命令比较 本文将对两种常见的文本处理工具——`sed`和`awk`进行详细的对比分析,并通过具体的示例来说明这两种工具在处理文本时的不同之处。 #### 1. 基本介绍 - **sed** (stream editor):是一种...

    sed-4.2.1-setup

    **sed**,全称“Stream Editor”,是Unix和类Unix操作系统中的一款强大文本处理工具。它能够对输入流(标准输入或文件)进行读取、处理并打印,常用于文本转换、替换和过滤任务,广泛应用于自动化脚本和命令行操作中...

    sed2048:sed中实现的2048游戏

    Bash用于为sed脚本提供用户输入和伪随机数。 使用W , A , S , D键进行控制。 运行: $ ./src/2048.sh 通过以下方式禁用彩色输出: $ ./src/2048.sh --no-color 与原始2048游戏的游戏区别: 我所知道的都不是...

    sed and wak 101

    sed -e 's/pattern1/replacement1/g' -e 's/pattern2/replacement2/g' file.txt ``` 这将全局替换文件`file.txt`中的`pattern1`和`pattern2`。 ##### 16. 使用`&`获取匹配的模式 `&` 可以用来引用整个匹配的模式...

Global site tag (gtag.js) - Google Analytics