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
相关推荐
蓁蓁啊2 小时前
GIT使用SSH 多账户配置
运维·git·ssh
路由侠内网穿透4 小时前
本地部署开源持续集成和持续部署系统 Woodpecker CI 并实现外部访问
服务器·网络·windows·ci/cd·开源
程序猿小三5 小时前
Linux下基于关键词文件搜索
linux·运维·服务器
虚拟指尖5 小时前
Ubuntu编译安装COLMAP【实测编译成功】
linux·运维·ubuntu
Larry_Yanan6 小时前
QML学习笔记(三十四)QML的GroupBox、RadioButton
c++·笔记·qt·学习·ui
椎4956 小时前
苍穹外卖前端nginx错误之一解决
运维·前端·nginx
im_AMBER6 小时前
杂记 14
前端·笔记·学习·web
刘某的Cloud6 小时前
parted磁盘管理
linux·运维·系统·parted
啊?啊?6 小时前
4 解锁 Linux 操作新姿势:man、grep、tar ,创建用户及添加权限等 10 大实用命令详解
linux·服务器·实用指令
程序员老舅6 小时前
干货|腾讯 Linux C/C++ 后端开发岗面试
linux·c语言·c++·编程·大厂面试题