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

使用语法

sed命令的使用规则是这样的:

1
sed [option] 'command' input_file

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

  • -n 使用安静(silent)模式(想不通为什么不是-s)。在一般sed的用法中,所有来自stdin的内容一般都会被列出到屏幕上。但如果加上-n参数后,则只有经过sed特殊处理的那一行(或者动作)才会被列出来;
  • -e 直接在指令列模式上进行 sed 的动作编辑;
  • -f 直接将 sed 的动作写在一个文件内, -f filename 则可以执行filename内的sed命令;
  • -r 让sed命令支持扩展的正则表达式(默认是基础正则表达式);
  • -i 直接修改读取的文件内容,而不是由屏幕输出。

    常用的命令有以下几种:

  • a \: append即追加字符串, a \的后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选择的行的后面都加上字符串s;

  • c \: 取代/替换字符串,c \后面跟上字符串s(多行字符串可以用\n分隔),则会将当前选中的行替换成字符串s;
  • d: delete即删除,该命令会将当前选中的行删除;
  • i \: insert即插入字符串,i \后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选中的行的前面都插入字符串s;
  • p: print即打印,该命令会打印当前选择的行到屏幕上;
  • s: 替换,通常s命令的用法是这样的:1,2s/old/new/g,将old字符串替换成new字符串

命令示例

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

[qifuguang@winwill~]$ 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
2
3
4
5
6
7
8
9
[qifuguang@winwill~]$ sed '1a \add one' test.txt
this is first line
add one
this is second line
this is third line
this is fourth line
this is fifth line
happy everyday
end

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[qifuguang@winwill~]$ 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
2
3
4
5
6
7
8
9
[qifuguang@winwill~]$ sed '/first/a \add one' test.txt
this is first line
add one
this is second line
this is third line
this is fourth line
this is fifth line
happy everyday
end

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

1
2
3
4
5
6
7
8
9
[qifuguang@winwill~]$ sed '/^ha.*day$/a \add one' test.txt
this is first line
this is second line
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
2
3
4
5
6
7
8
[qifuguang@winwill~]$ sed '$c \add one' test.txt
this is first line
this is second line
this is third line
this is fourth line
this is fifth line
happy everyday
add one

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

1
2
3
4
5
[qifuguang@winwill~]$ sed '4,$c \add one' test.txt
this is first line
this is second line
this is third line
add one

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

1
2
3
4
5
6
7
8
[qifuguang@winwill~]$ sed '/^ha.*day$/c \replace line' test.txt
this is first line
this is second line
this is third line
this is fourth line
this is fifth line
replace line
end

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

d命令

1
2
3
4
5
6
7
[qifuguang@winwill~]$ sed '/^ha.*day$/d' test.txt
this is first line
this is second line
this is third line
this is fourth line
this is fifth line
end

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

1
2
3
4
[qifuguang@winwill~]$ sed '4,$d' test.txt
this is first line
this is second line
this is third line

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

p命令

1
2
3
4
5
[qifuguang@winwill~]$ sed -n '4,$p' test.txt
this is fourth line
this is fifth line
happy everyday
end

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

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

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

s命令

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

1
2
3
4
5
6
7
8
[qifuguang@winwill~]$ sed 's/line/text/g' test.txt
this is first text
this is second text
this is third text
this is fourth text
this is fifth text
happy everyday
end

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

1
2
3
4
5
6
7
8
[qifuguang@winwill~]$ sed '/^ha.*day$/s/happy/very happy/g' test.txt
this is first line
this is second line
this is third line
this is fourth line
this is fifth line
very happy everyday
end

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

1
2
3
4
5
6
7
8
[qifuguang@winwill~]$ sed 's/\(.*\)line$/\1/g' test.txt
this is first
this is second
this is third
this is fourth
this is fifth
happy everyday
end

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

1
2
3
4
5
6
7
8
[qifuguang@winwill~]$ 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://qifuguang.me/2015/09/21/sed命令详解/

