sed命令

一、替换标记

s/pattern/replacement/flags

默认情况下只会替换每行的首次出现的内容,如果要替换其他位置需要使用flags

1、不使用flag

[root@bogon tmp]# cat data.txt
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
[root@bogon tmp]# sed 's/test/aaa/' data.txt
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
[root@bogon tmp]# cat data.txt
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.

2、使用flags/pattern/replacement/flags

有4种可用的替换标记:

  • 数字,表明新文本将替换第几处模式匹配的地方;
  • g,表明新文本将会替换所有匹配的文本;
  • p,表明原先行的内容要打印出来;
  • w file,将替换的结果写到文件中。

1)数字型,比如数字2,替换第2次出现的内容,不会修改原文件

[root@bogon tmp]# sed 's/test/hhhh/2' data.txt
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.

2)g,替换所有的行,不会修改原文件

[root@bogon tmp]# sed 's/test/aaa/g' data.txt
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.

3)w file,将替换的结果写到新文件中,不会替换原文件

[root@bogon tmp]# sed 's/test/aaa/w aaa.txt' data.txt
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.

查看当前目录,多了文件aaa.txt
[root@bogon tmp]# ls
aaa.txt keyring-nBhNRc orbit-gdm pulse-PDnM11f40MzK VMwareDnD
data.txt keyring-o1ipCa orbit-root pulse-tvQCaCozIrxH

4)p,p替换标记会打印与替换命令中指定的模式匹配的行

[root@bogon tmp]# cat aaa.txt
this is a aaa of the test script.
this is a sss of the test script.
[root@bogon tmp]# sed 's/aaa/test/p' aaa.txt
this is a test of the test script.
this is a test of the test script.
this is a sss of the test script.

这通常会和sed的-n选项一起使用。
$ cat data5.txt
This is a test line.
This is a different line.

$ sed -n 's/test/trial/p' data5.txt
This is a trial line.

-n选项将禁止sed编辑器输出。但p替换标记会输出修改过的行。将二者配合使用的效果就是
只输出被替换命令修改过的行。

二、使用地址

默认情况下,sed命令作用于文本数据的所有行。如果只想将命令作用于特定行或者某些行,则需要使用行寻址。

sed编辑器的2种行寻址方式:

  • 以数字形式表示行区间
  • 用文本模式来过滤出行

[address] command

address {

command1

command2

command3

}

1、数字方式行寻址

[root@localhost tmp]# sed '2s/test/hhh/' aaa.txt
this is a aaa of the test script.
this is a sss of the hhh script.

修改2-4行每行第2次匹配上的内容

[root@localhost tmp]# sed '2,4s/test/hhh/2' data.txt
this is a test of the test script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.

修改第2行至最后一行的匹配的内容

[root@localhost tmp]# sed '2,$s/test/hhh/2' data.txt
this is a test of the test script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.

2、命令组合方式

[root@localhost ~]# sed '2,4{        修改2-4行的内容
> s/test/hhh/2                                修改每行的第2次匹配的内容
> s/a test/1111/                              修改每行的第1次匹配的内容
> }' /tmp/data.txt
this is a test of the test script.
this is 1111 of the hhh script.
this is 1111 of the hhh script.
this is 1111 of the hhh script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
[root@localhost ~]#

3、删除行

$ sed '/number 1/d' data6.txt
This is line number 2.
This is line number 3.
This is line number 4.

sed使用的更多相关文章

  1. sed的应用

    h3 { color: rgb(255, 255, 255); background-color: rgb(30,144,255); padding: 3px; margin: 10px 0px } ...

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

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

  3. 6-2 sed 命令

    1. sed : stream editor,流编辑器 是处理纯ASICC纯文本,按行琢行操作的. 编辑器有两种,行编辑器和全屏编辑器 sed:默认不编辑原文件,仅对模式空间中的数据做处理,而后.处理 ...

  4. 基本shell编程【3】- 常用的工具awk\sed\sort\uniq\od

    awk awk是个很好用的东西,大量使用在linux系统分析的结果展示处理上.并且可以使用管道, input | awk ''  | output 1.首先要知道形式 awk 'command' fi ...

  5. sed awk grep三剑客常用

    sed的常用用法: awk的常用用法: grep的常用用法: 除了列出符合行之外,并且列出后10行. grep -A 10 Exception kzfinance-front.log 除了列出符合行之 ...

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

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

  7. Sed、Awk单行脚本快速参考

    文本间隔: # 在每一行后面增加一空行 sed G awk '{printf("%s\n\n",$0)}' # 将原来的所有空行删除并在每一行后面增加一空行. # 这样在输出的文本 ...

  8. sed awk 样例

    sed [options] '[action]' filename options: -n:一般sed命令会把所有数据都输出到屏幕,如果加入此选项,则只会把经过sed命令处理的行输出到屏幕. -e:允 ...

  9. linux sed命令详解

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

  10. sed命令详解

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

随机推荐

  1. C++的引用与const指针的关系以及各种传递方式

    首先我们知道 const int *p 与 int const *p 是一样的,即 *p 是常量:而 int * const p 跟上面是不一样的,即 p 是常量:我们知道引用只是一个别名,与变量共享 ...

  2. VB命令行参数分隔, 类似C语言中的main(int argc, char* argv[])

    VB6.0为了提供命令行参数的支持,提供了Command()接口,于是通过 Command() 函数可以得到所有传入的参数,但是很不友好的是,VB的所有参数都被合在了一起,成为了一个字符串,当有多个参 ...

  3. hook NtReadVirtualMemory干扰杀软扫描

      信息来源:邪恶八进制信息安全团队(www.eviloctal.com)文章作者:asm(http://www.sbasm.cn) 写了个对抗扫描的东西,跟大家分享!技术含量不高,大牛飘过.一直以来 ...

  4. 黑马程序猿——25,打印流,合并流,对象序列化,管道流,RandomAccessFile

    ------<ahref="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培 ...

  5. MySql(三):MyISAM和InnoDB区别详解

    MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Access Method:有索引的顺序访问方法)所改良.虽然性能极佳,但却有一个缺点 ...

  6. C数组逆序

    一.标准交换模式 /**** *标准交换模式 *实现数组的逆序,原理就是数组的首尾元素进行交换 ***/ #define N 5; int main(){ int array[N] = {15,20, ...

  7. Linux下UTF-8和GB2312互相转换的函数

    #include<iconv.h> #include <stdio.h> #include<iconv.h>using namespace std; int utf ...

  8. Atitit. Java script 多重多重catch语句的实现and Javascript js 异常机制

    Atitit. Java script 多重多重catch语句的实现and Javascript js 异常机制 1. 语法错误(ERROR)和运行期错误(Exception) 1 2. 错误类型判断 ...

  9. ECN

    ECN是工程变更管理,主要用来管理BOM的生效日期,及记录BOM的更改内容:在SAP系统中,需先创建ECN更改号码,凭ECN号码去更改管理BOM

  10. 微软七届MVP桂素伟:移动互联网与职业规划

    原文地址:http://student.csdn.net/mcd/topic/163587/955481 2014年10月19日在哈尔滨工业大学举办了CSDN高校俱乐部全国巡讲. 此次邀请到了微软七届 ...