Shell脚本之sed编辑器介绍(学习笔记)

一、介绍 

        sed编辑器被称作流编辑器,和普通的交互式文本编辑器恰好相反。它是文本处理中非常有用的工具,能够完美的配合正则表达式使用,处理时,把当前处理的行存储在临 时缓冲区中,称为模式空间,接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏 幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变。

        在流编辑器将所有命令与一行数据匹配完毕后,它会读取下一行数据并重复这个过程。在流编辑器处理 完流中的所有数据行后,它就会终止。由于命令是按顺序逐行给出的, sed编辑器只需对数据流进行一 遍处理就可以完成编辑操作。这使得sed编辑器要比交互式编辑器快得多,你可以快速完成对数据的自动修改。

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

        这个例子在sed编辑器中使用了s命令。 s命令会用斜线间指定的第二个文本字符串来替换第一个文本字 符串模式。在本例中是big test替换了test。 在运行这个例子时,结果应该立即就会显示出来。这就是使用sed编辑器的强大之处。当然,这个简单的测试只是修改了一行数据。不过就算编辑整个文件,处理速度也相差无几。

[root@kittod ~]# 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.
The quick brown fox jumps over the lazy dog.
[root@kittod ~]# 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.
The quick brown fox jumps over the lazy cat.

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

[root@kittod ~]# 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.
The quick green fox jumps over the lazy cat.
#两个命令都作用到文件中的每行数据上。命令之间必须用分号隔开,并且在命令末尾和分号之间不能有
空格。

        从文件中读取编辑器命令: 最后,如果有大量要处理的sed命令,那么将它们放进一个单独的文件中通常会更方便一些。可以在sed 命令中用-f选项来指定文件。

[root@kittod ~]# cat script1.sed
s/brown/green/
s/fox/elephant/
s/dog/cat/
[root@kittod ~]# 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.
The quick green elephant jumps over the lazy cat.

        我们很容易就会把sed编辑器脚本文件与bash shell脚本文件搞混。为了避免这种情况,可以使用.sed作 为sed脚本文件的扩展名。

二、sed编辑器基础
1、替换标记
[root@kittod ~]# cat data2.txt
This is a test of the test script.
This is the second test of the test script.
[root@kittod ~]# sed 's/test/trial/' data2.txt
This is a trial of the test script.
This is the second trial of the test script.

        替换命令在替换多行中的文本时能正常工作,但默认情况下只替换每行中出现的第一处。要让替换命 令能够替换一行中不同地方出现的文本必须使用替换标记( substitution flag)。

s/pattern/replacement/flags

有4种可用的替换标记:

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

[root@kittod ~]# sed 's/test/trial/2' data2.txt
This is a test of the trial script.
This is the second test of the trial script.
#/2:只替换每行中第二次出现的匹配模式

(2)、g,表明新文本将会替换所有匹配的文本;

[root@kittod ~]# sed 's/test/trial/g' data2.txt
This is a trial of the trial script.
This is the second trial of the trial script.
#/g: g替换标记使你能替换文本中匹配模式所匹配的每处地方。

(3)、p,表明原先行的内容要打印出来:p替换标记会打印与替换命令中指定的模式匹配的行。这通常会和sed的-n选项一起使用。

[root@kittod ~]# cat data3.txt
This is a test line.
This is a different line.
[root@kittod ~]# sed -n 's/test/trial/p' data3.txt
This is a trial line.

(4)、w file,将替换的结果写到文件中

[root@kittod ~]# cat data3.txt
This is a test line.
This is a different line.
[root@kittod ~]# sed -n 's/test/trial/p' data3.txt
This is a trial line.

        -n选项将禁止sed编辑器输出。但p替换标记会输出修改过的行。将二者配合使用的效果就是只输出被替换命令修改过的行。 w替换标记会产生同样的输出,不过会将输出保存到指定文件中。

[root@kittod ~]# sed 's/test/trial/w test.txt' data3.txt
This is a trial line.
This is a different line.
[root@kittod ~]# cat test.txt
This is a trial line.
2、替换字符

        有时你会在文本字符串中遇到一些不太方便在替换模式中使用的字符。 Linux中一个常见的例子就是正斜线( /)。 替换文件中的路径名会比较麻烦。比如,如果想用C shell替换/etc/passwd文件中的bash shell,必须这么做 :

[root@kittod ~]# sed -n 's/\/bin\/bash/\/bin\/csh/p' /etc/passwd
root:x:0:0:root:/root:/bin/csh
redhat:x:1000:1000:redhat:/home/redhat:/bin/csh

        由于正斜线通常用作字符串分隔符,因而如果它出现在了模式文本中的话,必须用反斜线来转义。这通常会带来一些困惑和错误。 要解决这个问题, sed编辑器允许选择其他字符来作为替换命令中的字符串分隔符:

[root@kittod ~]# sed -n 's!/bin/bash!/bin/csh!p' /etc/passwd
root:x:0:0:root:/root:/bin/csh
redhat:x:1000:1000:redhat:/home/redhat:/bin/csh
3、使用地址

        默认情况下,在sed编辑器中使用的命令会作用于文本数据的所有行。如果只想将命令作用于特定行或某 些行,则必须用行寻址。在sed编辑器中有两种形式的行寻址:

        a.数字方式的行寻址:

[root@kittod ~]# 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.

        sed编辑器只修改地址指定的第二行的文本。

[root@kittod ~]# sed '2,3s/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 dog.
#这次使用了行地址区间

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

[root@kittod ~]# 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.
The quick brown fox jumps over the lazy cat.

        b、使用文本模式过滤器

[root@kittod ~]# sed -n '/redhat/s/bash/csh/p' /etc/passwd
redhat:x:1000:1000:redhat:/home/redhat:/bin/csh

        该命令只作用到匹配文本模式的行上。虽然使用固定文本模式能帮你过滤出特定的值,就跟上面这个用 户名的例子一样,但其作用难免有限。 sed编辑器在文本模式中采用了一种称为正则表达式( regular expression)的特性来帮助你创建匹配效果更好的模式。

 4、命令组合

        要在单行上执行多条命令,可以用花括号将多条命令组合在一起。

[root@kittod ~]# sed '3,${
> s/brown/green/
> s/lazy/active/
> }' data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick green fox jumps over the active dog.
The quick green fox jumps over the active dog.
The quick green fox jumps over the active dog.
5、删除行

        删除命令 d名副其实,它会删除匹配指定寻址模式的所有行。使用该命令时要特别小心,如果你忘记加入寻址模式 的话,流中的所有文本行都会被删除。

[root@kittod ~]# 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.
The quick brown fox jumps over the lazy dog.
[root@kittod ~]# sed 'd' data1.txt

        当和指定地址一起使用时,删除命令显然能发挥出最大的功用。可以从数据流中删除特定的文本行,通过行号指定:

[root@kittod ~]# cat data4.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[root@kittod ~]# sed '3d' data4.txt
This is line number 1.
This is line number 2.
This is line number 4.

        当你想从指定行开始往后全部删除但文件后面的内容太长时,可以使用区间指定,或者更简便的是通过特殊的文件结尾字符:

[root@kittod ~]# sed '3,$d' data4.txt
This is line number 1.
This is line number 2.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值