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
相关推荐
programhelp_1 分钟前
Google 2026 New Grad SDE VO 三轮面试详解 | 含Behavioral、Coding、Design
java·服务器·数据库
qq_366032782 分钟前
Claude API中转怎么选?简易api下的国内接入与兼容 OpenAI 接口实践
大数据·运维·人工智能
Donk_674 分钟前
HAProxy实验搭建
运维·负载均衡
樱桃花下的小猫7 分钟前
腐蚀Rust-服务器插件模组教程
服务器·新手友好·云鸢互联·零门槛一键开服·腐蚀rust
青梅橘子皮9 分钟前
Linux---开发工具(2)(makefile、进度条、git、gdb)
linux·运维·服务器
Ether IC Verifier21 分钟前
TCP/IP协议握手原理详解——结合以太网连接过程
服务器·网络·数据库·网络协议·tcp/ip
AI算法沐枫26 分钟前
大一学生如何入门机器学习,深度学习,学习顺序如何?
人工智能·python·深度学习·学习·线性代数·算法·机器学习
剑神一笑26 分钟前
Linux less 命令深度解析:从源码看分页器的设计智慧
linux·运维·less
爱吃柠檬鸭30 分钟前
跨境电商创业的效率法则:用“500块+3天”原则实现低成本快速试错
大数据·服务器
IT大白鼠32 分钟前
Dirty Frag漏洞深度分析:Linux内核页缓存污染漏洞的技术原理与安全防护
linux·安全·dirty frag漏洞