linux shellsed命令,Linux 命令 & shell 脚本之10(学习sed编辑器)

sed编辑器会执行下列操作

(1) 一次从输入中读取一行数据。

(2) 根据所提供的编辑器命令匹配数据。

(3) 按照命令修改流中的数据。

(4) 将新的数据输出到STDOUT。

sed命令的格式如下。

sed options script file

sed命令选项

选 项

描 述

-e script

在处理输入时,将script中指定的命令添加到已有的命令中

-f file

在处理输入时,将file中指定的命令添加到已有的命令中

-n

不产生命令输出,使用print命令来完成输出

script参数指定了应用于流数据上的单个命令。如果需要用多个命令,要么使用-e选项在命令行中指定,要么使用-f选项在单独的文件中指定

在命令行定义编辑器命令

在sed编辑器中使用了s命令。s命令会用斜线间指定的第二个文本字符串来替换第一个文本字符串模

[root@MYSQL8 myshell]# echo "This is old str" | sed 's/old/new/'

This is new str

[root@MYSQL8 myshell]# echo "This is old str" | sed "s/old/new/"

This is new str

[root@MYSQL8 myshell]# echo 'This is old str' | sed 's/old/new/'

This is new str

[root@MYSQL8 myshell]# echo "This is a test" | sed 's/test/big test/'

This is a big test

2.编辑整个文件

$ cat data1.txt

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

$ sed 's/dog/cat/' data1.txt

The quick brown fox jumps over the lazy cat.

The quick brown fox jumps over the lazy cat.

The quick brown fox jumps over the lazy cat.

The quick brown fox jumps over the lazy cat.

重要的是,要记住,sed编辑器并不会修改文本文件的数据。它只会将修改后的数据发送到STDOUT

在命令行使用多个编辑器命令,要在sed命令行上执行多个命令时,只要用-e选项就可以了

[root@MYSQL8 myshell]# sed -e 's/brown/green/; s/dog/cat/' data1.txt

The quick green fox jumps over the lazy cat.

The quick green fox jumps over the lazy cat.

The quick green fox jumps over the lazy cat.

The quick green fox jumps over the lazy cat.

从文件中读取编辑器命令

[root@MYSQL8 myshell]# cat script1.sed

s/brown/green/

s/fox/elephant/

s/dog/cat/

[root@MYSQL8 myshell]# sed -f script1.sed data1.txt

The quick green elephant jumps over the lazy cat.

The quick green elephant jumps over the lazy cat.

The quick green elephant jumps over the lazy cat.

The quick green elephant jumps over the lazy cat.

4.要让替换命令能够替换一行中不同地方出现的文本必须使用替换标记(substitution flag)

s/pattern/replacement/flags

有4种可用的替换标记:

 数字,表明新文本将替换第几处模式匹配的地方;

 g,表明新文本将会替换所有匹配的文本;

 p,表明原先行的内容要打印出来;

 w file,将替换的结果写到文件中。

将替换标记指定为2的结果就是:sed编辑器只替换每行中第二次出现的匹配模式

[oracle@DB02 myshell]$ cat data4.txt

--old str is old msg--

--old msg in old str--

--aaa str is aaa msg--

[oracle@DB02 myshell]$ sed 's/old/new/2' data4.txt

--old str is new msg--

--old msg in new str--

--aaa str is aaa msg--

g替换标记使你能替换文本中匹配模式所匹配的每处地方

[oracle@DB02 myshell]$ sed 's/old/new/g' data4.txt

--new str is new msg--

--new msg in new str--

--aaa str is aaa msg--

p替换标记会打印与替换命令中指定的模式匹配的行(通常会和sed的-n选项一起使用,-n选项将禁止sed编辑器输出)

将p -n 二者配合使用的效果就是只输出被替换命令修改过的行

[oracle@DB02 myshell]$ sed -n 's/old/new/p' data4.txt

--new str is old msg--

--new msg in old str--

[oracle@DB02 myshell]$ sed -n 's/old/new/pg' data4.txt

--new str is new msg--

--new msg in new str--

w替换标记会产生同样的输出(和上面 p),不过会将输出保存到指定文件中

[oracle@DB02 myshell]$ sed 's/old/new/gw data4_new_txt' data4.txt

--new str is new msg--

--new msg in new str--

--aaa str is aaa msg--

[oracle@DB02 myshell]$ cat data4_new_txt

--new str is new msg--

--new msg in new str--

[oracle@DB02 myshell]$ sed -n 's/old/new/gw data4_new2_txt' data4.txt

[oracle@DB02 myshell]$ cat data4_new2_txt

--new str is new msg--

--new msg in new str--

