文档目录
一、sed-简介
1、shell对文件操作介绍
2、sed命令简介
3、sed语法: sed [options] '{command}{flags}' [filename]
二、sed-命令选项( -e ; -f ; -n ; -i ; -i.bak ; -r ; ! )
1、-e:将脚本中指定的一个或多个命令添加到处理输入时执行
2、-f:将文本中指定的命令添加到处理输入时执行的命令中
3、-n:抑制自动输出
4、-i:编辑文件内容
5、-i.bak:修改同时创建.bak备份源文件
三、sed-内部命令( a ; i ; p ; d ; s ; c ; y )
1、a :在匹配后面添加
2、i :在匹配前面添加
3、p :打印
4、d :删除
5、s :查找替换
6、c :更改
7、y :转换
四、sed-flags( 0-9 ; g ; p ; w filename)
1、数字:表示新文本替换的模式
2、g :表示用新文本替换现在文本的全部实例
3、p :表示打印原始的内容
4、w filename :将替换的结果写入文件
五、sed-小技巧
1、$统计文本有多少行
2、-r正则统计
3、^每行开头插入内容
4、$每行结尾追加内容
5、删除空行:/^$/d
6、删除空格:s/ *//g
7、查看/删除-注释行或空行

- - - - - - - - - -  - - - - - - - - - -  - - - - - - - - - -  分隔符 - - - - - - - - - -  - - - - - - - - - -  - - - - - - - - - -  - - - -

一、sed-简介

1、shell对文件操作介绍

shell脚本编写中,会用到对文件的相关操作,如:增加内容,修改内容,删除部分内容,查看部分内容等,但是上述举例的这些操作一般都是需要文本编辑器中才能操作,常用的文本编辑器如:gedit、vim、nano等交互式文本编辑器,脚本无法自己独立完成,必须有人参与才可以完成,如果这样的话违背了我们编写脚本的初衷(全部由机器完成,减少人的工作压力,提升工作效率),如何才能让这些操作全部脚本自己搞定,而不需要人的参与。

为了解决上述问题,liunx为大家提供了一些命令,比如Perl、sed等命令,今天我就着重为大家介绍一下sed命令

2、sed命令简介

sed是liunx中提供的一个外部命令,它是一个行编辑器,非交互式的对文件内容进行增删改查的操作,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。它和文本编辑器是有本质的区别:

文本编辑器:编辑对象是文件,一次处理一个文本

行编辑器:编辑的对象是文件中的行,一次处理文本中一行

3、sed语法: sed [options] '{command}{flags}' [filename]

二、sed-命令选项( -e ; -f ; -n ; -i ; -i.bak ; -r ; ! )

[root@localhost test20210807]# cat data1  #初始化数据
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.

1、-e :将脚本中指定的一个或多个命令添加到处理输入时执行

[root@localhost test20210808]# sed -e '3s/dog/cat/;s/brown/green/' data1  #同时替换2个内容:dog->cat;brown->green
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 cat.
4 the quick green fox jumps over the lazy dog.
5 the quick green fox jumps over the lazy dog.

2、-f :将文本中指定的命令添加到处理输入时执行的命令中

[root@localhost test20210808]# cat script20210808  #命令在脚本内一个命令写一行
3s/dog/cat/
s/brown/green/
[root@localhost test20210808]# sed -f script20210808 data1 #传递文本中命令执行
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 cat.
4 the quick green fox jumps over the lazy dog.
5 the quick green fox jumps over the lazy dog.

3、-n:抑制自动输出

[root@localhost test20210808]# sed -n '3s/dog/cat/p' data1   #将第3行第一个dog替换为cat,只打印次行,抑制自动打印
3 the quick brown fox jumps over the lazy cat.

4、-i:编辑文件内容

[root@localhost test20210808]# sed -i '3s/dog/cat/' data1  #修改源文件,不输出
[root@localhost test20210808]# cat data1
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.

5、-i.bak:修改同时创建.bak备份源文件

[root@localhost test20210808]# sed -i.bak '3s/dog/cat/' data1  #修改第3行cat->dog,并保存源文件为.bak
[root@localhost test20210808]# cat data1
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@localhost test20210808]# cat data1.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.

6、-r:使用扩展的正则表达式

