文章目录

  • sed命令详解
  • 一、基本语法
  • 二、实操案例
    • 内部命令选项说明
    • 命令选项说明
    • 标志
  • 三、sed小技巧

sed命令详解

sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”,接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。

一、基本语法

sed [options] '{command}[flags]' [filename]
# 中括号内容必有 大括号内容可有可无
sed  # 执行命令
[options]  # 命令选项
{command}[flags]    # sed内部选项和参数
[filename]     # 文件
命令选项
-e script 将脚本中指定的命令添加到处理输入时执行的命令中,  多条件,一行中要有多个操作
-f script 将文件中指定的命令添加到处理输入时执行的命令中
-n        抑制自动输出
-i        编辑文件内容
-i.bak    修改时同时创建.bak备份文件。
-r        使用扩展的正则表达式
!         取反 (跟在模式条件后与shell有所区别)sed常用内部命令
a   在匹配后面添加
i   在匹配前面添加
p   打印
d   删除
s   查找替换
c   更改
y   转换   N D P flags
数字             表示新文本替换的模式
g:             表示用新文本替换现有文本的全部实例
p:             表示打印原始的内容
w filename:     将替换的结果写入文件

二、实操案例

# 新加一行
sed -i ' /<\/java-config>/i \ <jvm-options>-Dlog4j2.formatMsgNoLookups=true</jvm-options>' ${BES_HOME}/conf/server.config# 判断环境变量(容器化环境)中是否有参数,有则替换
if [ -n "$jvmOptionsXmx" ];thensed -i "$(sed -n '/-Xmx/='${BES_HOME}/conf/server.config)""c\    $jvmOptionsXmx" ${BES_ HOME}/conf/server.config
fiif [ -n "$jvmOptionsXms" ];thensed -i "$(sed -n '/-Xms/='${BES_HOME}/conf/server.config)""c\  $jvmOptionsXms" ${BES_HOME}/conf/server.config
fi

这个案例是我们项目中实际用到的,效果就是配置JVM参数。具体学习看下面的即可。

准备数据

[root@jiangnan ~]# vim sed.txt
[root@jiangnan ~]# cat sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

内部命令选项说明

文件内容增加操作,将数据追加到某个位置之后,使用命令 a 。

  1. 在指定行后面追加内容

在第二到四行每行后新开一行追加数据: append data “haha”

[root@jiangnan data]# sed '2,4a\append data "haha"' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
append data "haha"
3 the quick brown fox jumps over the lazy dog.
append data "haha"
4 the quick brown fox jumps over the lazy dog.
append data "haha"
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#

说明:a表示在匹配的行后面新开一行新增内容。2,4a表示第2到4行,如果不写数字,表示所有行。

  1. 匹配字符串追加

找到包含"3 the"的行,在其后新开一行追加内容: append data “haha”

[root@jiangnan data]# sed '/3 the/a\append data "haha"' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
append data "haha"
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#

这里注意:/要匹配的字符串/

这种方法比较常用,匹配指定的字符串然后进行增加,比如XML文件等。

文件内容增加操作,将数据插入到某个位置之前,使用命令 i 。

  1. 匹配字符串插入

找到包含"3 the"的行,在其前新开一行插入内容: insert data “haha”

[root@jiangnan data]# sed '/3 the/i\append data "haha"' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
append data "haha"
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#

这里注意:/要匹配的字符串/

使用方法同a类似。

文件内容修改操作—替换,将一行中匹配的内容替换为新的数据,使用命令s。

  1. 在指定行后面追加内容

将sed.txt 中第二到第四行的dog替换为cat

[root@jiangnan data]# sed '2,4s/dog/cat/' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy cat.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy cat.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
  1. 匹配字符串所在行指定字符的替换

将包含字符串"3 the"的行中的dog替换为cat

[root@jiangnan data]# sed '/3 the/s/dog/cat/' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#

这里注意:/匹配字符串所在的行/s/指定匹配字符串/要替换的字符串/

文件内容修改操作—更改,将一行中匹配的内容替换为新的数据,使用命令c。

  1. 匹配行整行的替换

将sed.txt 文件中的第二、三、四行的内容更改为:change data “haha”

[root@jiangnan data]# sed '2,4c\change data haha' sed.txt
1 the quick brown fox jumps over the lazy dog.
change data haha
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
  1. 匹配字符串所在行整行的替换

将sed.txt文件中包含"3 the"的行内容更改为: change data “haha”

[root@jiangnan data]# sed '/3 the/c\change data haha' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
change data haha
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#

文件内容修改操作—字符转换,将一行中匹配的内容替换为新的数据,使用命令y。

  1. 将sed.txt中的a b c字符转换为对应的 A B C字符
