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)

大家都在看

  • 备用链接

    win10 stick note 地址 https://www.partitionwizard.com/partitionmanager/where-are-sticky-note…

    Linux 2023年6月7日
    092
  • 【论文笔记】(2015,防御蒸馏)Distillation as a Defense to Adversarial Perturbations against Deep Neural Networks

    有关蒸馏 (Distillation)的论文: (2006)Model Compression (2014)Do Deep Nets Really Need to be Deep?…

    Linux 2023年6月7日
    0139
  • rtmp和rtsp的区别

    刚刚接触到视频推流,搞不清楚rtmp和rtsp到底有什么区别 1.视频传输 RTSP+RTP主要用于IPTV,原因是传输数据使用的是UDP,在网络环境比较稳定的情况下,传输效率是比…

    Linux 2023年6月8日
    0101
  • 【转】我是一个CPU:这个世界慢!死!了!

    简介 经常听到有人说磁盘很慢、网络很卡,这都是站在人类的感知维度去表述的,比如拷贝一个文件到硬盘需要几分钟到几十分钟,够我去吃个饭啦;而从网络下载一部电影,有时候需要几个小时,我都…

    Linux 2023年6月16日
    0152
  • Docker centos7,宝塔

    拉取一个centos镜像 docker pull centos:centos7 运行一个容器 docker run -i -t -d –restart=always –name…

    Linux 2023年6月6日
    0100
  • 网络中冗余备份

    冗余备份的重要性 如今社会,网络是各个产业的新的血脉,网络的稳定性至关重要,一旦网络出现故障,导致断网、延迟丢包等很可能会导致生产作业停滞,造成较经济损失,为此冗余备份至关重要,从…

    Linux 2023年6月6日
    0128
  • redis 入门安装流程

    redis安装流程 安装linux的Redis [官网下载即可][ https://redis.io/download/ ] 一般会移动到opt目录下 mv redis-7.0.4…

    Linux 2023年6月7日
    0110
  • Spring5新特性—Log4j2

    Spring5新特性—Log4j2 Spring5新特性—Log4j2 创建一个Maven项目,导入依赖 org.apache.logging.log4j log4j-core 2…

    Linux 2023年6月14日
    092
  • PowerShell Automation : Introduction To Kasini3000

    tags: Ansible , pipeline , winrm , SSH , psremoting , automation , devops site mirror: htt…

    Linux 2023年6月14日
    0105
  • 多级缓存-redis缓存预热

    冷启动:服务刚刚启动时,Redis中并没有缓存,如果所有商品数据都在第一次查询时添加缓存,可能会给数据库带来较大压力。 缓存预热:在实际开发中,我们可以利用大数据统计用户访问的热点…

    Linux 2023年5月28日
    095
  • 初识MySQL数据库

    一 、引言 假设现在你已经是某大型互联网公司的高级程序员,让你写一个火车票购票系统,来hold住双十一期间全国的购票需求,你怎么写? 由于在同一时段抢票的人数太多,所以你的程序不可…

    Linux 2023年6月14日
    0123
  • 【证券从业】金融基础知识-第三章 证券市场主体01

    注1:后续学习并整理到第八章,全书完结后再合并成一个笔记进行源文件分享 注2:本章内容巨多,大约分为三篇文章记录消化 posted @2022-06-01 22:20 陈景中 阅读…

    Linux 2023年6月13日
    0125
  • Nginx基础入门篇(3)—返回状态码详解

    一般常见返回状态码 200 – 服务器成功返&a…

    Linux 2023年6月6日
    0102
  • 存储过程,存储函数(Oracle)

    –打印hello world create or replace procedure sayhelloworld as –说明部分 begin dbms_output.put_…

    Linux 2023年6月14日
    086
  • 搭建openvpn连接公司内网

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    Linux 2023年6月7日
    0124
  • Identity Server 4客户端认证控制访问API(一)

    一、说明 我们将定义一个api和要访问它的客户端,客户端将在identityser上请求访问令牌,并使用访问令牌调用api 二、项目结构与准备 1、创建项目QuickStartId…

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