7、!:取反(跟在模式条件后与shell有所区别)(暂无例子

三、sed-内部命令( a ; i ; p ; d ; s ; c ; y )

[root@localhost test20210807]# cat data1  #初始化数据
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.

1、a :在匹配后面添加

[root@localhost test20210807]# sed 'ahello world' data1  #在每行后追加helloworld
1 the quick brown fox jumps over the lazy dog.
hello world
2 the quick brown fox jumps over the lazy dog.
hello world
3 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
hello world
5 the quick brown fox jumps over the lazy dog.
hello world

2、i  :在匹配前面添加

[root@localhost test20210808]# sed 'ihello world' data1  #在每行前面插入helloworld
hello world
1 the quick brown fox jumps over the lazy dog.
hello world
2 the quick brown fox jumps over the lazy dog.
hello world
3 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
hello world
5 the quick brown fox jumps over the lazy dog.
[root@localhost test20210808]# sed '/3 the/i\hello world' data1 #匹配模式在第3行前插入
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
hello world
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.

3、p :打印

[root@localhost test20210808]# sed 'p' data1  #打印文件内容,再输出一遍
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@localhost test20210808]# sed '3p' data1 #打印第3行,再输出原始文件一遍
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.

4、d :删除

[root@localhost test20210808]# sed 'd' data1  #全部删除
[root@localhost test20210808]# sed '2d' data1 #删除第二行
1 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@localhost test20210808]# sed '2-5d' data1 #删除第2-5行
sed: -e expression #1, char 2: unknown command: `-'
[root@localhost test20210808]# sed 'd' data1 #全部删除
[root@localhost test20210808]# sed '2d' data1 #删除第2行
1 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@localhost test20210808]# sed '2,5d' data1 #删除第2-5行
1 the quick brown fox jumps over the lazy dog.
[root@localhost test20210808]# sed '/3 the/d' data1 #匹配模式删除第3行
1 the quick brown fox jumps over the lazy dog.
2 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、s :查找替换

[root@localhost test20210808]# sed 's/dog/cat/' data1  #替换dog为cat
1 the quick brown fox jumps over the lazy cat.
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 cat.
[root@localhost test20210808]# sed '3s/dog/cat/' data1 #替换第3行dog为cat
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@localhost test20210808]# sed '2,4s/dog/cat/' data1 #替换第2-4行dog为cat
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@localhost test20210808]# sed '/3 the/s/dog/cat/' data1 #匹配模式替换第3行dog为cat
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.

6、c :更改

[root@localhost test20210808]# sed 'c\hello world' data1  #每行更改
hello world
hello world
hello world
hello world
hello world
[root@localhost test20210808]# sed '3c\hello world' data1 #修改第3行
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@localhost test20210808]# sed '2,4c\hello world' data1 #删除2-4行,并在第2行位置插入
1 the quick brown fox jumps over the lazy dog.
hello world
5 the quick brown fox jumps over the lazy dog.
[root@localhost test20210808]# sed '/3 the/c\hello world' data1 #匹配模式更改第3行
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

7、y :转换

[root@localhost test20210808]# sed 'y/abcdefg/ABCDEFG/' data1  #转换指定值
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@localhost test20210808]# sed 'y/abcdefg/1234567/' data1 #转换指定值
1 th5 qui3k 2rown 6ox jumps ov5r th5 l1zy 4o7.
2 th5 qui3k 2rown 6ox jumps ov5r th5 l1zy 4o7.
3 th5 qui3k 2rown 6ox jumps ov5r th5 l1zy 4o7.
4 th5 qui3k 2rown 6ox jumps ov5r th5 l1zy 4o7.
5 th5 qui3k 2rown 6ox jumps ov5r th5 l1zy 4o7.

四、sed-flags:( 0-9 ; g ; p ; w filename )

[root@localhost test20210808]# cat data3  #数据准备-flags测试
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.

1、数字 :表示新文本替换的模式

[root@localhost test20210808]# sed 's/dog/cat/2' data3   #将第2个dog替换为cat
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.

2、g :表示用新文本替换现在文本的全部实例

