Sed

来自开放百科 - 灰狐
跳转到: 导航, 搜索
Wikipedia-35x35.png 您可以在Wikipedia上了解到此条目的英文信息 Sed Thanks, Wikipedia.

Sed流编辑器是用来从文件读取文本或者从管道实现基本的变化。结果送到标准输出。 sed 命令的语法不指定输出文件,但是结果可以通过使用输出重定向来写入到文件中。编辑器并不改变原来的文件。

sed -i 's/Huihoo Power/Huihoo.com/g' *.html 
cat index.html.old | sed "s@`cat find.txt`@`cat replace.txt`@" > index.html

./experiment.html

Here's some text
Some more
<!-- START -->
Text to be cut
Some more text
<!-- END -->
Text after cut section

./replace.txt

replaced

command:

$ cat experiment.html | sed -e "/<\!-- START -->/,/<\!-- END -->/c `cat replace.txt`"

result:

Here's some text
Some more
replaced
Text after cut section


Sed用法


1. 将 filename 文件中的 Giga 字符串替换成 GigaRama

  sed s/Giga/GigaRama/ filename

2. 将 filename 文件中的 xfish 字符串那一行刪除

  sed /xfish/d filename

3. 指定哪一行,将之刪除

  sed '4d' filename

4. 或指定第一行到第几行,将之刪除

  sed '1,4d' filename

5. 将第一行到第五行打印出来

  sed -n 1,5p filename

6. 将 file 文件中出现 xfish 字符串的那一行大都写到 file2 里

  sed -n '/xfish/w file2' file

7. 萬用字元的使用,將 file 檔案內的 xfis? 哪一行寫到 file2 內

  sed '/xfis./w file2' file

8. 萬用字串的使用,將 file 檔案內的 xfis* 哪一行寫到 file2 內

  sed '/xfis*/w file2' file

9. 選定字元的使用,將 file 檔案內的 xfis[abcd] 哪一行寫到 file2 內

  sed '/xfis[abcd]/w file2' file

10. 特別符號的取消,利用 /

  sed s/\<title\>/\<TITLE\>/ file

11. 一行的起頭的取代,將 file 檔案的每一行起頭都加上 Hi..

  sed s/^/Hi.. / file

12. 一行的結尾的取代,將 file 檔案的每一行結尾都加上 Hi..

  sed s/$/Hi.. / file

13. 多重條件的指定,利用 -e 選項

  sed -e 's/Giga/GigaRama/' -e 's/^/Hi../' file

http://www.gnu.org/software/sed/

分享您的观点
个人工具
名字空间

变换
操作
导航
工具箱