首页 > 其他分享 >无涯教程-Sed - 特殊字符

无涯教程-Sed - 特殊字符

时间:2023-11-21 21:31:53浏览次数:35  
标签:Coelho Tolkien 无涯 Pages Paulo Sed Martin George 特殊字符

SED提供了两个特殊字符,它们被视为命令。本章说明了这两个特殊字符的用法。

= 命令

" ="命令显示行号。以下是" ="命令的语法:

[/pattern/]= 
[address1[,address2]]=

= 命令将行号及其内容写入标准输出流。以下示例说明了这一点。

[jerry]$sed '=' books.txt 

执行上述代码后,您将得到以下输出:

1 
1) A Storm of Swords, George R. R. Martin, 1216 
2 
2) The Two Towers, J. R. R. Tolkien, 352 
3 
3) The Alchemist, Paulo Coelho, 197 
4 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5 
5) The Pilgrimage, Paulo Coelho, 288 
6 
6) A Game of Thrones, George R. R. Martin, 864

让无涯教程打印行号和前四行的内容。下面的命令显示前四行带有行号,其余的不打印行号。

[jerry]$sed '1, 4=' books.txt 

执行上述代码后,您将得到以下输出:

1 
1) A Storm of Swords, George R. R. Martin, 1216 
2 
2) The Two Towers, J. R. R. Tolkien, 352 
3 
3) The Alchemist, Paulo Coelho, 197 
4 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

此外,当模式匹配成功时,可以指示SED打印行号。下面的示例打印包含模式" Paulo"的行号。

[jerry]$sed '/Paulo/=' books.txt 

执行上述代码后,您将得到以下输出:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

您能猜出下面的SED命令做什么吗?

[jerry]$sed -n '$=' books.txt

执行上述代码后,您将得到以下输出:

6 

在命令部分,使用" $="来打印最后一行的行号及其内容,但是,还提供了 -n 标志,该标志禁止模式缓冲区的默认打印,因此,仅显示最后一个行号。

& 命令

SED支持特殊字符&。 每当模式匹配成功时,此特殊字符都会存储匹配的模式。 

book.txt文件中的每一行都有编号。在每一行的开头添加书号。以下示例说明了这一点。

[jerry]$sed 's/[[:digit:]]/Book number &/' books.txt

执行上述代码后,您将得到以下输出:

Book number 1) A Storm of Swords, George R. R. Martin, 1216 
Book number 2) The Two Towers, J. R. R. Tolkien, 352 
Book number 3) The Alchemist, Paulo Coelho, 197 
Book number 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
Book number 5) The Pilgrimage, Paulo Coelho, 288 
Book number 6) A Game of Thrones, George R. R. Martin, 864 

这个示例很简单。 

     首先,无涯教程搜索第一个出现的数字,即行号(这就是使用[[:digit:]]的原因),SED会自动将匹配存储在特殊字符&中。

     在第二步中,在每个匹配的模式之前(即每行之前)插入书号。

再举一个示例。 在book.txt文件中,最后一位数字表示该书的页数。 在此之前添加“Pages=”。 为此,找到该数字的最后一次出现,并将其替换为“ Pages=&”。 在这里,&存储匹配的模式,即页数

[jerry]$sed 's/[[:digit:]]*$/Pages=&/' books.txt 

执行上述语法后,将得到以下输出:

1) A Storm of Swords, George R. R. Martin, Pages=1216 
2) The Two Towers, J. R. R. Tolkien, Pages=352 
3) The Alchemist, Paulo Coelho, Pages=197 
4) The Fellowship of the Ring, J. R. R. Tolkien, Pages=432 
5) The Pilgrimage, Paulo Coelho,Pages=288 
6) A Game of Thrones, George R. R. Martin, Pages=864 

参考链接

https://www.learnfk.com/sed/sed-special-characters.html

标签:Coelho,Tolkien,无涯,Pages,Paulo,Sed,Martin,George,特殊字符
From: https://blog.51cto.com/u_14033984/8506681

