首页 > Operation System, tips > sed的”-i”参数用来回写文件

sed的”-i”参数用来回写文件

2009年5月31日 哈哈 发表评论 阅读评论

经常会有用命令行直接完成编辑文件的操作(而不是像vim进入编辑模式可视化编辑文件)的需求
直接更改文本文件的内容可以用sed来做
但是sed缺省是只改动输出的内容
而原来的文件是不会去动的
比如

sed -s ‘/^to /d/’ in.file

用来删除掉文件in.file里以”to “开头的每一行
但上面的这个命令只是显示正确,真正的in.file却没有被改动
有人曾尝试用

sed -s ‘/^to /d/’ in.file > in.file

这样只会清空文件in.file,也不会有需要的结果
其实sed有个参数”-i”可以用来回写文件
上面的需求可以这样实现:

sed -s ‘/^to /d/’ -i in.file

原创文章,转载请注明: 转载自嘻嘻哈哈的部落格(blog)

本文链接地址: sed的”-i”参数用来回写文件

分类: Operation System, tips 标签:
  1. 2009年6月1日16:26 | #1

    sed还很特殊啊,以为可以用 >

  1. 本文目前尚无任何 trackbacks 和 pingbacks.

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word