[root@jiangnan data]# sed 'y/abc/ABC/' sed.txt
1 the quiCk Brown fox jumps over the lAzy dog.
2 the quiCk Brown fox jumps over the lAzy dog.
3 the quiCk Brown fox jumps over the lAzy dog.
4 the quiCk Brown fox jumps over the lAzy dog.
5 the quiCk Brown fox jumps over the lAzy dog.
[root@jiangnan data]#

文件内容删除,将文件中的指定数据删除,使用命令d。

  1. 删除文件sed.txt 第三到第四行的数据
[root@jiangnan data]# sed '3,4d' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#

文件内容查看,将文件内容输出到屏幕,使用命令p。

  1. 打印sed.txt的内容
[root@jiangnan data]# sed 'p' sed.txt
1 the quick brown fox jumps over the lazy dog.
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#
  1. 打印sed.txt文件包含字符串"3 the"的行
[root@jiangnan data]# sed '/3 the/p' sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#

现象:可以看得出,打印内容是重复的行,原因是打印了指定文件内容一次,又将读入缓存的所有数据打印了一次,所以会看到这样的效果, 如果不想看到这样的结果,可以加命令选项-n抑制内存输出即可。

[root@jiangnan data]# sed -n '/3 the/p' sed.txt
3 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#

命令选项说明

在命令行中使用多个命令 -e

  1. 将brown替换为green dog替换为cat
[root@jiangnan data]# sed -e 's/brown/green/;s/dog/cat/' sed.txt
1 the quick green fox jumps over the lazy cat.
2 the quick green fox jumps over the lazy cat.
3 the quick green fox jumps over the lazy cat.
4 the quick green fox jumps over the lazy cat.
5 the quick green fox jumps over the lazy cat.
[root@jiangnan data]#

从文件读取编辑器命令 -f 适用于日常重复执行的场景

1)将命令写入文件
[root@jiangnan data]# vim abc.txt
[root@jiangnan data]# cat abc.txt
s/brown/green/
s/dog/cat/
s/fox/elephant/
[root@jiangnan data]# 2)使用-f命令选项调用命令文件
[root@jiangnan ~]# sed -f abc.txt sed.txt
1 the quick green elephant jumps over the lazy cat.
2 the quick green elephant jumps over the lazy cat.
3 the quick green elephant jumps over the lazy cat.
4 the quick green elephant jumps over the lazy cat.
5 the quick green elephant jumps over the lazy cat.
[root@jiangnan data]#

抑制内存输出 -n

  1. 打印sed.txt文件的第二行到最后一行内容 $最后的意思
[root@jiangnan data]# sed -n '2,$p' sed.txt
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#

使用正则表达式 -r

  1. 打印sed.txt中以字符串"3 the"开头的行内容
[root@jiangnan data]# sed -n -r '/^(3 the)/p' sed.txt
3 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#

说明:在正则表达式中如果出现特殊字符(^$.*/[]),需要以前导 “\” 号做转义

注意:做完了上面的案例,但是我们发现源文件并没有任何改变,原因就是sed操作的是缓存区里的内容,对我们的源文件没有做改变。如果需要修改文件内容可以直接使用-i命令选项,-i命令选项提供了备份功能,比如参数使用-i.bak,那么在修改源文件的同时会先备份一个以.bak结尾的源文件,然后再进行修改操作。

[root@jiangnan data]# sed -i.bak 's/brown/green/' sed.txt
[root@jiangnan data]# cat sed.txt
1 the quick green fox jumps over the lazy dog.
2 the quick green fox jumps over the lazy dog.
3 the quick green fox jumps over the lazy dog.
4 the quick green fox jumps over the lazy dog.
5 the quick green fox jumps over the lazy dog.
[root@jiangnan data]# cat sed.txt.bak
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]#

标志

准备数据

[root@jiangnan data]# vi sed2.txt
[root@jiangnan data]# cat sed2.txt
1 the quick brown fox jumps over the lazy dog . dog
2 the quick brown fox jumps over the lazy dog . dog
3 the quick brown fox jumps over the lazy dog . dog
4 the quick brown fox jumps over the lazy dog . dog
5 the quick brown fox jumps over the lazy dog . dog
[root@jiangnan data]#

数字标志:此标志是一个非零正数,默认情况下,执行替换的时候,如果一行中有多个符合的字符串,如果没有标志位定义,那么只会替换第一个字符串,其他的就被忽略掉了,为了能精确替换,可以使用数字位做定义。

  1. 替换一行中的第二处dog为cat
[root@jiangnan data]# sed 's/dog/cat/2' sed2.txt
1 the quick brown fox jumps over the lazy dog . cat
2 the quick brown fox jumps over the lazy dog . cat
3 the quick brown fox jumps over the lazy dog . cat
4 the quick brown fox jumps over the lazy dog . cat
5 the quick brown fox jumps over the lazy dog . cat
[root@jiangnan data]# sed 's/dog/cat/' sed2.txt
1 the quick brown fox jumps over the lazy cat . dog
2 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy cat . dog
5 the quick brown fox jumps over the lazy cat . dog
[root@jiangnan data]#

