【问题标题】:insert a line to a specific position in multiple files在多个文件中的特定位置插入一行
【发布时间】:2013-07-10 22:00:00
【问题描述】:

我正在寻找一种在文件中的特定位置插入一行的方法。

有点像这样:using sed

但问题是,我想将输出写入到我获得输入的同一个文件中,并且我需要使用多个文件来执行此操作。

很遗憾这不起作用:sed '3iline 3' input.txt > input.txt

这可行:sed '3iline 3' input.txt > tmp.txt && cat tmp.txt > input.txt

但我不再使用 find 和 exec...

我希望这样的事情是可能的:

find /usr/local/share/ -iname 'xyz.htm' -exec sed '19i<p>TEXT</p>' {} > {} \;

但它不是这样工作的,所以我最终写了一个有效的短脚本。但这仍然困扰着我,因为我一直认为应该可以简短(也许在线)和简单地做到这一点。

我希望有人能指出我正确的方向。

【问题讨论】:

    标签: bash shell sed find


    【解决方案1】:

    如果我理解正确,您需要-i 选项。 这将对原始文件进行更改。它不需要任何其他管道和重定向。

    sed -i.bak '3iline 3' input.txt
    

    这将备份带有 .bak 扩展名的原始文件。

    来自man sed

     -i[SUFFIX], --in-place[=SUFFIX]
           edit files in place (makes backup if extension supplied)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      相关资源
      最近更新 更多