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编辑器进行处理。例:

$ 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选项。
命令之间以分号(;)分隔,并且命令末尾和分号之间不能有空格。

$ 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选项指定文件.

$ 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 更多的替换项

看一个例子:

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

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

$ echo "test test" | sed 's/test/hello/g'
hello hello
2.1.1 替换标志

格式:s/pattern/replacement/flag

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

依次举例:


#数值
$ 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

}

$ cat<file1
hello world
hello world
hello world
hello wordl
hello world
2.1.2.1 以数字形式寻址

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

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

将3~5行的hello替换成world

$ 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


$ 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 命令组

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

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

d:删除命令。

例:


$ 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
…’


$ 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

$ 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

$ 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 可以是相对路径或绝对路径。但必须有该文件的写权限。

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

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

[address]r file

$ sed '2r Sedin' file1
test line1
test line2
insert line1
insert line2
test line3
  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值