对比有标志和没有标志的区别

g标志:将一行中的所有符合的字符串全部执行替换

  1. 将包含3 the所在行中的dog替换为cat
[root@jiangnan data]# sed '/3 the/s/dog/cat/g' sed2.txt
1 the quick brown fox jumps over the lazy dog . dog
2 the quick brown fox jumps over the lazy dog . dog
3 the quick brown fox jumps over the lazy cat . cat
4 the quick brown fox jumps over the lazy dog . dog
5 the quick brown fox jumps over the lazy dog . dog
[root@jiangnan data]#

‘g’表示global,全部替换

p标志:打印文本内容,类似于-p命令选项

[root@jiangnan data]# sed '3s/dog/cat/p' sed2.txt
1 the quick brown fox jumps over the lazy dog . dog
2 the quick brown fox jumps over the lazy dog . dog
3 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy dog . dog
5 the quick brown fox jumps over the lazy dog . dog
[root@jiangnan data]#

w filename标志:将修改的内容存入filename文件中

[root@jiangnan data]# sed '3s/dog/cat/w sed3.txt' sed2.txt
1 the quick brown fox jumps over the lazy dog . dog
2 the quick brown fox jumps over the lazy dog . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy dog . dog
5 the quick brown fox jumps over the lazy dog . dog
[root@jiangnan data]# cat sed3.txt
3 the quick brown fox jumps over the lazy cat . dog
[root@jiangnan data]#

可以看出,将修改的第三行内容存在了sed3.txt文件中

三、sed小技巧

统计data2有多少行
[root@jiangnan ~]# sed -n '$=' data2
5打印data2内容时加上行号
[root@jiangnan ~]# sed  '=' data2
1
1 the quick brown fox jumps over the lazy dog . dog
2
2 the quick brown fox jumps over the lazy dog . dog
3
3 the quick brown fox jumps over the lazy dog . dog
4
4 the quick brown fox jumps over the lazy dog . dog
5
5 the quick brown fox jumps over the lazy dog . dog
输出包含the的所在行的行号(= 用来输出行号)
[root@jiangnan ~]# sed -n '/the/=' test.txt 输出以PI开头的行
[root@jiangnan ~]# sed -n '/^PI/p' test.txt 输出以数字结尾的行
[root@jiangnan ~]# sed -n '/[0-9]$/p' test.txt 输出包含单词wood的行 \< ,\>表示单词边界
[root@jiangnan ~]# sed -n '/\<wood\>/p' test.txt
a wood cross!每行开始添加#字符
[root@jiangnan ~]# sed 's/^/#/' test.txt 在包含the的每行行首添加#字符
[root@jiangnan ~]# sed '/the/s/^/#/' test.txt 在每行末尾添加EOF字符
[root@jiangnan ~]# sed 's/$/EOF/' test.txt 将3-5行所有的the替换为THE
[root@jiangnan ~]# sed '3,5s/the/THE/g' test.txt 将包含the的行中的o替换为O
[root@jiangnan ~]# sed '/the/s/o/O/g' test.txt 迁移符合条件的文本
H 复制到剪贴板;
g,G 将剪贴板中的数据覆盖/追加到指定行;
w保存为文件;
r读取指定文件;
a 追加指定内容将包含the的行迁移到行尾(;用于多个操作)H复制到剪贴板---d删除---$G追加到行尾
[root@jiangnan ~]# sed '/the/{H;d};$G' test.txt 将1-5行迁移到17行后
[root@jiangnan ~]# sed '1,5{H;d};17G' test.txt 将包含the的行另存为新文件
[root@jiangnan ~]# sed '/the/w out.file' test.txt [root@jiangnan ~]# lsanaconda-ks.cfg  out.file  test.txt  :wq[root@jiangnan ~]# cat out.file 在包含the每行后添加文件hostname内容
[root@jiangnan ~]# sed '/the/r /etc/hostname' test.txt 在第3行后插入新行,内容为New
[root@jiangnan ~]# sed '3aNew' test.txt 在包含the的每行后插入新行
[root@jiangnan ~]# sed '/the/aNew' test.txt 在第3行后插入多行(\n 换行符)
[root@jiangnan ~]# sed '3aNew1\nNew2' test.txt
ngnan ~]# sed '1,5{H;d};17G' test.txt 将包含the的行另存为新文件
[root@jiangnan ~]# sed '/the/w out.file' test.txt [root@jiangnan ~]# lsanaconda-ks.cfg  out.file  test.txt  :wq[root@jiangnan ~]# cat out.file 在包含the每行后添加文件hostname内容
[root@jiangnan ~]# sed '/the/r /etc/hostname' test.txt 在第3行后插入新行,内容为New
[root@jiangnan ~]# sed '3aNew' test.txt 在包含the的每行后插入新行
[root@jiangnan ~]# sed '/the/aNew' test.txt 在第3行后插入多行(\n 换行符)
[root@jiangnan ~]# sed '3aNew1\nNew2' test.txt

