sed(stream editor)是一款高效的流编辑器,它一次只处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后把缓冲区的内容送往屏幕,接着处理下一行,这样不断重复,直到文件末尾。sed处理过的文件内容并没有改变,除非你使用重定向存储输出。

sed命令的使用规则

命令格式如下:

  1. sed [option] 'command' input_file

其中option是可选的,常用的option有如下几种:

  1. -n 使用安静(silent)模式,只列出经过sed特殊处理的那一行(或者动作)内容; -e 直接在指令列模式上进行 sed 的动作编辑; -f 直接将 sed 的动作写在一个文件内, -f filename 则可以执行filename内的sed命令; -r sed命令支持扩展的正则表达式(默认是基础正则表达式); -i 直接修改读取的文件内容,而不是由屏幕输出;

常用的命令有以下几种:

  1. a \: append追加字符串,可将其后的字符加在所选择内容的后面 c \: 取代/替换字符串,可将其后内容替换至所选内容 d delete删除,该命令会将当前选中的行删除 i \: insert插入字符串,可将其后内容插入至所选内容前 p print即打印,该命令会打印当前选择的行到屏幕上 s 替换,通常s命令的用法是这样的:12s/old/new/g,将old字符串替换成new字符串

命令示例

假设有一个本地文件test.txt,文件内容如下:

  1. [root@linuxprobe ~]$ cat test.txt this is first line this is second line this is third line this is fourth line this fifth line happy everyday end

本节将使用该文件详细演示每一个命令的用法。

a命令
  1. [root@linuxprobe ~]$ sed '1a \add one' test.txt this is first line add one this is secondline this is third line this is fourth line this is fifth line happy everyday end

本例命令部分中的1表示第一行,同样的第二行写成2,第一行到第三行写成1,3,用$表示最后一行,比如2,$表示第二行到最后一行中间所有的行(包含第二行和最后一行)。
本例的作用是在第一行之后增加字符串”add one”,从输出可以看到具体效果。

  1. [root@linuxprobe ~]$ sed '1,$a \add one' test.txt this is first line add one this is second line add one this is third line add one this is fourth line add one this is fifth line add one happy everyday add one end add one

本例表示在第一行和最后一行所有的行后面都加上”add one”字符串,从输出可以看到效果。

  1. [root@linuxprobe ~]$ sed '/first/a \add one' test.txt this is first line add one this is secondline this is third line this is fourth line this is fifth line happy everyday end

本例表示在包含”first”字符串的行的后面加上字符串”add one”,从输出可以看到第一行包含first,所以第一行之后增加了”add one”

  1. [root@linuxprobe ~]$ sed '/^ha.*day$/a \add one' test.txt this is first line this is secondline this is third line this is fourth line this is fifth line happy everyday add one end

本例使用正则表达式匹配行,^ha.*day$表示以ha开头,以day结尾的行,则可以匹配到文件的”happy everyday”这样,所以在该行后面增加了”add one”字符串。

i命令

i命令使用方法和a命令一样的,只不过是在匹配的行的前面插入字符串,所以直接将上面a命令的示例的a替换成i即可,在此就不啰嗦了。

c命令
  1. [root@linuxprobe ~]$ sed '$c \add one' test.txt this is first line this is secondline this is third line this is fourth line this is fifth line happy everyday add one

本例表示将最后一行替换成字符串”add one”,从输出可以看到效果。

  1. [root@linuxprobe ~]$ sed '4,$c \add one' test.txt this is first line this is secondline this is third line add one

本例将第四行到最后一行的内容替换成字符串”add one”。

  1. [root@linuxprobe ~]$ sed '/^ha.*day$/c \replace line' test.txt this is first line this is secondline this is third line this is fourth line this is fifth line replace line end

本例将以ha开头,以day结尾的行替换成”replace line”。

d命令
  1. [root@linuxprobe ~]$ sed '/^ha.*day$/d' test.txt this isfirst line this issecond line this isthird line this isfourth line this isfifth line end

本例删除以ha开头,以day结尾的行。

  1. [root@linuxprobe ~]$ sed '4,$d' test.txt thisis first line thisis second line thisis third line

本例删除第四行到最后一行中的内容。

p命令
  1. [root@linuxprobe ~]$ sed -n '4,$p' test.txt thisis fourth line thisis fifth line happy everyday end

本例在屏幕上打印第四行到最后一行的内容,p命令一般和-n选项一起使用。

  1. [root@linuxprobe ~]$ sed -n '/^ha.*day$/p' test.txt happy everyday

本例打印以ha开始,以day结尾的行。

s命令

实际运用中s命令式最常使用到的。

  1. [root@linuxprobe ~]$ sed 's/line/text/g' test.txt this isfirst text this issecond text this isthird text this isfourth text this isfifth text happy everyday end

本例将文件中的所有line替换成text,最后的g是global的意思,也就是全局替换,如果不加g,则只会替换本行的第一个line。

  1. [root@linuxprobe ~]$ sed '/^ha.*day$/s/happy/very happy/g' test.txt this isfirst line this issecond line this isthird line this isfourth line this isfifth line very happy everyday end

本例首先匹配以ha开始,以day结尾的行,本例中匹配到的行是”happy everyday”这样,然后再将该行中的happy替换成very happy。

  1. [root@linuxprobe ~]$ sed 's/\(.*\)line$/\1/g' test.txt thisis first thisis second thisis third thisis fourth thisis fifth happy everyday end