[root@localhost test20210808]# sed 's/dog/cat/g' data3   #将所有的dog替换为cat
1 the quick brown fox jumps over the lazy cat.cat.
2 the quick brown fox jumps over the lazy cat.cat.
3 the quick brown fox jumps over the lazy cat.cat.
4 the quick brown fox jumps over the lazy cat.cat.
5 the quick brown fox jumps over the lazy cat.cat.

3、p  :表示打印原始的内容

[root@localhost test20210808]# sed '3s/dog/cat/p' data3   #将第3行第一个dog替换为cat,且打印出
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.

4、w filename :将替换的结果写入文件

[root@localhost test20210808]# sed '3s/dog/cat/w savedata3' data3   #将第3行第一个dog替换为cat,且w保存到对应文件内
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@localhost test20210808]# cat savedata3
3 the quick brown fox jumps over the lazy cat.dog.

五、sed-小技巧

[root@localhost test20210808]# cat data1  #测试数据
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.

 1、$统计文本有多少行

[root@localhost test20210808]# sed -n '$=' data1  #统计文本有多少行,等价于wc -l < data1
5

2、-r正则统计

[root@localhost test20210808]# sed -nr '/^(root)(.*)(bash)$/p' /etc/passwd  #统计以root开头中间是任意,bash结尾的账户信息
root:x:0:0:root:/root:/bin/bash

3、^每行开头插入内容

[root@localhost test20210808]# sed s/^/hi../ data1  #每行开头插入hi..
hi..1 the quick brown fox jumps over the lazy dog.
hi..2 the quick brown fox jumps over the lazy dog.
hi..3 the quick brown fox jumps over the lazy dog.
hi..4 the quick brown fox jumps over the lazy dog.
hi..5 the quick brown fox jumps over the lazy dog.

4、$每行结尾追加内容

[root@localhost test20210808]# sed s#\$#verygood# data1  #每行结尾追加verygood
1 the quick brown fox jumps over the lazy dog.verygood
2 the quick brown fox jumps over the lazy dog.verygood
3 the quick brown fox jumps over the lazy dog.verygood
4 the quick brown fox jumps over the lazy dog.verygood
5 the quick brown fox jumps over the lazy dog.verygood

5、删除空行:/^$/d

[root@localhost test20210808]# cat data4  #测试数据
test line1 test line3 ok hello test very good test line5
[root@localhost test20210808]# sed /^$/d data4 #删除空行
test line1
test line3 ok hello test very good
test line5

6、删除空格:s/ *//g

[root@localhost test20210808]# cat data4  #测试数据
test line1
test line3 ok hello test very good
test line5
[root@localhost test20210808]# sed "s/ *//g" data4 #删除空格
testline1
testline3okhellotestverygood
testline5

 7、查看/删除-注释行或空行

[root@localhost test20210808]# cat data2 #测试空行和#号的删除
# line1 注释
# line2 注释 line4 正文
[root@localhost test20210808]# sed -ne '/^#/p;/^$/p' data2 #-e查看#号或空格开头
# line1 注释
# line2 注释 [root@localhost test20210808]# sed -nr '/(^#|#|^$)/p' data2 #正则查看#号或空格开头
# line1 注释
# line2 注释 [root@localhost test20210808]# sed -r '/(^#|#|^$)/d' data2 #正则删除#号或空格开头
line4 正文