选择其他字符来作为替换命令中的字符串分隔符,如 用 “!” 替換 “/”

[oracle@DB02 myshell]$ cat data5.txt

--my homedir is /home/ag, no /home/xag--

--your home is /home/yu, no /home/you--

[oracle@DB02 myshell]$ sed 's!/home!/myhome!' data5.txt

--my homedir is /myhome/ag, no /home/xag--

--your home is /myhome/yu, no /home/you--

[oracle@DB02 myshell]$ sed 's!/home!/myhome!g' data5.txt

--my homedir is /myhome/ag, no /myhome/xag--

--your home is /myhome/yu, no /myhome/you--

用 “#” 替換 “/”

[oracle@DB02 myshell]$ sed 's#/home#/myhome#g' data5.txt

--my homedir is /myhome/ag, no /myhome/xag--

--your home is /myhome/yu, no /myhome/you--

5.用行寻址将命令作用于特定行或某些行

在sed编辑器中有两种形式的行寻址:

 以数字形式表示行区间

 用文本模式来过滤出行

两种形式都使用相同的格式来指定地址:

[address]command

也可以将特定地址的多个命令分组:

address {

command1

command2

command3

}

[oracle@DB02 myshell]$ cat data1.txt

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

1. 数字方式的行寻址

在命令中指定的地址是单个行号

[oracle@DB02 myshell]$ sed '2s/dog/cat/' data1.txt

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy cat.

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

使用了行地址区间(逗号以及结尾行号指定的一定区间范围内的行)

[oracle@DB02 myshell]$ sed '1,3s/dog/cat/' data1.txt

The quick brown fox jumps over the lazy cat.

The quick brown fox jumps over the lazy cat.

The quick brown fox jumps over the lazy cat.

The quick brown fox jumps over the lazy dog.

将命令作用到文本中从某行开始的所有行,可以用特殊地址——美元符

[oracle@DB02 myshell]$ sed '2,$s/dog/cat/' data1.txt

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy cat.

The quick brown fox jumps over the lazy cat.

The quick brown fox jumps over the lazy cat.

2. 使用文本模式过滤器

sed编辑器允许指定文本模式来过滤出命令要作用的行。格式如下:

/pattern/command

必须用正斜线将要指定的pattern封起来。sed编辑器会将该命令作用到包含指定文本模式的行上。

如下將含有 “in” 的行中 old 替換為 new

[oracle@DB02 myshell]$ sed '/in/s/old/new/g' data4.txt

--old str is old msg--

--new msg in new str--

--aaa str is aaa msg--

3. 命令组合

[oracle@DB02 myshell]$ sed '2{

> s/fox/elephant/

> s/dog/cat/

> }' data1.txt

The quick brown fox jumps over the lazy dog.

The quick brown elephant jumps over the lazy cat.

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

[oracle@DB02 myshell]$ cat script3.sed

2{

s/fox/elephant/

s/dog/cat/

}

[oracle@DB02 myshell]$ sed -f script3.sed data1.txt

The quick brown fox jumps over the lazy dog.

The quick brown elephant jumps over the lazy cat.

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

6.删除行(sed编辑器不会修改原始文件。删除的行只是从sed编辑器的输出中消失了)

删除命令d名副其实,它会删除匹配指定寻址模式的所有行

[oracle@DB02 myshell]$ cat data6.txt

This is line number 一.

This is line number 二.

This is line number 三.

This is line number 四.

通过行号指定(第3行)

[oracle@DB02 myshell]$ sed '3d' data6.txt

This is line number 一.

This is line number 二.

This is line number 四.

通过特定行区间指定(從第1行到第3行)

[oracle@DB02 myshell]$ sed '1,3d' data6.txt

This is line number 四.

通过特殊的文件结尾字符(從第1行到最後一行)

[oracle@DB02 myshell]$ sed '2,$d' data6.txt

This is line number 一.

使用文本模式过滤器

[oracle@DB02 myshell]$ sed '/number 一/d' data6.txt

This is line number 二.

This is line number 三.

This is line number 四.

使用两个文本模式来删除某个区间内的行,指定的第一个模式会“打开”行删除功能,

第二个模式会“关闭”行删除功能。sed编辑器会删除两个指定行之间的所有行(包括指定的行)

[oracle@DB02 myshell]$ sed '/一/,/三/d' data6.txt

This is line number 四.

[oracle@DB02 myshell]$ cat data6.txt

This is line number 一.

This is line number 二.

This is line number 三.

This is line number 四.

This is line number 1.

This is line number 2.

This is line number 3.

This is line number 4.

This is line number 一 again.

This is line number 二 again.

This is line number 三 again.