这个例子有点复杂,先分解一下。首先s命令的模式是s/old/new/g这样的,所以本例的old部分即\(.*\)line$,sed命令中使用\(\)包裹的内容表示正则表达式的第n部分,序号从1开始计算,本例中只有一个\(\)所以\(.*\)表示正则表达式的第一部分,这部分匹配任意字符串,所以\(.*\)line$匹配的就是以line结尾的任何行。然后将匹配到的行替换成正则表达式的第一部分(本例中相当于删除line部分),使用\1表示匹配到的第一部分,同样\2表示第二部分,\3表示第三部分,可以依次这样引用。比如下面的例子:

  1. [root@linuxprobe ~]$ sed 's/\(.*\)is\(.*\)line/\1\2/g' test.txt this first this second this third this fourth this fifth happy everyday end

正则表达式中is两边的部分可以用\1和\2表示,该例子的作用其实就是删除中间部分的is。

本文转载自:http://www.linuxprobe.com/high-efficiency-and-easy-sed-command.html

更多Linux干货请访问:http://www.linuxprobe.com/

高效而轻松的sed命令的更多相关文章

  1. 运用sed命令高效地删除文件的特定行

    运用 sed 命令高效地删除文件的特定行 正常来说,我们想要删除文件中的某些行内容,一般都是先打开这个文件,然后找到要删除的内容,再然后选中这些行并按删除键进行删除,这在数据量很少时是没有问题的.但是 ...

  2. 轻松学会文本处理工具之二 linux sed命令

    sed命令的语法格式: sed的命令格式: sed [option]  'sed command'filename sed的脚本格式:sed [option] -f  'sed  script'fil ...

  3. Shell之sed命令

    sed用于一次性处理所有的编辑任务,尤为高效,为用户节省了大量的时间,sed适用于以下三种场合: 1.编辑相对交互文本编辑器而言太大的文件: 2.编辑命令太复杂,在交互式文本编辑器中难以输入的情况: ...

  4. Linux实战教学笔记12:linux三剑客之sed命令精讲

    第十二节 linux三剑客之sed命令精讲 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,前言 我们都知道,在Linux中一切皆文件,比如配置文件,日志文件,启动文件 ...

  5. sed 命令详解

    sed 用于筛选和转换文本的流编辑器 描述: sed是一个流编辑器,流编辑器对一个输入流执行基本的文本转换(输入流来自文件或者管道行).虽然在某些方面类似于很多可运行脚本的编辑器,但是sed的工作方式 ...

  6. 文本处理三剑客之sed命令

    第十八章.文本处理三剑客之sed命令 目录 sed介绍 sed命令常用选项 sed常用编辑命令 sed使用示例 sed高级语法 18.1.sed简介 sed全名stream editor,流编辑器,s ...

  7. linux shell 用sed命令在文本的行尾或行首添加字符

    转自 http://www.cnblogs.com/aaronwxb/archive/2011/08/19/2145364.html 昨天写一个脚本花了一天的2/3的时间,而且大部分时间都耗在了sed ...

  8. linux sed命令详解

    简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ...

  9. sed命令详解

    搜索 纠正错误  添加实例 sed 功能强大的流式文本编辑器 补充说明 sed 是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时 ...

随机推荐

  1. javascript之document对象

    一.修改网页元素 当使用document提供的方法和Element的属性得到网页元素之后,就可以对元素的内容进行修改,如下例所示的“全选/全不选”的实现. 例3-17 <html> < ...

  2. 用while循环语句计算1!+2!+……20!之和

    package nothh; public class mmm { public static void main(String[] args) { // TODO Auto-generated me ...

  3. batch insert 1 million datas into mysql

    最近尝试插入1百万条数据进db,以mysql为例. 1. 顺序insert 先写了个无脑的for循环作为base-line,插1万条耗时1m53s,根本不敢插1百万. foreach(var stud ...

  4. MyEclipse的 at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>错误解决办法

    我们使用myeclipse自动部署web项目时会设置服务器的位置,而部署的相关配置会存储在myeclipse工作目录下的“/.metadata/.plugins/org.eclipse.core.ru ...

  5. WP8.1 Study12:文件压缩与Known Folder(包含SD卡操作)

    一.文件压缩 当应用程序保存和加载数据,它可以使用压缩. 1.使用 Windows.Storage.Compression.Compressor 压缩,获得一个Compressor stream. v ...

  6. nginx初识

  7. iOS中属性Property的常用关键字的使用说明

    属性关键字的作用 现在我们iOS开发中,基本都是使用ARC(自动引用计数)技术,来编写我们的代码.因此在属性property中我们经常使用的关键字有strong,weak,assign,copy,no ...

  8. PAT 06-3 单词长度

    此题为简化版,题设为“以'.'结尾”.“单词与语言无关”.三种情况下的输入输出为 . -> 无输出 a     b. ->1 1(无空格) a   b  . ->1 1(无空格) 程 ...

  9. python3中输出不换行

    python2中输出默认是换行的,为了抑制换行,是这么做的: print x, 到了python3中,print变成一个函数,这种语法便行不通了.用2to3工具转换了下,变成这样了: print(x, ...

  10. 使用struts2的<s>标签出错

    15:org.apache.struts2.views.jsp.ActionTag 16:JSP 17:18:19:executeResult Server: Resin/3.1.4a Content ...