相关文章

  • 无涯教程-Sed - 模式范围
    本章介绍了SED如何处理PatternRange(模式范围)PatternRange可以是简单的文本或复杂的正则表达式。下面的示例打印作者Paulo的所有书籍。[jerry]$sed-n'/Paulo/p'books.txt执行上述代码后,您将得到以下输出:3)TheAlchemist,PauloCoelho,1975)ThePilgrimage,Paul......
  • 无涯教程-Sed - 分支操作
    可以使用 t命令创建分支。仅当上一个命令成功时,t命令才会跳转到标签。让无涯教程以与上一章相同的示例为例,但是现在不打印单个连字符(-),而是打印四个连字符。以下示例说明了t命令的用法。[jerry]$sed-n'h;n;H;xs/\n/,/:Loop/Paulo/s/^/-//----/!tLoopp'bo......
  • 无涯教程-Sed - 循环语句
    与其他编程语言一样,SED也提供了循环和分支函数来控制执行流程。在本章中,无涯教程将探索更多有关如何在SED中使用循环和分支的信息。SED中的循环的工作方式类似于goto语句。SED可以跳到标签所标签的行,然后继续执行其余命令。在SED中,可以如下定义label :label:start:end......
  • 学习笔记:A Survey on Large Language Model basedAutonomous Agents
    挑选了自己感兴趣的部分整理了一下。目录ASurveyonLargeLanguageModelbasedAutonomousAgents1LLM-AAConstruction1.1ArchitectureDesign2LLM-AAApplication3LLM-AAEvaluation4ChallengeASurveyonLargeLanguageModelbasedAutonomousAgents北大高林学院的......
  • 无涯教程-Sed - 简介
    sed(意为流编辑器,源自英语“streameditor”的缩写)是一个使用简单紧凑的编程语言来解析和转换文本Unix实用程序。sed由贝尔实验室的LeeE.McMahon于1973年至1974年开发,并且现在大多数操作系统都可以使用。sed基于交互式编辑器ed(“editor”,1971)和早期qed(“quickeditor”,1......
  • 无涯教程-Ruby Profiler −函数
    在大多数情况下,您可以通过消除瓶颈来提高慢速程序的性能。探查器是找到瓶颈的工具。为了向您的Ruby程序添加配置文件,您需要首先使用命令行options-rprofile加载Profile库。使用语法$ruby-rprofile[programfile][arguments]Example这是从hello.rb文件生成的输......
  • 【略读论文|时序知识图谱补全】DREAM: Adaptive Reinforcement Learning based on Att
    会议:SIGIR,时间:2023,学校:苏州大学计算机科学与技术学院,澳大利亚昆士兰布里斯班大学信息技术与电气工程学院,Griffith大学金海岸信息通信技术学院摘要:原因:现在的时序知识图谱推理方法无法生成显式推理路径,缺乏可解释性。方法迁移:由于强化学习(RL)用于传统知识图谱上的多跳推理开......
  • 无涯教程-Interactive Ruby (irb) −函数
    交互式Ruby或irb是Ruby附带的交互式编程环境。它是由石冢启十先生撰写的。使用语法要调用它,请在shell或命令提示符下键入irb,然后开始输入Ruby语句和表达式。使用退出或退出退出irb。$irb[.rb][options][programfile][arguments]这是options的完整列表-Sr.No.Comma......
  • 无涯教程-RubyGems −函数
    RubyGems是Ruby的软件包实用程序,它可以安装Ruby软件包并使它们保持最新。使用语法$gemcommand[arguments...][options...]Example检查是否安装了RubyGems-$gem--version0.9.0RubyGems命令这是RubyGems所有重要命令的列表-Sr.No.Command&Description1build......
  • vue3_Extraneous non-props attributes (class) were passed to component but could
    今天的开发中发现了这个问题Extraneousnon-propsattributes(class)werepassedtocomponentbutcouldnotbeautomaticallyinheritedbecausecomponentrendersfragmentortextrootnodes.原因:是因为vue3中允许在<template>中不设置根节点,所以我在某个页面中......