shell脚本(15)-sed命令的更多相关文章

  1. linux下shell脚本中sed命令的用法

    先来给一个案例: #将old.sql文件中的符号“|”替换为“,”,并保存到test.sql文件中 sed "s/|/,/g" "old.sql"> te ...

  2. centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课

    centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件.目录属性  shell数组简单用法 $( ) 和$ ...

  3. shell脚本批量执行命令----必需判断上一步执行结果--没有捷径

    # 注意:shell脚本批量执行命令,不能只写一个函数,然后把所有命令复制进去,之前试过这样是不行的.必须要有一个判断命令执行成功与否的语句 # 简单的命令可以不加结果判断符号,但是遇到解压包.sed ...

  4. shell脚本一条命令直接发送http请求(xjl456852原创)

    我们知道nc命令是一个网络工具.可以连接tcp/udp.也能模拟发送http请求. 现在介绍通过shell脚本,一条命令直接发送http请求. 命令如下,可以对下面的地址等信息自行修改: #!/bin ...

  5. shell脚本中sqlite3命令查询数据库失败返回空,并将错误信息打印到标准错误输出

    shell脚本中sqlite3命令查询数据库失败返回空,并将错误信息打印到标准错误输出 如: #/bin/sh local ret='sqlite3 test.db "select test ...

  6. 在shell中使用sed命令替换/为\/

    sed命令相关: https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html https://www.cnblogs.com/D ...

  7. (转)shell脚本之seq命令

    shell脚本之seq命令 原文:http://blog.csdn.net/paoxiaohui/article/details/52830595 seq 用于生成从一个数到另一个数之间的所有整数. ...

  8. shell脚本之tr命令使用

    tr命令用来进行对标准输入的内容做替换.例如 # echo 'HELLO WORLD!!!' | tr "A-Z" "a-z" hello world!!! 这 ...

  9. Shell脚本之sed详解

    在编写shell脚本的过程中,我们经常需要使用sed流编辑器和awk对文本文件进行处理. 一.什么是sed? sed 是一种在线编辑器,它一次处理一行内容.sed是非交互式的编辑器.它不会修改文件,除 ...

  10. shell读取配置文件-sed命令

    在编写启动脚本时,涉及到读取配置文件,特地记录下shell脚本读取启动文件的方式.主要提供两种格式的读取方式,方式一配置文件采用"[]"进行分区,方式二配置文件中需要有唯一的配置项 ...

随机推荐

  1. 关于C#反射概念,附带案例!

    反射 C#中的反射是一种使程序在运行时能够动态地获取类型信息并调用其成员的技术.通过反射,程序可以在运行时进行类型的动态加载.创建对象.调用方法和属性,以及访问和修改字段等.反射可以使程序更加灵活,但 ...

  2. 解决This application failed to start because cannot find or load the qt platform plugin 'xcb'

    问题描述: 在使用linux系统训练自己的数据集合时,出现了上述问题,首先第一个想法就是先Google,但是在看了一些国内外的文章后依然没有将问题解决 问题原因: 这是由于这几天我在安装cuda.cu ...

  3. 使用axios发送请求的几种方式

    1.是什么? axios 它的底层是用了 XMLHttpRequest(xhr)方式发送请求和接收响应,xhr 相对于之前讲过的 fetch api 来说,功能更强大,但由于是比较老的 api,不支持 ...

  4. 为什么说数字孪生和GIS高度互补?它们各自从对方那里获得了什么?

    在数字化时代,数字孪生和GIS作为两项重要技术,它们的融合正日益受到人们的关注和认可.数字孪生是将实体世界与数字世界紧密结合的技术,可以创建实时的虚拟副本,对物理系统进行模拟.优化和预测.而GIS则是 ...

  5. java方法的定义与执行

    java中的方法在类中定义. 定义方法格式: 访问修饰符    返回值类型    方法名(参数列表){   ...  执行内容  ...   return 返回值; } 访问修饰符:表示方法在哪里能被 ...

  6. 使用dtd定义元素

  7. PHP中的反序列化漏洞理解

    序列化serialize() 序列化说通俗点就是把一个对象变成可以传输的字符串,比如下面是一个对象: class S{ public $test="pikachu"; } $s=n ...

  8. 如何在SAP GUI中快速执行新的事务代码

    当我们成功登录SAP的某个连接后,在SAP GUI起始页(SAP轻松访问),我们可以通过点击[收藏夹]或者在界面左上角的输入框输入对应的事务代码,直接进入对应事务的界面.但是下面列举的场景,你是否知道 ...

  9. Spring事务状态处理

    Spring事务提交后执行:深入理解和实践 在Java开发中,Spring框架的事务管理是一个核心概念,尤其是在企业级应用中.理解和正确使用Spring事务对于保证应用的数据一致性和稳定性至关重要.本 ...

  10. 文心一言 VS 讯飞星火 VS chatgpt (137)-- 算法导论11.3 3题

    三.用go语言,考虑除法散列法的另一种版本,其中 h(k) = k mod m,m=$2^p-1$,k为按基数 $2^p$ 表示的字符串.试证明:如果串可由串 y 通过其自身的字符置换排列导出,则x和 ...