sed命令详解 (转载)的更多相关文章

  1. Linux学习之sed命令详解

    概述 sed是stream editor的简称,也就是流编辑器.它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区 ...

  2. sed命令详解 vim高级技巧 shell编程上

    第1章 sed命令详解 1.1 查找固定的某一行 1.1.1 awk命令方法 [root@znix ~]# awk '!/oldboy/' person.txt 102,zhangyao,CTO 10 ...

  3. 【转】【Linux】sed命令详解

    sed命令详解 sed是stream editor的简称,也就是流编辑器.它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令 ...

  4. 转 linux之sed命令详解

    http://jingyan.baidu.com/article/fec4bce2228f60f2618d8bb0.html sed  编辑裁剪文件命令 sed -i "s/\/db\/te ...

  5. Linux shell sed 命令详解

    详细的sed命令详解,请参考https://my.oschina.net/u/3908182/blog/1921761 sed命令常见用途 查找关键词做全局替换 查找某行的关键词做替换 查找关键字所在 ...

  6. 【文本处理命令】之sed命令详解

    sed行处理命令详解 一.简介 sed命令是一种在线编辑器.一个面向字符流的非交互式编辑器,也就是说sed不允许用户与它进行交互操作.sed是按行来处理文本内容的,它一次处理一行内容.处理时,把当前处 ...

  7. vmstat 命令详解 转载

    vmstat 命令详解   procs:r-->在运行队列中等待的进程数b-->在等待io的进程数w-->可以进入运行队列但被替换的进程 memoyswap-->现时可用的交换 ...

  8. linux sar 命令详解(转载)

    linux sar 命令详解 2013-04-01 11:05 [小 大] 来源: 开源中国社区 评论: 0 分享至: 百度权重查询 词库网 网站监控 服务器监控 SEO监控 手机游戏 iPhone游 ...

  9. 【转载】sed命令详解

    [转载自]http://www.cnblogs.com/edwardlost/archive/2010/09/17/1829145.html   sed -i  把后面的操作后的文本输出回原文本   ...

随机推荐

  1. 什么是AngularJs?特点是什么?和JQuery什么区别和联系

    什么是AngularJs? AngularJs是js框架,集中操作数据,不关注Dom操作,适用于以数据操作为主的的SPA(单页应用). 它的特点 采用MVC模型 双向数据绑定 依赖注入 模块化 与jQ ...

  2. Java对象finalize()方法

    Java提供了一种在对象即将被销毁时执行资源释放的方法.在Java中创建对象,但是不能销毁对象.JVM运行一个称为垃圾收集器的低优先级特殊任务来销毁不再引用的所有对象. 垃圾回收器给我们一个机会,在对 ...

  3. CSRF如何防御

    总结网上所说,细细的归纳下 CSRF利用的时网站对用户网页浏览器的信任.在受害人不知情的情况下以 受害人的名义伪造请求发送给攻击者的站点. 1.首先XSS漏洞先防护好(一般是通过过滤器更改特殊字符) ...

  4. Apache 2.4.12 64位+Tomcat-8.0.32-windows-x64负载集群方案

    上次搞了Apache 2.2的集群方案,但是现在自己的机器和客户的服务器一般都是64位的,而且tomcat已经到8了.重新做Apache 2.4.12 64位+Tomcat-8.0.32-window ...

  5. ActiveX (ocx) 控件 在vs2010 上debug 的方法

    1.在项目的属性中配置Debug,设置调试选项卡中的“命令”.“命令参数”.“工作目录”,“命令”为IE的路径,“命令参数”为自己写的htm页面路径(因htm中未配置ocx路径,所以直接把htm放在了 ...

  6. JUnit中Assert简单介绍

    junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...

  7. Redis哨兵机制(sentinel)

    1.简介: 1.是什么: Redis-Sentinel是Redis官方推荐的高可用(HA)方案,当用Reids 做master-slave高可用方案时,假如master宕机了,redis本身(包括它的 ...

  8. 企业级NginxWeb服务优化实战(上)

    企业级NginxWeb服务优化实战(上) 1. Nginx基本安全优化 1.1 调整参数隐藏Nginx软件版本号信息 一般来说,软件的漏洞都和版本有关,这个很像汽车的缺陷,同一批次的要有问题就都有问题 ...

  9. HDU-5215 Cycle 无向图判奇环偶环

    题意:给一个无向图,判断这个图是否存在奇环和偶环. 解法:网上有一种只用dfs就能做的解法,但是我不太理解. 这里用的是比较复杂的.首先奇环很简单可以用二分图染色判断.问题是偶环怎么判断?这里我们想, ...

  10. redis配置篇

    配置 Redis的配置信息在/etc/redis/redis.conf下. 查看 sudo vi /etc/redis/redis.conf 核心配置选项 绑定ip:如果需要远程访问,可将此⾏注释,或 ...