SHELL脚本学习(十)初识 sed编辑器

一、sed 编辑器

sed 编辑器被称作流编辑器,根据命令来处理数据流中的数据。

这些数据要么从命令行输入,要么保存在命令文本文件中。

sed编辑器可以执行下列操作:

  1. 从输入中读取一行数据
  2. 根据所提供的命令匹配数据
  3. 按照命令修改数据流中的数据
  4. 将新的数据输出到STDOUT

sed 命令格式如下:

sed options script file
sed命令行选项

选项 描述
-e commands 在处理输入时,加入额外的sed命令
-f file 在处理输入时,将file中的命令加入到已有的命令中
-n 不产生命令输出,使用p命令完成输出

script指定了应用于流数据的单个命令。如果想使用多个命令可以使用 -e或-f选项。

1.1 在命令行中定义编辑器命令

默认情况下sed会将指定的命令应用于STDIN输入流中

。因此,可以直接将数据通过管道传给sed编辑器进行处理。例:

shell 复制代码
$ echo "I have a apple" | sed "s/apple/orange/"
I have a orange

$ cat < file1
I have a apple
I have a apple
I have a apple
I have a apple

$ sed "s/apple/orange/" ./file1
I have a orange
I have a orange
I have a orange
I have a orange

s:替换命令。会用第二个字符串(orange)替换第一个字符串(apple)。

1.2 在命令行中使用多个命令

如果要在sed命令行中使用多个命令,可以使用-e选项。

命令之间以分号(;)分隔,并且命令末尾和分号之间不能有空格。

shell 复制代码
$ cat <file1
hello world
hello world
ubuntu@VM-8-14-ubuntu:~$ sed -e 's/hello/first/; s/world/second/' file1
first second
first second
1.3 从文件中读取编辑器命令

如果有大量需要执行的命令,那么把它们放到文件中会更方便一些。

可以使用sed命令中的-f选项指定文件.

shell 复制代码
$ cat <file1
hello world
hello world

$ cat < sed_cmd_file
s/hello/first/
s/world/second/

$ sed -f sed_cmd_file file1
first second
first second

二、sed 编辑器基础命令

成功使用sed编辑器的关键在于掌握各式各样的命令和格式。本节介绍一些可以运用于脚本的基础命令和功能。

2.1 更多的替换项

看一个例子:

shell 复制代码
$ echo "test test" | sed 's/test/hello/'
hello test

默认情况下sed命令只替换第一个匹配的单词。如果想全部匹配可以使用替换标志g

shell 复制代码
$ echo "test test" | sed 's/test/hello/g'
hello hello
2.1.1 替换标志

格式:s/pattern/replacement/flag
4种可用的替换标志

标志 描述
数值 指出要替换第几处匹配
g 替换全部匹配
p 打印出替换后的行
w file 将替换的结果写入文件

依次举例:

shell 复制代码
#数值
$ echo "test test" | sed 's/test/hello/1'
hello test

$ echo "test test" | sed 's/test/hello/2'
test hello

#g
$ echo "test test" | sed 's/test/hello/g'
hello hello

#p
$ echo "test test" | sed -n 's/test/hello/p'
hello test

#w file
$ echo "test test" | sed  -n 's/test/hello/w sedout'
$ cat<sedout
hello test
2.1.2 使用地址

默认情况下,sed的命令会作用于所有文本行。如果只想让命令作用于某一行或某几行,则需要使用行寻址。

两种寻址方式:

  1. 以数字形式表示的行区间
  2. 匹配行内文本的模式

格式

[address]command

address{

command1

command2

...

}

shell 复制代码
$ cat<file1
hello world
hello world
hello world
hello wordl
hello world
2.1.2.1 以数字形式寻址

将file1文件中的第3行的world替换成hello

shell 复制代码
$ sed '3s/world/hello/' file1
hello world
hello world
hello hello
hello wordl
hello world

将3~5行的hello替换成world

shell 复制代码
$ sed '3,5s/hello/world/' file1
hello world
hello world
world world
world wordl
world world

$ sed '3,$s/hello/world/' file1
hello world
hello world
world world
world wordl
world world

$ 是最后一行。

2.1.2.2 使用文本模式过滤

格式:

/pattern/command

shell 复制代码
$ cat < file1
test line1
test line2
test line3
$ sed -n '/2/p' file1
test line2

$ sed -n '/line2/s/test/TEST/p' file1
TEST line2
2.1.2.3 命令组

如果希望在单行中执行多条命令,可以用花括号将其组合在一起。

例:

shell 复制代码
$ sed -n '2{s/test/TEST/p
s/line/ /p}' file1
TEST line2
TEST  2
2.2 删除行

d:删除命令。

例:

shell 复制代码
$ sed '2d' file1
test line1
test line3

$ sed '/line1/d' file1
test line2
test line3
2.3 插入和附加文本
  • 插入(insert)(i)命令会在指定行前增加一行。
  • 附加(append)(a)命令会在指定行后增加一行。
    命令格式

sed '[address]command

新行1

新行2

...'

shell 复制代码
$ cat <file1
test line1
test line2
test line3

$ sed '2i\test line new' file1
test line1
test line new
test line2
test line3

$ sed '2a\test line new' file1
test line1
test line2
test line new
test line3
2.4 修改行

修改命令(c)允许修改数据流中整行文本的内容。

格式:

sed '[address]c\

新行' file

shell 复制代码
$ cat <file1
test line1
test line2
test line3

$ sed '3c\
> hello shell' file1
test line1
test line2
hello shell

$ sed '/line1/c\
hello shell' file1
hello shell
test line2
test line3
2.5 转换命令

转换命令(y)是唯一一个可以处理单个字符的sed命令

[address]y/inchars/outchars

shell 复制代码
$ cat <file1
test line1
test line2
test line3

$ sed 'y/t/L/' file1
LesL line1
LesL line2
LesL line3

$ sed '3y/t/L/' file1
test line1
test line2
LesL line3
2.6 使用sed 处理文件
2.6.1写入文件

写入命令(w)用来向文件中写入数据。

命令格式:

[address]w file

file 可以是相对路径或绝对路径。但必须有该文件的写权限。

shell 复制代码
$ sed -n '1,2w sedout' file1
$ cat < sedout
test line1
test line2
2.6.2 从文件读取数据

读命令(r)允许将独立文件中的数据插入到数据流中。

格式:

[address]r file

shell 复制代码
$ sed '2r Sedin' file1
test line1
test line2
insert line1
insert line2
test line3
相关推荐
pyliumy6 分钟前
rsync 全网备份
linux·运维·服务器
@月落9 分钟前
alibaba获得店铺的所有商品 API接口
java·大数据·数据库·人工智能·学习
6230_19 分钟前
git使用“保姆级”教程1——简介及配置项设置
前端·git·学习·html·web3·学习方法·改行学it
苹果醋327 分钟前
SpringCloud系列之一---搭建高可用的Eureka注册中心
运维·nginx
诚诚k33 分钟前
docker存储
运维·docker·容器
sorel_ferris35 分钟前
Ubuntu-24.04中Docker-Desktop无法启动
linux·ubuntu·docker
ggb199937 分钟前
【python的坑】vpn下,python request报错 check_hostname requires server_hostname
linux·运维·服务器
小O_好好学1 小时前
vi | vim基本使用
linux·编辑器·vim
-SGlow-1 小时前
Linux相关概念和重要知识点(4)(自举、vim)
linux·运维·vim
多多*1 小时前
OJ在线评测系统 登录页面开发 前端后端联调实现全栈开发
linux·服务器·前端·ubuntu·docker·前端框架