文章作者:Tyan
博客:noahsnail.com | CSDN | 簡書
sed編輯器中是Linux世界中最廣泛使用的兩個命令行編輯器之1。sed編輯器被稱作流編輯器(stream editor),流編輯器在編輯器處理數據之前基于預先提供的1組規則來編輯數據流。sed編輯器可以根據命令來處理數據流中的數據。sed編輯器會履行以下操作:
在流編輯器將所有命令和1行數據匹配終了后,它會讀取下1行數據重復這個進程。
sed命令的格式以下:
sed options script file
$ 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.
下一篇 微信小程序全套使用指南