sed高阶用法

a 追加

[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa
//向第二行后面追加'hi world'
[root@localhost ~]# sed '2ahi world' test
hello world
jjjd
hi world
aaaaaaa

//过滤包含'world'的行,在其后面追加'hi world'
[root@localhost ~]# sed '/world/ahi world' test
hello world
hi world
jjjd
aaaaaaa

i 插入

[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa

//在第一行插入'hhhh'
[root@localhost ~]# sed '1ihhhh' test
hhhh
hello world
jjjd
aaaaaaa

//过滤包含'jd'的行,插入'hhhh'
[root@localhost ~]# sed '/jd/ihhhh' test
hello world
hhhh
jjjd
aaaaaaa

c 更改

[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa

//将第一行更改为'hi world'
[root@localhost ~]# sed '1chi world' test
hi world
jjjd
aaaaaaa

//过滤包含'world'的行,更改为'HEELO WORLD'
[root@localhost ~]# sed '/world/cHELLO WORLD' test
HELLO WORLD
jjjd
aaaaaaa

y 替换,与’s///g’不同的是,y可以将指定的多个单个字符进行替换

[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa

//将文件里的a全部替换为h,h全部替换为a
[root@localhost ~]# sed 'y/ah/ha/' test
aello world
jjjd
hhhhhhh

//可以使用,这种方式进行大小写替换,将文件里的j全部变为大写
[root@localhost ~]# sed 'y/j/J/' test
hello world
JJJd
aaaaaaa

d 删除

[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa

//删除第一行
[root@localhost ~]# vim test
[root@localhost ~]# sed '1d' test
jjjd
aaaaaaa

//删除包含'hello'的行
[root@localhost ~]# sed '/hello/d' test
jjjd
aaaaaaa

D 删除,删除模式空间中直到第一个包含换行符的这部分内容

[root@localhost ~]# cat test
This line is followed by 1 blank line

This line is followed by 2 blank line

This line is followed by 3 blank line

This line is followed by 4 blank line

This is th end

[root@localhost ~]# sed '/^$/{N;/^\n$/d}' test
This line is followed by 1 blank line

This line is followed by 2 blank line
This line is followed by 3 blank line

This line is followed by 4 blank line
This is th end

//换成D试一试
[root@localhost ~]# sed '/^$/{N;/^\n$/D}' test
This line is followed by 1 blank line

This line is followed by 2 blank line

This line is followed by 3 blank line

This line is followed by 4 blank line

This is th end

p 打印 会打印匹配到的行

[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa

//打印第二行,但是会发现,第二行打印了两次,这是因为模式空间和sed的默认输出功能没有区分开,所以要使用-n抑制默认的输出
[root@localhost ~]# sed '2p' test
hello world
jjjd
jjjd
aaaaaaa
[root@localhost ~]# sed -n '2p' test
jjjd

打印以'world'结尾的行
[root@localhost ~]# sed -n '/world$/p' test
hello world

P 打印 打印匹配的行的开端到第一个换行符结束

[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa

[root@localhost ~]# sed -n '/world/{N;P}' test
hello world

//换成p试试
[root@localhost ~]# sed -n '/world/{N;p}' test
hello world
jjjd

n 读取当前匹配行的下一行

[root@localhost ~]# cat test
hello world
jjjd
aaaaaaa

//匹配'world',将world的下一行放入模式空间,然后p打印,并用-n抑制sed的默认打印功能
[root@localhost ~]# sed -n '/world$/{n;p}' test
jjjd

N 读取当前匹配的行并和下一行一起追加到模式空间

[root@localhost ~]# cat test
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.

//匹配以'Operator'结尾的行,将其和下一行追加到模式空间,进行替换
[root@localhost ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide /installation Guide\n/g}' test
Consult Section 3.1 in the installation Guide
for a description of the tape drives
available on your system.

//如果在替换过后'Guide'不进行换行的话,效果如下
[root@localhost ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g}' test
Consult Section 3.1 in the installation Guide for a description of the tape drives
available on your system.

h 将模式空间的内容复制到保持空间

H 将模式空间的内容追加到保持空间

g 将保持空间的内容复制到模式空间

G 将保持空间的内容追加到模式空间

[root@localhost ~]# sed  '/1/{h;d};/2/g' test
1
11
111

[root@localhost ~]# sed  '/1/{h;d};/2/G' test
2
1
22
11
222
111

[root@localhost ~]# sed  '/1/{H;d};/2/g' test

1

1
11

1
11
111

[root@localhost ~]# sed  '/1/{H;d};/2/G' test
2

1
22

1
11
222

1
11
111

x 交换保持空间和模式空间的内容

[root@localhost ~]# sed  '/1/{H;d};/2/x' test

1
2
11
22
111

Original: https://www.cnblogs.com/zicnotes/p/16699465.html
Author: Zic师傅
Title: sed高阶用法

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/607008/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Java基础系列–07_Object类的学习及源码分析

    Java中Object根类及其底层的学习 Object: 超类(1)Object是类层次结构的顶层类,是所有类的 根类,超类。所有的类都直接或者间接的继承自Object类。所有对象…

    Linux 2023年6月7日
    081
  • RISC-V汇编指南

    The RISC-V Assembly Programmer’s Manual is I think it’s probably better to bee…

    Linux 2023年6月6日
    068
  • shell内置命令和外部命令的区别

    shell内置命令和外部命令的区别 内部命令实际上是shell程序的一部分,其中包含的是一些比较简单的linux系统命令,这些命令由shell程序识别并在shell程序内部完成运行…

    Linux 2023年6月7日
    075
  • SpringBoot + Vue + ElementUI 实现后台管理系统模板 — 后端篇(三): 整合阿里云 OSS 服务 — 上传、下载文件、图片

    (1) 相关博文地址: SpringBoot + Vue + ElementUI 实现后台管理系统模板 — 前端篇(一):搭建基本环境:https://www.cnblogs.c…

    Linux 2023年6月11日
    078
  • 消费税

    1994年税制改革时,我国才设置了独立的消费税,与实行普遍征收的增值税配套,对特定消费品进行特殊调节。 消费税的特点: (一)征税范围具有选择性 有选择地确定若干个征税项目,在税法…

    Linux 2023年6月14日
    0101
  • 让gitlab暴露node-exporter供外部prometheus使用

    花了两天部署了一套监控服务 prometheus+node-exporter+grafana,公司的gitlab服务器准备部署node-exporter的时候突然发现gitlab已…

    Linux 2023年6月7日
    0127
  • CentOS 压缩解压

    打包:将多个文件合成一个总的文件,这个总的文件通常称为 “归档”。 压缩:将一个大文件通过某些压缩算法变成一个小文件。 1.1、tar 压缩格式: tar …

    Linux 2023年6月8日
    084
  • docker安装xxl-job-admin

    拉取镜像 docker pull xuxueli/xxl-job-admin:2.3.1 运行容器 docker run -d -e PARAMS=”–sp…

    Linux 2023年6月7日
    086
  • 【原创】Linux虚拟化KVM-Qemu分析(三)之KVM源码(1)

    背景 Read the fucking source code! –By 鲁迅 A picture is worth a thousand words. –…

    Linux 2023年6月8日
    086
  • spingboot使用redis连接池报错

    配置如下,增加了连接池相关 redis: host: localhost port: 6379 database: 7 lettuce: pool: max-active: 20 …

    Linux 2023年5月28日
    087
  • MSSQL·CONCAT函数的基础使用

    | 0.47分钟 | 752.8字符 | 1、应用场景 2、基础使用 3、声明与参考资料 | SCscHero | 2022/5/13 PM10:18 | 系列 | 已完成 每一个…

    Linux 2023年6月14日
    082
  • mysql select语句查询流程是怎么样的

    mysql select查询的数据是查询内存里面,如果没有查询的数据没有在内存,就需要mysql的innodb引擎读取磁盘,将数据加载的内存后在读取。这就体现了,mysql查询大量…

    Linux 2023年6月8日
    092
  • Guava 内存缓存的使用

    一、概述 guava⽬前有三种刷新本地缓存的机制: expireAfterAccess:当缓存项在指定的时间段内没有被读或写就会被回收。 expireAfterWrite:当缓存项…

    Linux 2023年6月16日
    0106
  • TCP/UDP 编程模型

    TCP编程模型 server创建socket套接字 socket套接字–可以理解为文件描述符(file descriptor),UNIX把网络看成文件 /** * @p…

    Linux 2023年6月6日
    0103
  • 列表初始化

    C++11将列表初始化(大括号初始化)作为一种通用的初始化方式.可用于所有类型. 数组以前就可以用列表初始化,但 C++11 中的列表初始化新增了一些功能: 初始化数组时,可省略等…

    Linux 2023年6月13日
    072
  • 关于熵,条件熵,交叉熵等的介绍

    参考:《数学之美》一文搞懂交叉熵在机器学习中的使用,透彻理解交叉熵背后的直觉详解机器学习中的熵、条件熵、相对熵和交叉熵常用的分类问题中的损失函数 1.信息量与信息熵 香农在他著名的…

    Linux 2023年6月13日
    094
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球