This is line number 四 again.

如搜索後再次出現 “一” 則繼續匹配並刪除

[oracle@DB02 myshell]$ sed '/一/,/三/d' data6.txt

This is line number 四.

This is line number 1.

This is line number 2.

This is line number 3.

This is line number 4.

This is line number 四 again.

7.插入和附加文本

sed编辑器允许向数据流插入和附加文本行。两个操作的区别可能比较让人费解:

 插入(insert)命令(i)会在指定行前增加一个新行;

 附加(append)命令(a)会在指定行后增加一个新行。

这两条命令的费解之处在于它们的格式。它们不能在单个命令行上使用。你必须指定是要将

行插入还是附加到另一行。格式如下:

sed '[address]command\

new line'

new line中的文本将会出现在sed编辑器输出中你指定的位置。记住,当使用插入命令时,

文本会出现在数据流文本的前面。

使用插入命令时,文本会出现在数据流文本的前面。

[oracle@DB02 myshell]$ echo "Test Line 2" | sed 'i\Test Line 1'

Test Line 1

Test Line 2

使用附加命令时,文本会出现在数据流文本的后面

[oracle@DB02 myshell]$ echo "Test Line 2" | sed 'a\Test Line 1'

Test Line 2

Test Line 1

[oracle@DB02 myshell]$ head -n 4 data6.txt > data6_new.txt

[oracle@DB02 myshell]$ cat data6_new.txt

This is line number 一.

This is line number 二.

This is line number 三.

This is line number 四.

在含有 “number 三” 的前面插入一行

[oracle@DB02 myshell]$ sed '/number 三/i\

> This is an inserted line.' data6_new.txt

This is line number 一.

This is line number 二.

This is an inserted line.

This is line number 三.

This is line number 四.

将一个新行插入到数据流第三行前

[oracle@DB02 myshell]$ sed '3i\

> This is an inserted line.' data6_new.txt

This is line number 一.

This is line number 二.

This is an inserted line.

This is line number 三.

This is line number 四.

[oracle@DB02 myshell]$ cat data6_new.txt

This is line number 一.

This is line number 二.

This is line number 三.

This is line number 四.

使用 sed 腳本

[oracle@DB02 myshell]$ cat script6.sed

3i This is an inserted line.

[oracle@DB02 myshell]$ sed -f script6.sed data6_new.txt

This is line number 一.

This is line number 二.

This is an inserted line.

This is line number 三.

This is line number 四.

将一个新行附加到数据流中第三行后

[oracle@DB02 myshell]$ sed '3a\

> This is an appended line.' data6_new.txt

This is line number 一.

This is line number 二.

This is line number 三.

This is an appended line.

This is line number 四.

将新行附加到数据流的末尾,只要用代表数据最后一行的美元符就可以

[oracle@DB02 myshell]$ sed '$a\

> This is a new line of text.' data6_new.txt

This is line number 一.

This is line number 二.

This is line number 三.

This is line number 四.

This is a new line of text.

在数据流起始位置增加一个新行。只要在第一行之前插入新行即可。

要插入或附加多行文本,就必须对要插入或附加的新文本中的每一行使用反斜线,直到最后一行

[oracle@DB02 myshell]$ sed '1i\

> This is one line of new text.\

> This is another line of new text.' data6_new.txt

This is one line of new text.

This is another line of new text.

This is line number 一.

This is line number 二.

This is line number 三.

This is line number 四.

8.修改行

修改(change)命令允许修改数据流中整行文本的内容。它跟插入和附加命令的工作机制一样,你必须在sed命令中单独指定新行

sed编辑器会修改第三行中的文本

[oracle@DB02 myshell]$ sed '3c\

> This is a changed line of text.' data6_new.txt

This is line number 一.

This is line number 二.

This is a changed line of text.

This is line number 四.

可以用文本模式来寻址

[oracle@DB02 myshell]$ sed '/number 三/c\

> This is a changed line of text.' data6_new.txt

This is line number 一.

This is line number 二.

This is a changed line of text.

This is line number 四.

在修改命令中使用地址区间(会用这一行文本来替换数据流中的两行文本,而不是逐一修改这两行文本)

[oracle@DB02 myshell]$ sed '2,3c\

> This is a new line of text.' data6_new.txt

This is line number 一.

This is a new line of text.

This is line number 四.

9.转换命令

(转换命令是一个全局命令,它会文本行中找到的所有指定字符自动进行转换,不会考虑它们出现的位置)

转换(transform)命令(y)是唯一可以处理单个字符的sed编辑器命令。转换命令格式如下:

[address]y/inchars/outchars/

