【sed】进阶
- N:将数据流的下一行加入当前模式空间
- D:删除多行组中的一行
- P:打印多行组中的一行
# cat data1.txt
this is the header line this is the data line this is the last line
经n命令处理后可跳过第一行:
# sed '/header/{n;d}' data1.txt
this is the header line
this is the data line
this is the last line
# cat data2.txt
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.
# sed '/first/{N;s/\n/ /}' data2.txt
This is the header line.
This is the first data line. This is the second data line.
This is the last line.
Tips:使用N命令可查找分散在两行的文本。注意最后一行进入模式空间,执行N命令由于没有后续的可读入行,会导致后面的命令无法执行。
# cat data5.txt This is the header line.
This is a data line. This is the last line.
# sed '/^$/{N ; /header/D}' data5.txt
This is the header line.
This is a data line.
This is the last line.
Tips:这种办法可实现删除特殊行,比如需要同时满足两种条件的行。
# sed -n '/header/{N;P}' data5.txt
This is the header line.
|
命令
|
描述
|
|
h
H
g
G
x
|
将模式空间复制到保持空间
将模式空间附加到保持空间
将保持空间复制到模式空间
将保持空间附加到模式空间
交换模式空间和保持空间
|
# sed -n '/first/ {h ; p ; n ; p ; g ; p }' data2.txt
This is the first data line.
This is the second data line.
This is the first data line.
# sed -n '/header/p' data2.txt
This is the header line.
普通p命令打印匹配文本行,使用!后,效果相反,不打印该行而打印其它行:
# sed -n '/header/!p' data2.txt
This is the first data line.
This is the second data line.
This is the last line.
结合保持空间反转文本行的顺序:
# sed -n '{1!G ; h ; $p }' data2.txt
This is the last line.
This is the second data line.
This is the first data line.
This is the header line.
Tips:sed编辑器默认会将命令循环应用在每一行文本上,因此合理使用命令,可写出简洁高效的sed脚本
# sed '{2,3b ; s/This is/Is this/ ; s/line./test?/}' data2.txt
Is this the header test?
This is the first data line.
This is the second data line.
Is this the last test?
加address,加label,分支匹配处的行跳到标签处,执行标签后的命令;不匹配的行执行所有命令:
# sed '/first/b jump1 ; s/This is the/No jump on/; :jump1 ; s/This is the/Jump here on/' data2.txt
No jump on header line.
Jump here on first data line.
No jump on second data line.
No jump on last line.
不加address,b命令无条件跳转到标签上,可造成循环的效果:
# echo "This, is, a, test, to, remove, commas." | sed -n '{:start;s/,//1p;b start}'
This is, a, test, to, remove, commas.
This is a, test, to, remove, commas.
This is a test, to, remove, commas.
This is a test to, remove, commas.
This is a test to remove, commas.
This is a test to remove commas.
^C
该脚本不会停止循环,可使用/,/b start代替b start,在没有逗号时自动结束循环。
# sed 's/first/matched/; t ; s/This/Here/' data2.txt
Here is the header line.
This is the matched data line.
Here is the second data line.
Here is the last line.
加label,跳转到label处:
# echo "This,is,a,test,to,remove,commas." | sed -n '{:start ; s/,/ /p ; t start}'
This is,a,test,to,remove,commas.
This is a,test,to,remove,commas.
This is a test,to,remove,commas.
This is a test to,remove,commas.
This is a test to remove,commas.
This is a test to remove commas.
# sed 's/first/matched/; t ;s/This/Here/;s/line/LINE/' data2.txt
Here is the header LINE.
This is the matched data line.
Here is the second data LINE.
Here is the last LINE.
# sed 's/first/matched/; t jump ;s/This/Here/; :jump ;s/line/LINE/' data2.txt
Here is the header LINE.
This is the matched data LINE.
Here is the second data LINE.
Here is the last LINE.
Tips:使用标签和t命令可在脚本命令中形成循环
# echo "The cat sleeps in his hat." | sed 's/.at/"&"/g'
The "cat" sleeps in his "hat".
【替代单独的单词】
# echo | sed ':start;s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;t start'
,,
Tips:圆括号必须转义
【sed】进阶的更多相关文章
- 第11章:sed进阶操作
第11章:sed进阶操作 sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法 sed命令行格式为 ...
- 《Linux命令行与shell脚本编程大全》第二十一章 sed进阶
本章介绍一些sed编辑器提供的高级特性. 21.1 多行命令 按照之前的知识,所有的sed编辑器命令都是针对单行数据执行操作的. 在sed编辑器读取数据流时,它会基于换行符的位置将数据分成行,一次处理 ...
- sed进阶N;P;D
案例 sed 的高级替换 $cat file1 why:1 why:2 3 4 5 why:6 why:7 8 why:9 $cat file2 why:1 why:2 3 4 5 why:6 why ...
- sed进阶教程
寻址规则 常规寻址 如果没有指定地址,那么命令将应用于每一行. 如果只有一个地址,那么命令应用于与这个地址匹配的任意行. 如果指定了由逗号分隔的两个地址,那么命令应用于匹配第一个地址(不包括第一个地址 ...
- sed进阶
下面这些命令未必经常会用到,但当需要时,知道这些肯定是件好事. 一.多行命令 sed命令通常是对一行数据进行处理,然后下一行重复处理. sed编辑器包含了三个可用来处理多行文本的特殊命令 N:将数据流 ...
- 文本处理工具——sed进阶
一sed的搜索替代 (一)常见的和替代相关的选项 搜索替代,和vim的写法很像 s///:查找替换,支持使用其它分隔符,s@@@,s### p: 显示替换成功的行,就是打印. w /PATH/TO/S ...
- Shell编程—sed进阶
1多行命令 sed编辑器包含了三个可用来处理多行文本的特殊命令. N:将数据流中的下一行加进来创建一个多行组来处理. D:删除多行组中的一行. P:打印多行组中的一行. 1.1next命令 1. 单行 ...
- sed命令总结-Linux
sed命令总结-Linux linuxsed 2018年02月08日 19时27分57秒 命令语法经常忘记,每次总是看笔记不切实际,记不起来的要多查manual,本次总结按照manual总结,希望下次 ...
- 两个有用的shell工具总结
shell工具之一:sed sed基础 sed编辑器被称作流编辑器,与常见的交互式文本编辑器刚好相反.文本编辑器可以通过键盘来交互式地插入.删除.替换文本中的数据:而流编辑器是基于一组预先的规则来编辑 ...
- 《Linux命令行与shell脚本编程大全 第3版》
第一部分 Linux 命令行 第1章 初识Linux she1.1 什么是Linux 21.1.1 深入探究Linux 内核 31.1.2 GNU 工具 61.1.3 Linux 桌面环境 81 ...
随机推荐
- HTML类
class Html: def __init__(self,name): self.name = name @staticmethod def full_name(): print('全称:Hype ...
- 2018 年 IoT 那些事儿
本文作者:murphyzhang.xmy.fen @腾讯安全云鼎实验室 2018年,是 IoT 高速发展的一年,从空调到电灯,从打印机到智能电视,从路由器到监控摄像头统统都开始上网.随着5G网络的 ...
- pip install win32api报错解决方法
在安装pywinauto模块,导入模块后,提示缺少:win32api 但是在使用pip install安装win32api后,居然报错 错误信息如下: Could not find a version ...
- 【异常】诡异的mysql错误,Pagehelper插件混乱导致吗
1 详细的异常信息 Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in ...
- LoadRunner(7)
一.参数化策略 1.Select next row(How? 如何取?)取值方式 选择下一行 1)Sequential:顺序的 每个VU都从第一行开始,顺序依次向下取值: 数据取完可以从头循环重复使用 ...
- LoadRunner(4)
一.LoadRunner工具的组成 1.VuGen 虚拟用户脚本生成器 脚本好比:武器 VuGen好比:兵工厂 VU好比:士兵 2.Controller 压力调度控制台 好比:总指挥部 3.Analy ...
- Oracle数据库结构
之前写了一篇文章<Oracle-知识结构漫谈> 粗略的介绍了Oracle数据库接口,在这里再更加详细的描述一下,当做是对原有知识的巩固,温故知新. Oracle体系结构数据库的体系结构是从 ...
- requests模拟浏览器请求模块初识
requests模拟浏览器请求模块初识 一.下载 requests模拟浏览器请求模块属于第三方模块 源码下载地址http://docs.python-requests.org/zh_CN/lates ...
- python_tkinter基本属性
1.外形尺寸 尺寸单位:只用默认的像素或者其他字符类的值!,不要用英寸毫米之类的内容. btn = tkinter.Button(root,text = '按钮') # 设置按钮尺寸,绝大多数默认单位 ...
- C# 集合(9) 持续更新
数组的大小是固定的.如果元素个数动态,就使用集合类. List<T>是与数组相当的集合类.其他的集合:队列.栈.链表.字典和集. .NET Framework 1.0 包括非泛型集合类,如 ...