国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 服务器 > Linux sed编辑器

Linux sed编辑器

来源:程序员人生   发布时间:2018-06-11 17:10:27 阅读次数:4707次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. sed编辑器

sed编辑器中是Linux世界中最广泛使用的两个命令行编辑器之1。sed编辑器被称作流编辑器(stream editor),流编辑器在编辑器处理数据之前基于预先提供的1组规则来编辑数据流。sed编辑器可以根据命令来处理数据流中的数据。sed编辑器会履行以下操作:

  1. 1次从输入中读取1行数据。
  2. 根据所提供的编辑器命令匹配数据。
  3. 依照命令修改流中的数据。
  4. 将新的数据输出到STDOUT。

在流编辑器将所有命令和1行数据匹配终了后,它会读取下1行数据重复这个进程。

2. 命令情势

sed命令的格式以下:

sed options script file

3. Demo

  • 基本处理
$ echo "This is a test" | sed 's/test/big test/'
This is a big test

上面的例子中使用了s命令,s命令会用斜线中的第2个文本来替换第1个文本。处理文件以下:

# test文件内容

$ cat test
This is a test.
This is a test.
This is a test.
This is a test.
This is a test.


# sed处理
$ sed 's/test/demo/' test
This is a demo.
This is a demo.
This is a demo.
This is a demo.
This is a demo.

# test文件内容
$ cat test
This is a test.
This is a test.
This is a test.
This is a test.
This is a test.

**注:**sed编辑器不会修改文本文件的数据,它只会将处理后的数据发送到STDOUT。

  • 履行多个命令

-e选项可以履行多个命令,多个命令用;隔开。

$ sed -e 's/a/an/; s/test/egg/' test
This is an egg.
This is an egg.
This is an egg.
This is an egg.
This is an egg.
  • 从文件中读取命令

-f选项可让sed履行文件中的命令。

$ cat script.sed
s/a/an/
s/test/egg/

$ sed -f script.sed test
This is an egg.
This is an egg.
This is an egg.
This is an egg.
This is an egg.

参考文献

  1. Linux命令行与shell脚本编程
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生