sed使用
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使用的更多相关文章
- sed的应用
		h3 { color: rgb(255, 255, 255); background-color: rgb(30,144,255); padding: 3px; margin: 10px 0px } ... 
- 文本处理三剑客之sed命令
		第十八章.文本处理三剑客之sed命令 目录 sed介绍 sed命令常用选项 sed常用编辑命令 sed使用示例 sed高级语法 18.1.sed简介 sed全名stream editor,流编辑器,s ... 
- 6-2 sed 命令
		1. sed : stream editor,流编辑器 是处理纯ASICC纯文本,按行琢行操作的. 编辑器有两种,行编辑器和全屏编辑器 sed:默认不编辑原文件,仅对模式空间中的数据做处理,而后.处理 ... 
- 基本shell编程【3】- 常用的工具awk\sed\sort\uniq\od
		awk awk是个很好用的东西,大量使用在linux系统分析的结果展示处理上.并且可以使用管道, input | awk '' | output 1.首先要知道形式 awk 'command' fi ... 
- sed awk grep三剑客常用
		sed的常用用法: awk的常用用法: grep的常用用法: 除了列出符合行之外,并且列出后10行. grep -A 10 Exception kzfinance-front.log 除了列出符合行之 ... 
- linux shell 用sed命令在文本的行尾或行首添加字符
		转自 http://www.cnblogs.com/aaronwxb/archive/2011/08/19/2145364.html 昨天写一个脚本花了一天的2/3的时间,而且大部分时间都耗在了sed ... 
- Sed、Awk单行脚本快速参考
		文本间隔: # 在每一行后面增加一空行 sed G awk '{printf("%s\n\n",$0)}' # 将原来的所有空行删除并在每一行后面增加一空行. # 这样在输出的文本 ... 
- sed awk 样例
		sed [options] '[action]' filename options: -n:一般sed命令会把所有数据都输出到屏幕,如果加入此选项,则只会把经过sed命令处理的行输出到屏幕. -e:允 ... 
- linux sed命令详解
		简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ... 
- sed命令详解
		搜索 纠正错误 添加实例 sed 功能强大的流式文本编辑器 补充说明 sed 是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时 ... 
随机推荐
- Python title() 方法
			描述 Python title() 方法返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写. 语法 title() 方法语法: S.title() 参数 无. ... 
- C#四舍五入保留一位小数
			DateTime d1 = hrStaff.DateJoin; DateTime d2 = DateTime.Now; TimeSpan d3 = d2.Subtract(d1); ; //int i ... 
- Mybatis 中延时加载
			1 为了处理N+1 问题,Mybatis 引入了延时加载功能,意义是一开始并不取出关联数据,只有当使用时,才发送sql语句去取. mybatis中两个全局设置 lazyLoadingEnabled 和 ... 
- atitit.提升软件开发的生产力关健点-------大型开发工具最关健
			atitit.提升软件开发的生产力关健点-------大型开发工具最关健 1. 可以创作出更好的工具遍历自己 1 2. 大型工具包括哪些方面 2 2.1. ide 2 2.2. dsl 2 2.3. ... 
- atitit。gui 界面皮肤以及换肤总结 java .net c++
			atitit.gui 界面皮肤以及换肤总结 java .net c++ 1. Swing 的皮肤 1 1.1. windows风格 1 1.2. Mac风格 ( liquid 框架) 1 2. 如何给 ... 
- 1、Reactive Extensions for .NET(译)
			注:本文的工程是基于 vs2010 的,在 vs2012 中区别不大. 本文的意图是让读者熟悉 Reactive Extension for .net(Rx) 的使用.通过一系列的例子,让读者感受 基 ... 
- lo4j 日志级别
			日志记录器(Logger)的行为是分等级的.如下表所示: 分为OFF.FATAL.ERROR.WARN.INFO.DEBUG.TRACE.ALL或者您定义的级别.Log4j建议只使用四个级别,优先级从 ... 
- C语言  ·  打印1~100间的质数(素数)
			算法提高 c++_ch02_04 时间限制:1.0s 内存限制:256.0MB 问题描述 输出1~100间的质数并显示出来.注意1不是质数. 输出格式 每行输出一个质数. 2 3 . ... 
- EAGAIN、EWOULDBLOCK、EINTR与非阻塞 长连接
			EAGAIN.EWOULDBLOCK.EINTR与非阻塞 长连接 EWOULDBLOCK用于非阻塞模式,不需要重新读或者写 EINTR指操作被中断唤醒,需要重新读/写 在Linux环境下开发经常会碰到 ... 
- 针对16v554(ttyS0-15)的ttyAT0的login配置
			1 ## /etc/inittab# console::sysinit:/etc/init.d/rcSconsole::respawn:/sbin/getty -L 115200 ttyAT0 vt1 ... 
