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. 大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful) .net core 控制台程序使用依赖注入(Autofac)

    大比速:remoting.WCF(http).WCF(tcp).WCF(RESTful).asp.net core(RESTful) 近来在考虑一个服务选型,dotnet提供了众多的远程服务形式.在只 ...

  2. 本地ubuntu下pycharm 如何利用远程开发环境时显示图片

    最近使用pycharm远程开发tensorflow,每次在想显示图像时,苦于不知怎么操作,就通过保存后再看结果,使得调试很不方便.今天打算解决这个问题,收获也是很多啊. 我首先参考了这两篇博客: ht ...

  3. 导入项目出现: Unable to resolve target ‘android-10′ 解决办法

    进入到android项目根目录下,打开项目文件project.properties ,修改 target=android-10  的值.把10改为当前虚拟机API level的版本即可.我这里改为17 ...

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

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

  5. js 温故而知新 用typeof 来判断一个未定义的变量

    一直以为,如果你使用一个未定义的变量,肯定会报错.甚至根本不可能有这种场景. 但仔细想想还是有的,譬如你要判断全局是否存在$变量.或者要为全局暴漏一个全局变量之前,先判断是否有这个变量. typeof ...

  6. Object-C中的字符串对象2-可变字符串

    // // main.m // 字符串-可变字符串 // // Created by zhangqs008 on 14-2-13. // Copyright (c) 2014年 zhangqs008. ...

  7. union共用体的对齐

    union DATE { char a; ]; double b; }; DATE max; cout<< sizeof(max) << endl; 这个问题很好回答,并且我把 ...

  8. 玩转Linux文件描述符和重定向

    本文介绍linux中文件描述符与重定向的相关知识,文件描述符是与文件输入.输出相关联的整数,它们用来跟踪已打开的文件.有需要的朋友参考下. 原文出处:http://www.jbxue.com/arti ...

  9. NB 命令安装需似机(无图型化安装)

    [root@ok ~]# virt-install -v -n 09ng04 -r 512 --vcpus=4 --location=/home/ISO/CentOS-6.7-x86_64-bin-D ...

  10. 从乌云的错误漏洞分析看Mifare Classic安全

    前言 12年2月初国内著名安全问题反馈平台-乌云发布了有关某公司员工卡的金额效验算法破解的安全问题.从整个漏洞分析来看,漏洞的提交者把员工卡的数据分析得非常仔细,以至很多刚刚接触或者未曾接触的都纷纷赞 ...