微信公众号先已开通,搜索 “江小南和他的小伙伴们” 就能找到我哦,各位小伙伴们可以关注一下,文章会进行同步更新,方便查看哦。

【Linux篇】sed命令详解相关推荐

  1. Linux的sed命令详解大全

    Linux的sed命令详解大全 一.sed命令介绍 二.sed 的运行模式 三.sed的相关选项 四.sed基本用法 1.sed语法 2.sed的查看功能 ①查看passwd文件的第5到第8行内容 ② ...

  2. linux 之sed命令详解

    sed命令详解 sed是什么 sed是linux文本处理三剑客之一,全称 StreamEDitor,非常著名的行编辑器,功能十分强大. sed的工作过程 sed处理文本时每次从文件复制出来一行,放在工 ...

  3. 【转】【Linux】sed命令详解

    sed命令详解 sed是stream editor的简称,也就是流编辑器.它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space ...

  4. Linux系统sed命令详解

    目录 一.简介 二.选项 三.文本编辑 四.具体实例 4.1 追加 4.2 删除 4.3 替换 4.4 打印 4.5 从文件中读内容 4.6 sed带变量执行 一.简介 sed命令可依照脚本的指令来处 ...

  5. Linux神兵利器—sed命令详解

    文章目录 一.概述 二.sed基本指令 `2.1.sed语法格式` `2.2.命令选项` `2.3.基本操作指令` `2.4.sed支持的数据定位方法` `2.5.示例` p指令 直接使用行号匹配 使 ...

  6. linux之sed命令详解

    调用sed命令有两种形式: **sed [options] 'command' file(s) sed [options] -f scriptfile file(s)** a\ 在当前行后面加入一行文 ...

  7. Linux的Sed命令详解

    简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space),接着用sed命令处理缓冲区中的内容,处 ...

  8. linux sed 详解,linux的sed命令详解

    一丶简介 sed是一种流编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(patternspace),接着用sed命令处理缓冲区中的内容,处理 ...

  9. linux sed p变量,Linux sed 命令详解系列教程之各种问题解决

    本文目录: 1 sed中使用变量和变量替换的问题 2 反向引用失效问题 3 "-i"选项的文件保存问题 4 贪婪匹配问题 5 sed命令"a"和"N& ...

最新文章

  1. 使用深度学习阅读和分类扫描文档
  2. idea设置scanner = new Scanner(System.in);的实时模板
  3. python0x80070005拒绝访问_PowerShell启用winrm失败:拒绝访问 0x80070005 -2147024891
  4. Codeforces 988E. Divisibility by 25
  5. MFC中Windows窗口消息循环及多线程之间关系
  6. [Network Architecture]DPN(Dual Path Network)算法详解(转)
  7. android WebView总 结
  8. Android 系统(124)---Android 如何快速写满存储空间
  9. JAVA CLASS混淆工具:JShrink简单试用
  10. 登录emc磁阵提示java版本低_安装完打开 eclipse 提示 JVM 版本较低
  11. 【人脸识别】基于matlab GUI PCA+SVM人脸识别(准确率)【含Matlab源码 823期】
  12. 完全免费的在线遥感影像下载器-转载
  13. 【Excel VBA】Application类下的一些常用功能总结
  14. js-05--对象是什么、创建对象、对象使用、操作对象、遍历对象、内置对象、Math、任意范围随机数、日期对象、字符串对象
  15. Mac系统如何显示隐藏文件
  16. 基于Matlab的空中交通管制仿真(附源码)
  17. macOS SwiftUI 教程之 06 绘图实现正弦波,通过调整频率和步进值来更新正弦波视图 (教程含源码)
  18. Python使用Treeview制作表格(二)
  19. js截取某个字段后面的字符串
  20. 大数据分布式计算开源框架Hadoop的介绍和运用

热门文章

  1. Linux查看Mac和IP地址
  2. 如何给mac book pro升级内存
  3. TX2制作镜像并烧写镜像
  4. 1.1图像与数字图像
  5. 嵌入式学习——stm32(3.1)定时器基础
  6. linux oracle 更换ip,Linux 修改IP地址和网关
  7. Linux修改IP地址--修改为静态IP
  8. 在制作WORD小报时添加艺术横线或者艺术竖线
  9. Python网络爬虫-原理及基础知识
  10. 20194731 第一次作业-博客初体验