转换命令会对inchars和outchars值进行一对一的映射。inchars中的第一个字符会被转

换为outchars中的第一个字符,第二个字符会被转换成outchars中的第二个字符。这个映射过

程会一直持续到处理完指定字符。如果inchars和outchars的长度不同,则sed编辑器会产生一

条错误消息。

將 一、二、三 轉換成 1、2、3

[oracle@DB02 myshell]$ sed 'y/一二三/123/' data6_new.txt

This is line number 1.

This is line number 2.

This is line number 3.

This is line number 四.

10.打印数据流中的信息

 p命令用来打印文本行;

 等号(=)命令用来打印行号;

 l(小写的L)命令用来列出行。

1. 打印行

[oracle@DB02 myshell]$ echo "this is a test" | sed 'p'

this is a test

this is a test

是打印包含匹配文本模式的行

[oracle@DB02 myshell]$ sed -n '/number 三/p' data6_new.txt

This is line number 三.

在命令行上用-n选项,你可以禁止输出其他行,只打印包含匹配文本模式的行。也可以用它来快速打印数据流中的某些行

[oracle@DB02 myshell]$ sed -n '1,3p' data6_new.txt

This is line number 一.

This is line number 二.

This is line number 三.

[oracle@DB02 myshell]$ sed -n 2p data6_new.txt

This is line number 二.

在修改之前查看行,也可以使用打印命令,比如与替换或修改命令一起使用。可以创建一个脚本在修改行之前显示该行

[oracle@DB02 myshell]$ cat script3.sed

2{

p

s/fox/elephant/

s/dog/cat/p

}

[oracle@DB02 myshell]$ sed -n -f script3.sed data1.txt

The quick brown fox jumps over the lazy dog.

The quick brown elephant jumps over the lazy cat.

2. 打印行号

[oracle@DB02 myshell]$ sed '=' data6_new.txt

1

This is line number 一.

2

This is line number 二.

3

This is line number 三.

4

This is line number 四.

[oracle@DB02 myshell]$ sed -n '/number 三/=' data6_new.txt

3

[oracle@DB02 myshell]$ sed -n '/number 三/{

> =

> p

> }' data6_new.txt

3

This is line number 三.

3. 列出行(列出(list)命令(l)可以打印数据流中的文本和不可打印的ASCII字符)

[oracle@DB02 myshell]$ sed -n 'l' data6_new.txt

This is line number \344\270\200. $

This is line number \344\272\214. $

This is line number \344\270\211. $

This is line number \345\233\233.$

11.使用 sed 处理文件

1. 写入文件

w命令用来向文件写入行。该命令的格式如下:

[address]w filename

将数据流中的前两行打印到一个文本文件data6_tmp.txt中

[oracle@DB02 myshell]$ sed -n '1,2w data6_tmp.txt' data6.txt

[oracle@DB02 myshell]$ cat data6_tmp.txt

This is line number 一.

This is line number 二.

[oracle@DB02 myshell]$ sed -n '/number 三/w data6_tmp.txt' data6.txt

[oracle@DB02 myshell]$ cat data6_tmp.txt

This is line number 三.

This is line number 三 again.

2. 从文件读取数据

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

读取命令的格式如下:

[address]r filename

[root@MYSQL8 myshell]# cat data12.txt

This is an added line.

This is the second added line.

将 data12.txt 文件内容 插入到 data6_new.txt 的第3行后

[root@MYSQL8 myshell] sed '3r data12.txt' data6_new.txt

This is line number 一.

This is line number 二.

This is line number 三.

This is an added line.

This is the second added line.

This is line number 四.

在匹配文本模式的行后插入data12.txt 文件内容

[root@MYSQL8 myshell] sed '/number 二/r data12.txt' data6_new.txt

This is line number 一.

This is line number 二.

This is an added line.

This is the second added line.

This is line number 三.

This is line number 四.

在数据流的末尾添加文本,只需用美元符地址符就行

[root@MYSQL8 myshell] sed '$r data12.txt' data6_new.txt

This is line number 一.

This is line number 二.

This is line number 三.

This is line number 四.

This is an added line.

This is the second added line.

[root@MYSQL8 myshell] cat notice.std

Would the following people:

LIST

please report to the ship captain.

[root@MYSQL8 myshell] cat data11.txt

Blum, R Browncoat

Harken, C Alliance

读取命令的另一个很酷的用法是和删除命令配合使用:利用另一个文件中的数据来替换文件中的占位文本

[root@MYSQL8 myshell] sed '/LIST/{

> r data11.txt

> d

> }' notice.std

Would the following people:

Blum, R Browncoat

Harken, C Alliance

please report to the ship captain.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值