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)

大家都在看

  • VS2022编译太慢

    解决方法是把编译出的exe程序或目录添加到杀毒软件白名单 一个C++的helloworld,在vs里硬是10秒才能编译启动。不知道大家有没有遇到。禁用符号加载还是很慢。甚至换成co…

    Linux 2023年6月6日
    0112
  • Linux 配置Git

    前言:请各大网友尊重本人原创知识分享,谨记本人博客: 南国以南i 一、用git –version命令检查是否已经安装 二、下载git源码并解压 wget https:/…

    Linux 2023年5月27日
    0135
  • 软件定义网络第一次作业

    配置结果 如何pip解决下载过慢问题 实验环境配置 环境安装截图如下 安装环境过程中一些问题的解决 github连接不上 在hosts文件中加上以下语句 140.82.114.3 …

    Linux 2023年6月7日
    0112
  • 前端基础之JavaScript(二)

    一、函数 1.1 函数定义 JavaScript中的函数和Python中的非常相似,只是定义方式有点区别。 // 普通函数定义 function f1() { console.lo…

    Linux 2023年6月14日
    0101
  • JS 模块化- 04 CMD 规范与 Sea JS

    1 CMD 规范介绍 CMD: Common Module Definition, 通用模块定义。与 AMD 规范类似,也是用于浏览器端,异步加载模块,一个文件就是一个模块,当模块…

    Linux 2023年6月6日
    0116
  • 解决关闭shell会话窗口则会发现ASP.NET Core应用也会被关闭问题

    统计了三种方法: 一、使用nohup命令即可 【最简单】 nohup dotnet xxxx.dll 【xxxx为应用名称】 一般会报:nohup: ignoring input …

    Linux 2023年6月8日
    0125
  • 新手如何引入Echart图标

    1.首先需要到Echart官网去下载配置文件 (官网地址:https://echarts.apache.org/zh/index.html) a.点击下载 b.点击下载后进入到这个…

    Linux 2023年6月13日
    093
  • [云计算]腾讯云从业者认证-思维导图

    第一章 云计算基础介绍 第二章 腾讯云服务器产品介绍 第三章 腾讯云网络产品介绍 第四章 腾讯云CDN加速产品介绍 第五章 腾讯云存储产品介绍 第六章 腾讯云数据库产品介绍 第七章…

    Linux 2023年6月13日
    0129
  • Maven中POM文件总体配置说明

    POM文件总体配置说明 xxx xxx xxx xxx 4.0.0 xxx xxx jar 1.0-SNAPSHOT xxx-maven http://maven.apache.o…

    Linux 2023年6月6日
    094
  • python爬虫_入门_翻页

    写出来的爬虫,肯定不能只在一个页面爬,只要要爬几个页面,甚至一个网站,这时候就需要用到翻页了 其实翻页很简单,还是这个页面http://bbs.fengniao.com/forum…

    Linux 2023年6月6日
    087
  • Shell中的特殊符号(special characters)和含义

    之前写过两篇关于Bash语法的blog,分别是: 个人感觉,想要通畅地读懂bash脚本,还差一个部分,那就是符号。 个人网上的讲bash符号的文章有点乱,要么有错,要么不全,要么太…

    Linux 2023年5月28日
    099
  • php获取图片的主要颜色值RGB

    public function dominant_color() { $image = 'D:/Python/flow/test_photos/12240303_80d8…

    Linux 2023年6月7日
    0117
  • 正则表达

    常用表达式 单字符:. : 除换行以外所有字符[] :[aoe] [a-w] 匹配集合中任意一个字符\d :数字 [0-9]\D : 非数字\w :数字、字母、下划线、中文\W :…

    Linux 2023年6月13日
    092
  • 高速USB转8串口产品设计-RS232串口

    基于480Mbps 高速USB转8路串口芯片CH348,可以为各类主机扩展出8个独立的串口。使用厂商提供的VCP串口驱动程序,可支持Windows、Linux、Android、ma…

    Linux 2023年6月7日
    098
  • MySQL — 数据操作语言

    DML 全称 Data Manipulation Language。数据操作语言,用来对数据库表中的数据进行增删改。 插入一条数据 插入多条数据 update &#x886…

    Linux 2023年6月8日
    0106
  • 数据结构 图

    cpp;gutter:true;</p> <h1>include</h1> <p>using namespace std;</…

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