shell学习之-sed用法解析_shell笔记之sed编辑器的基础用法(上)

在开始写之前,先申明,本文是自己看书后,用自己的语言总结下来的笔记,如有不对之处,还请各位博友予以指正。

shell脚本最常用的功能之一就是处理文本文件,我们利用编写的shell脚本可以简化重启服务器的操作步骤,重启各种服务,进行各种应用服务的自动化部署,简化了不少操作时间。然而shell脚本也有不足之处,就是在处理文本内容上不是那么灵活了。

假如有多个文件,内容大体类似,但是需要将里面的个别字符替换为我们需要的字符,在这个时候,shell脚本就显得力不从心了。不过sed编辑器的出现,弥补了shell脚本的不足。sed命令行编辑器能够很方便的格式化、插入、修改、替换文本内容。linux下最常用的两个文本命令行编辑器就是sed和gawk,本文记录的是sed命令行编辑器的使用。

使用sed命令格式为 sed options script file   options参数允许自定义sed运行行为

参数如下:-e script

将脚本中指定的命令添加到处理输入时执行的命令中

-f file

将文件中指定的命令添加到处理输入时执行的命令中

-n

不需要为每个命令产生输出,但要等待打印命令

下面用个简单的例子做一下文本字符替换练习:

$ echo "this is study script" | sed 's/script/bashshell/'

this is study bashshell

利用管道,将数据流传送给sed进行替换,本例中sed使用了s命令,用第二个字符串bashshell替换了script。当然,这个例子只是编辑了一行数据,如果多行数据,处理的速度是否能够使用相同时间呢?接着看下面这个例子

$ cat test.txt

this is test script

this is test script

this is test script

this is test script

this is test script

$ sed 's/script/shell/' test.txt

this is test shell

this is test shell

this is test shell

this is test shell

this is test shell

sed命令执行与返回数据几乎同时进行,它在处理每一行数据的同时,会显示执行后的结果,在sed编辑器结束处理整个文件之前,就能看到结果。

注意,sed编辑器并不修改文件中的内容,利用cat test.txt查看文件,结果文件中的内容还是原来的。我们再加上- i参数执行后试试看。

$ sed -i 's/script/shell/' test.txt

执行完毕后,并不显示执行后的结果,但是文件中的script却已经被替换成shell字符了。这就是- i选项的用意。

如果一次性要处理多个命令,sed支持在命令行中使用多个编辑器命令,但是要加上-e参数,使用;将多个命令隔开。

$ sed -e 's/test/dog/;s/script/shell/' test.txt

两个命令之间,必须要用分号隔开,且在命令结尾和分号之间,不能有任何空格。

除了分号分隔命令外,在bash shell当中,我们还可以使用次提示符 ,只需要输入前单引号打开脚本,bash会提示你继续输入,直到你键入后单引号结束。

我们再建立一个新的内容较多的文本。

$ cat test2.txt

this is script test, we are study bash shell

this is script test, we are study bash shell

this is script test, we are study bash shell

this is script test, we are study bash shell

this is script test, we are study bash shell

$ sed -e'

> s/script/shell/

> s/study/learning/

> s/bash/c/' test2.txt

this is shell test, we are learning c shell

this is shell test, we are learning c shell

this is shell test, we are learning c shell

this is shell test, we are learning c shell

this is shell test, we are learning c shell

小提示: 一定要在后单引号出现的同一行上完成该命令,因为bash检测到后单引号就处理命令。

如果有多个sed命令要处理,那么我们可以把这些命令单独保存在一个文件当中,然后加上-f选项指定文件进行处理,会更加方便。我们将上述例子中执行的多个命令写入script1文件当中,然后加上-f选项进行处理。

$ cat script1

s/script/shell/

s/study/learning/

s/bash/c/

$ sed -f script1 test2.txt

this is shell test, we are learning c shell

this is shell test, we are learning c shell

this is shell test, we are learning c shell

this is shell test, we are learning c shell

this is shell test, we are learning c shell

大家已看到上述例子当中,使用s命令将新文本替换某一行的文本,不过,还有其他几个选项可用于文本替换。

对于替换命令替换文本字符串匹配模式的方法,有一个值得注意的地方,下面就先看看这个例子:

$ cat test2.txt

this is script test, we are test bash shell

this is script test, we are test bash shell

this is script test, we are test bash shell

this is script test, we are test bash shell

this is script test, we are test bash shell

$ sed 's/test/study/' test2.txt

this is script study, we are test bash shell

this is script study, we are test bash shell

this is script study, we are test bash shell

this is script study, we are test bash shell

this is script study, we are test bash shell

看到了吗?用s命令将test替换为study的时候,只是替换了文本内每行的第一个匹配字符,第二个匹配字符并没有被替换掉。

要使替换命令继续替换接下来匹配的字符,则必须使用替换标记,替换标记要放在替换命令字符串之后,格式如下:

s/pattern/replacement/flags

就先记录到这里吧,下一篇再接着讲替换标记。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值